-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chainable.stencil
86 lines (77 loc) · 3.45 KB
/
Chainable.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
{% macro newlineIfNotLast isLast %}
{% if not isLast %}
{% endif %}
{% endmacro %}
{% macro compiler_condition variable %}
{% set available %}{% if variable.attributes.available[0].arguments[0] and variable.attributes.available[0].arguments[0] %}{{variable.attributes.available[0].arguments[0]}}{% endif %}{% endset %}
{% if available|contains:"iOS 16" %}
#if compiler(>=5.7)
{% endif %}
{% if available|contains:"iOS 15.5" %}
#if compiler(>=5.6.1)
{% endif %}
{% if available|contains:"iOS 15.4" %}
#if compiler(>=5.6)
{% endif %}
{% if available|contains:"iOS 15.2" %}
#if compiler(>=5.5.2)
{% endif %}
{% endmacro %}
{% macro compiler_condition_end variable %}
{% set available %}{% if variable.attributes.available[0].arguments[0] and variable.attributes.available[0].arguments[0] %}{{variable.attributes.available[0].arguments[0]}}{% endif %}{% endset %}
{% if available|contains:"iOS 16" or available|contains:"iOS 15.5" or available|contains:"iOS 15.4" or available|contains:"iOS 15.2" %}
#endif
{% endif %}
{% endmacro %}
{% macro main type %}
// sourcery:file:{{ type.name }}+Chainable.swift
import {{argument.framework}}
{% call compiler_condition type %}
{{type.attributes.available[0]}}
extension {{type.name}}: Chainable {
public typealias ChainableObjectCompatible = {{ type.name }}
}
{{type.attributes.available[0]}}
public extension ChainableObject where Base: {{ type.name }} {
{% for variable in type.allVariables|instance|openSet %}
{% set isContains %}{% for parentVariable in type.inherits[type.inheritedTypes[0]].allVariables|instance|openSet %}{% if parentVariable.name == variable.name %}true{% endif %}{% endfor %}{% endset %}
{% if not isContains == "true" %}
{% call compiler_condition variable %}
{{variable.attributes.available[0]}}
@discardableResult
func {{ variable.name }}(_ newValue: {{ variable.typeName }}) -> Self {
base.{{ variable.name }} = newValue
return self
}
{% call compiler_condition_end variable %}
{% call newlineIfNotLast forloop.last %}
{% endif %}
{% endfor %}
// Methods
{% for method in type.allMethods|instance %}
{% set isContains %}{% for parentMethod in type.inherits[type.inheritedTypes[0]].allMethods|instance %}{% if parentMethod.name == method.name %}true{% endif %}{% endfor %}{% endset %}
{% if not isContains == "true" and method.actualReturnTypeName.name == "Void" and not method.isAsync %}
{% map method.parameters into result1 %}{%if maploop.item.argumentLabel %}{{maploop.item.argumentLabel}}: {{maploop.item.name}}{% else %}{{maploop.item.name}}{% endif %}{%ifnot maploop.last %},{% endif %}{% endmap %}
{% if method.attributes.available[0] %}
{% call compiler_condition method %}
{{method.attributes.available[0]}}
{% elif method.definedInType.isExtension %}
{{ method.definedInType.attributes.available[0]}}
{% endif %}
{{definedInType.attributes.available}}
@discardableResult
func {{ method.name }}{% if method.throws %} throws {% endif %} -> Self {
{% if method.throws %}try {% endif %}base.{{ method.callName }}({% for p in result1 %}{{ p }}{% if not forloop.last %} {% endif %}{% endfor %})
return self
}
{% call compiler_condition_end method %}
{% call newlineIfNotLast forloop.last %}
{% endif %}
{% endfor %}
}
{% call compiler_condition_end type %}
// sourcery:end
{% endmacro %}
{% for type in types.implementing.NeedChainable|class %}
{% call main type %}
{% endfor %}