forked from salemove/ios-sdk-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swiftgen-template.stencil
141 lines (128 loc) · 5.1 KB
/
swiftgen-template.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{% if catalogs %}
{% set colorAlias %}{{param.colorAliasName|default:"AssetColorTypeAlias"}}{% endset %}
{% set imageAlias %}{{param.imageAliasName|default:"Image"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
private let bundleManaging: BundleManaging = .live
#if os(OSX)
import AppKit.NSImage
{{accessModifier}} typealias {{colorAlias}} = NSColor
{{accessModifier}} typealias {{imageAlias}} = NSImage
#elseif os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIImage
{{accessModifier}} typealias {{colorAlias}} = UIColor
{{accessModifier}} typealias {{imageAlias}} = UIImage
#endif
// swiftlint:disable superfluous_disable_command
// swiftlint:disable file_length
{% set enumName %}{{param.enumName|default:"Asset"}}{% endset %}
{% set imageType %}{{param.imageTypeName|default:"ImageAsset"}}{% endset %}
@available(*, deprecated, renamed: "{{imageType}}")
{{accessModifier}} typealias {{enumName}}Type = {{imageType}}
{{accessModifier}} struct {{imageType}} {
{{accessModifier}} fileprivate(set) var name: String
{{accessModifier}} var image: {{imageAlias}} {
let bundle = bundleManaging.current()
#if os(iOS) || os(tvOS)
let image = {{imageAlias}}(named: name, in: bundle, compatibleWith: nil)
#elseif os(OSX)
let image = bundle.image(forResource: NSImage.Name(name))
#elseif os(watchOS)
let image = {{imageAlias}}(named: name)
#endif
guard let result = image else {
assertionFailure("Unable to load image named \(name).")
return {{imageAlias}}()
}
return result
}
}
{% set colorType %}{{param.colorTypeName|default:"ColorAsset"}}{% endset %}
{{accessModifier}} struct {{colorType}} {
{{accessModifier}} fileprivate(set) var name: String
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, OSX 10.13, *)
{{accessModifier}} var color: {{colorAlias}} {
return {{colorAlias}}(asset: self)
}
}
{% macro enumBlock assets sp %}
{{sp}} {% call casesBlock assets sp %}
{{sp}} {% if not param.noAllValues %}
{{sp}} // swiftlint:disable trailing_comma
{{sp}} {{accessModifier}} static let allColors: [{{colorType}}] = [
{{sp}} {% set sp2 %}{{sp}} {% endset %}
{{sp}} {% call allValuesBlock assets "color" "" sp2 %}
{{sp}} ]
{{sp}} {{accessModifier}} static let allImages: [{{imageType}}] = [
{{sp}} {% set sp %}{{sp}} {% endset %}
{{sp}} {% call allValuesBlock assets "image" "" sp2 %}
{{sp}} ]
{{sp}} // swiftlint:enable trailing_comma
{{sp}} @available(*, deprecated, renamed: "allImages")
{{sp}} {{accessModifier}} static let allValues: [{{enumName}}Type] = allImages
{{sp}} {% endif %}
{% endmacro %}
{% macro casesBlock assets sp %}
{{sp}} {% for asset in assets %}
{{sp}} {% if asset.type == "color" %}
{{sp}} {{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{colorType}}(name: "{{asset.value}}")
{{sp}} {% elif asset.type == "image" %}
{{sp}} {{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{imageType}}(name: "{{asset.value}}")
{{sp}} {% elif asset.items %}
{{sp}} {% set sp2 %}{{sp}} {% endset %}
{{sp}} {% call casesBlock asset.items sp2 %}
{{sp}} {% endif %}
{{sp}} {% endfor %}
{% endmacro %}
{% macro allValuesBlock assets filter prefix sp %}
{{sp}} {% for asset in assets %}
{{sp}} {% if asset.type == filter %}
{{sp}} {{prefix}}{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}},
{{sp}} {% elif asset.items %}
{{sp}} {% call allValuesBlock asset.items filter prefix2 sp %}
{{sp}} {% endif %}
{{sp}} {% endfor %}
{% endmacro %}
// swiftlint:disable identifier_name line_length nesting type_body_length type_name
{{accessModifier}} enum {{enumName}} {
{% if catalogs.count > 1 %}
{% for catalog in catalogs %}
{{accessModifier}} enum {{catalog.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% call enumBlock catalog.assets " " %}
}
{% endfor %}
{% else %}
{% call enumBlock catalogs.first.assets "" %}
{% endif %}
}
// swiftlint:enable identifier_name line_length nesting type_body_length type_name
{{accessModifier}} extension {{imageAlias}} {
@available(iOS 1.0, tvOS 1.0, watchOS 1.0, *)
@available(OSX, deprecated,
message: "This initializer is unsafe on macOS, please use the {{imageType}}.image property")
convenience init!(asset: {{imageType}}) {
#if os(iOS) || os(tvOS)
let bundle = bundleManaging.current()
self.init(named: asset.name, in: bundle, compatibleWith: nil)
#elseif os(OSX)
self.init(named: NSImage.Name(asset.name))
#elseif os(watchOS)
self.init(named: asset.name)
#endif
}
}
{{accessModifier}} extension {{colorAlias}} {
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, OSX 10.13, *)
convenience init!(asset: {{colorType}}) {
let bundle = bundleManaging.current()
#if os(iOS) || os(tvOS)
self.init(named: asset.name, in: bundle, compatibleWith: nil)
#elseif os(OSX)
self.init(named: NSColor.Name(asset.name), bundle: bundle)
#elseif os(watchOS)
self.init(named: asset.name)
#endif
}
}
{% else %}
// No assets found
{% endif %}