Skip to content

Instantly share code, notes, and snippets.

@egabor
Last active February 19, 2022 20:07
Show Gist options
  • Save egabor/eb3d6108926e1f0fe6ddcd68a1d6e568 to your computer and use it in GitHub Desktop.
Save egabor/eb3d6108926e1f0fe6ddcd68a1d6e568 to your computer and use it in GitHub Desktop.
[IMAGE] Snippets for Xcode for generating strongly typed image resources. [SwiftGen 6.0]
pod 'SwiftGen', '~> 6.0'
if [[ -f "${PODS_ROOT}/SwiftGen/bin/swiftgen" ]]; then
"${PODS_ROOT}/SwiftGen/bin/swiftgen" config run --config <#$SRCROOT/path-to-config-file#>/swiftgen-colors.yml
else
echo "warning: SwiftGen is not installed. Run 'pod install --repo-update' to install it."
fi
input_dir: <#ProjectName#>/<#Resources#>
output_dir: <#ProjectName#>/<#Resources#>/Generated/
xcassets:
inputs:
- Images.xcassets
outputs:
params:
enumName: Images
publicAccess: true
templatePath: "swiftui-images.stencil"
output: Images.swift
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set enumName %}{{param.enumName|default:"Asset"}}{% endset %}
{% set colorType %}{{param.colorTypeName|default:"ColorAsset"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
import SwiftUI
{% macro imageNameStructBlock assets %}
{% call imageNameLineBlock assets %}
{% endmacro %}
{% macro imageNameLineBlock assets %}
{% for asset in assets %}
{% if asset.type == "image" %}
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String = "{{asset.value}}"
{% elif asset.items %}
{% call imageNameLineBlock asset.items %}
{% endif %}
{% endfor %}
{% endmacro %}
{% macro swiftUIImageExtensionBlock assets %}
{% call swiftUIImageLineBlock assets %}
{% endmacro %}
{% macro swiftUIImageLineBlock assets %}
{% for asset in assets %}
{% if asset.type == "image" %}
static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = Image(ImageName.{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}})
{% elif asset.items %}
{% call swiftUIImageLineBlock asset.items %}
{% endif %}
{% endfor %}
{% endmacro %}
// swiftlint:disable identifier_name line_length nesting type_body_length type_name
// MARK: Image Names
{{accessModifier}} struct ImageName {
{% if catalogs.count > 1 or param.forceFileNameEnum %}
{% for catalog in catalogs %}
{% filter indent:2 %}{% call imageNameStructBlock catalog.assets %}{% endfilter %}
{% endfor %}
{% else %}
{% call imageNameStructBlock catalogs.first.assets %}
{% endif %}
}
// MARK: - Image Assets
{{accessModifier}} extension Image {
{% if catalogs.count > 1 or param.forceFileNameEnum %}
{% for catalog in catalogs %}
{% filter indent:2 %}{% call swiftUIImageExtensionBlock catalog.assets %}{% endfilter %}
{% endfor %}
{% else %}
{% call swiftUIImageExtensionBlock catalogs.first.assets %}
{% endif %}
}
// swiftlint:enable identifier_name line_length nesting type_body_length type_name
{% else %}
// No assets found
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment