Created
September 16, 2020 16:37
-
-
Save arpitdsoni/37e30edb94dd8ce5c2521a0b10e805d3 to your computer and use it in GitHub Desktop.
Modified SwiftGen xcassets template for images. Makes static const of type UIImage or NSImage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swiftlint:disable all | |
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen | |
{% if catalogs %} | |
{% set enumName %}{{param.enumName|default:"Images"}}{% endset %} | |
{% set enumNameForStrings %}{{param.enumNameForStrings|default:"ImageNames"}}{% endset %} | |
{% set imageType %}{{param.imageTypeName|default:"Image"}}{% endset %} | |
{% set forceNamespaces %}{{param.forceProvidesNamespaces|default:"false"}}{% endset %} | |
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %} | |
#if os(macOS) | |
import AppKit | |
#elseif os(iOS) | |
import UIKit | |
#elseif os(iOS) || os(tvOS) || os(watchOS) | |
import UIKit | |
#endif | |
// swiftlint:disable superfluous_disable_command file_length implicit_return | |
{% macro enumBlock assets %} | |
{% call casesBlock assets "" %} | |
{% if param.allValues %} | |
// swiftlint:disable trailing_comma | |
{% if resourceCount.image > 0 %} | |
{{accessModifier}} static let allImages: [{{imageType}}] = [ | |
{% filter indent:2 %}{% call allValuesBlock assets "image" "" %}{% endfilter %} | |
] | |
{% endif %} | |
// swiftlint:enable trailing_comma | |
{% endif %} | |
{% endmacro %} | |
{% macro enumStringsBlock assets %} | |
{% for asset in assets %} | |
{% if asset.type == "image" %} | |
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = "{{asset.value}}" | |
{% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %} | |
{{accessModifier}} enum {{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | |
{% filter indent:4 %}{% call enumStringsBlock asset.items %}{% endfilter %} | |
} | |
{% elif asset.items %} | |
{% call enumStringsBlock asset.items %} | |
{% endif %} | |
{% endfor %} | |
{% endmacro %} | |
{% macro casesBlock assets assetName %} | |
{% for asset in assets %} | |
{% if asset.type == "image" %} | |
{% set namedValue %}{% if assetName != "" %}{{imageType}}(named: {{enumNameForStrings}}{{assetName}}.{{asset.value|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}){% else %}{{imageType}}(named: {{enumNameForStrings}}.{{asset.value|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}){% endif %}{% endset %} | |
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{namedValue}} | |
{% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %} | |
{{accessModifier}} enum {{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | |
{% set conAssetName %}{{assetName}}.{{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}}{% endset %} | |
{% filter indent:2 %}{% call casesBlock asset.items conAssetName %}{% endfilter %} | |
} | |
{% elif asset.items %} | |
{% call casesBlock asset.items "" %} | |
{% endif %} | |
{% endfor %} | |
{% endmacro %} | |
{% macro allValuesBlock assets filter prefix %} | |
{% for asset in assets %} | |
{% if asset.type == filter %} | |
{{prefix}}{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}, | |
{% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %} | |
{% set prefix2 %}{{prefix}}{{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}}.{% endset %} | |
{% call allValuesBlock asset.items filter prefix2 %} | |
{% elif asset.items %} | |
{% call allValuesBlock asset.items filter prefix %} | |
{% endif %} | |
{% endfor %} | |
{% endmacro %} | |
// swiftlint:disable identifier_name line_length nesting type_body_length type_name | |
// MARK: - {{enumNameForStrings}} | |
{{accessModifier}} enum {{enumNameForStrings}} { | |
{% if catalogs.count > 1 or param.forceFileNameEnum %} | |
{% for catalog in catalogs %} | |
{{accessModifier}} enum {{catalog.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | |
{% filter indent:2 %}{% call enumStringsBlock catalog.assets %}{% endfilter %} | |
} | |
{% endfor %} | |
{% else %} | |
{% call enumStringsBlock catalogs.first.assets %} | |
{% endif %} | |
} | |
// MARK: - Image Assets | |
{{accessModifier}} enum {{enumName}} { | |
{% if catalogs.count > 1 or param.forceFileNameEnum %} | |
{% for catalog in catalogs %} | |
{{accessModifier}} enum {{catalog.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} { | |
{% filter indent:2 %}{% call enumBlock catalog.assets %}{% endfilter %} | |
} | |
{% endfor %} | |
{% else %} | |
{% call enumBlock catalogs.first.assets %} | |
{% endif %} | |
} | |
// swiftlint:enable identifier_name line_length nesting type_body_length type_name | |
// MARK: - Implementation Details | |
{% if resourceCount.image > 0 %} | |
#if os(macOS) | |
{{accessModifier}} typealias Image = NSImage | |
#elseif os(iOS) || os(tvOS) || os(watchOS) | |
{{accessModifier}} typealias Image = UIImage | |
#endif | |
{{accessModifier}} extension {{imageType}} { | |
convenience fileprivate init(named: String) { | |
#if os(iOS) || os(tvOS) | |
let bundle = {{param.bundle|default:"BundleToken.bundle"}} | |
self.init(named: named, in: bundle, compatibleWith: nil)! | |
#elseif os(macOS) | |
self.init(named: NSImage.Name(named))! | |
#elseif os(watchOS) | |
self.init(named: named)! | |
#endif | |
} | |
} | |
{% endif %} | |
{% if not param.bundle %} | |
// swiftlint:disable convenience_type | |
private final class BundleToken { | |
static let bundle: Bundle = { | |
Bundle(for: BundleToken.self) | |
}() | |
} | |
// swiftlint:enable convenience_type | |
{% endif %} | |
{% else %} | |
// No assets found | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You don't have to call .image every time now.
Output: