Skip to content

Instantly share code, notes, and snippets.

@egabor
Last active February 19, 2022 20:07
Show Gist options
  • Save egabor/866ee3d51017f0c43772c1bfe990d5ab to your computer and use it in GitHub Desktop.
Save egabor/866ee3d51017f0c43772c1bfe990d5ab to your computer and use it in GitHub Desktop.
[COLOR] Snippets for Xcode for generating strongly typed color 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:
- Colors.xcassets
outputs:
params:
enumName: Colors
publicAccess: true
templatePath: "swiftui-colors.stencil"
output: Colors.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
// MARK: - Color Assets
{% macro enumBlock assets %}
{% call casesBlock assets %}
{% endmacro %}
{% macro casesBlock assets %}
{% for asset in assets %}
{% if asset.type == "color" %}
static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = Color("{{asset.value}}")
{% elif asset.items %}
{% call casesBlock asset.items %}
{% endif %}
{% endfor %}
{% endmacro %}
// swiftlint:disable identifier_name line_length nesting type_body_length type_name
{{accessModifier}} extension Color {
{% 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
{% else %}
// No assets found
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment