Skip to content

Commit a46bf91

Browse files
art-divinRuslan Alikhamov
authored and
Ruslan Alikhamov
committed
Integrated async codegen
1 parent a3aa366 commit a46bf91

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

Templates/Main.swifttemplate

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
1-
<%# ================================================== SETUP -%><%_ -%>
2-
<%_ var all = types.all
3-
all += types.protocols.map { $0 }
4-
all += types.protocolCompositions.map { $0 }
1+
func generate(type: Type, methodRegistrar: MethodRegistrar, subscriptRegistrar: SubscriptRegistrar) async -> String {
2+
var bufferStr: String = ""
53
var mockedCount = 0
6-
-%>
7-
8-
<%_ for type in all { -%><%_ -%>
9-
<%_ let autoMockable: Bool = type.inheritedTypes.contains("AutoMockable") || type.annotations["AutoMockable"] != nil
4+
let autoMockable: Bool = type.inheritedTypes.contains("AutoMockable") || type.annotations["AutoMockable"] != nil
105
let protocolToDecorate = types.protocols.first(where: { $0.name == (type.annotations["mock"] as? String) })
11-
let inlineMockable = protocolToDecorate != nil
12-
guard let aProtocol = autoMockable ? type : protocolToDecorate else { continue }
6+
guard let aProtocol = autoMockable ? type : protocolToDecorate else { return bufferStr }
137
mockedCount += 1
14-
8+
var current = Current()
159
let associatedTypes: [String]? = Helpers.extractAssociatedTypes(from: aProtocol)
1610
let attributes: String = Helpers.extractAttributes(from: type.attributes)
1711
let typeAliases: [String] = Helpers.extractTypealiases(from: aProtocol)
1812
let genericTypesModifier: String = Helpers.extractGenericTypesModifier(associatedTypes)
1913
let genericTypesConstraints: String = Helpers.extractGenericTypesConstraints(associatedTypes)
2014
let allSubscripts = aProtocol.allSubscripts
2115
let allVariables = uniques(variables: aProtocol.allVariables.filter({ !$0.isStatic }))
22-
let containsVariables = !allVariables.isEmpty
2316
let allStaticVariables = uniques(variables: aProtocol.allVariables.filter({ $0.isStatic }))
24-
let containsStaticVariables = !allStaticVariables.isEmpty
2517
let allMethods = uniques(methods: aProtocol.allMethods.filter({ !$0.isStatic || $0.isInitializer }))
26-
let selfConstrained = allMethods.map(wrapMethod).contains(where: { $0.returnsGenericConstrainedToSelf || $0.parametersContainsSelf })
18+
let selfConstrained = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.contains(where: { $0.returnsGenericConstrainedToSelf || $0.parametersContainsSelf })
2719
let accessModifier: String = selfConstrained ? "public final" : "open"
28-
Current.accessModifier = accessModifier // TODO: Temporary workaround for access modifiers
20+
current.accessModifier = accessModifier // TODO: Temporary workaround for access modifiers
2921
let inheritFromNSObject = type.annotations["ObjcProtocol"] != nil || attributes.contains("@objc")
3022
let allMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ !$0.isStatic }))
3123
let allStaticMethods = uniques(methods: aProtocol.allMethods.filter({ $0.isStatic && !$0.isInitializer }))
3224
let allStaticMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ $0.isStatic }))
33-
let conformsToStaticMock = !allStaticMethods.isEmpty || !allStaticVariables.isEmpty
34-
let conformsToMock = !allMethods.isEmpty || !allVariables.isEmpty -%><%_ -%><%_ -%>
25+
let conformsToStaticMock = !allStaticMethods.isEmpty || !allStaticVariables.isEmpty-%><%_ -%><%_ -%>
3526
<%_ if autoMockable { -%>
3627
// MARK: - <%= type.name %>
3728
<%= attributes %>
@@ -105,39 +96,37 @@
10596
<%# ================================================== VARIABLES -%><%_ -%>
10697
<%_ for variable in allVariables { -%>
10798
<%_ if autoMockable { -%>
108-
<%= stubProperty(variable,"\(type.name)\(mockTypeName)") %>
99+
<%= stubProperty(variable,"\(type.name)\(mockTypeName)", current: current) %>
109100
<%_ } else { %>
110-
<%= stubProperty(variable,"\(type.name)") %>
101+
<%= stubProperty(variable,"\(type.name)", current: current) %>
111102
<%_ } %>
112103
<%_ } %> <%_ -%>
113104

114105
<%# ================================================== STATIC VARIABLES -%><%_ -%>
115106
<%_ for variable in allStaticVariables { -%>
116107
<%_ if autoMockable { -%>
117-
<%= stubProperty(variable,"\(type.name)\(mockTypeName)") %>
108+
<%= stubProperty(variable,"\(type.name)\(mockTypeName)", current: current) %>
118109
<%_ } else { %>
119-
<%= stubProperty(variable,"\(type.name)") %>
110+
<%= stubProperty(variable,"\(type.name)", current: current) %>
120111
<%_ } %>
121112
<%_ } %> <%_ -%>
122113

123114
<%# ================================================== METHOD REGISTRATIONS -%><%_ -%>
124-
<%_ MethodWrapper.clear() -%>
125-
<%_ SubscriptWrapper.clear() -%>
126115
<%_ if autoMockable { -%>
127-
<%_ Current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%>
116+
<%_ current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%>
128117
<%_ } else { %>
129-
<%_ Current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%>
118+
<%_ current.selfType = "\(type.name)\(mockTypeName)\(genericTypesModifier)" -%>
130119
<%_ } %>
131-
<%_ let wrappedSubscripts = allSubscripts.map(wrapSubscript) -%>
132-
<%_ let wrappedMethods = allMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%>
133-
<%_ let wrappedVariables = allVariables.map(justWrap) -%>
134-
<%_ let wrappedMethodsForMethodType = allMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%>
135-
<%_ let wrappedInitializers = allMethods.map(wrapMethod).filter({ $0.method.isInitializer }) -%>
136-
<%_ let wrappedStaticMethods = allStaticMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%>
137-
<%_ let wrappedStaticVariables = allStaticVariables.map(justWrap) -%>
138-
<%_ let wrappedStaticMethodsForMethodType = allStaticMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%>
139-
<%_ for variable in allVariables { propertyRegister(variable) } -%>
140-
<%_ for variable in allStaticVariables { propertyRegister(variable) } -%>
120+
<%_ let wrappedSubscripts = allSubscripts.map { wrapSubscript($0, current: current, subscriptRegistrar: subscriptRegistrar) } -%>
121+
<%_ let wrappedMethods = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%>
122+
<%_ let wrappedVariables = allVariables.map { justWrap($0, current: current) } -%>
123+
<%_ let wrappedMethodsForMethodType = allMethodsForMethodType.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%>
124+
<%_ let wrappedInitializers = allMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.method.isInitializer }) -%>
125+
<%_ let wrappedStaticMethods = allStaticMethods.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%>
126+
<%_ let wrappedStaticVariables = allStaticVariables.map { justWrap($0, current: current) } -%>
127+
<%_ let wrappedStaticMethodsForMethodType = allStaticMethodsForMethodType.map { wrapMethod($0, current: current, methodRegistrar: methodRegistrar) }.filter({ $0.wrappedInMethodType() }) -%>
128+
<%_ for variable in allVariables { propertyRegister(variable, methodRegistrar: methodRegistrar, current: current) } -%>
129+
<%_ for variable in allStaticVariables { propertyRegister(variable, methodRegistrar: methodRegistrar, current: current) } -%>
141130
<%_ for method in wrappedMethods { method.register() } -%>
142131
<%_ for wrapped in wrappedSubscripts { wrapped.register() } -%>
143132
<%_ for method in wrappedStaticMethods { method.register() } -%><%_ -%>
@@ -176,7 +165,7 @@
176165
<%_ for method in wrappedStaticMethodsForMethodType { -%>
177166
<%= method.methodTypeDeclarationWithParameters() _%>
178167
<%_ } %> <%_ for variable in allStaticVariables { -%>
179-
<%= propertyMethodTypes(variable) %>
168+
<%= propertyMethodTypes(variable, current: current) %>
180169
<%_ } %> <%_ %>
181170
<%_ -%>
182171
static func compareParameters(lhs: StaticMethodType, rhs: StaticMethodType, matcher: Matcher) -> Matcher.ComparisonResult {
@@ -194,7 +183,7 @@
194183
switch self { <%_ for method in wrappedStaticMethodsForMethodType { %>
195184
<%= method.intValueCase -%><% } %>
196185
<%_ for variable in allStaticVariables { -%>
197-
<%= propertyMethodTypesIntValue(variable) %>
186+
<%= propertyMethodTypesIntValue(variable, current: current) %>
198187
<%_ } %> <%_ -%>
199188
}
200189
}
@@ -217,8 +206,8 @@
217206
}
218207

219208
<%_ for variable in allStaticVariables { -%>
220-
<%= wrapProperty(variable).givenConstructorName(prefix: "Static") -%> {
221-
<%= wrapProperty(variable).givenConstructor(prefix: "Static") _%>
209+
<%= wrapProperty(variable, current: current).givenConstructorName(prefix: "Static") -%> {
210+
<%= wrapProperty(variable, current: current).givenConstructor(prefix: "Static") _%>
222211
}
223212
<%_ } %> <%_ %>
224213
<%_ for method in wrappedStaticMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%>
@@ -248,7 +237,7 @@
248237
<%= method.verificationProxyConstructorName(prefix: "Static") -%> { <%= method.verificationProxyConstructor(prefix: "Static") _%> }
249238
<%_ } %> <%_ -%>
250239
<%_ for variable in allStaticVariables { -%>
251-
<%= propertyTypes(variable) %>
240+
<%= propertyTypes(variable, current: current) %>
252241
<%_ } %> <%_ -%>
253242
}
254243

@@ -271,7 +260,7 @@
271260
<%_ for method in wrappedMethodsForMethodType { -%>
272261
<%= method.methodTypeDeclarationWithParameters() _%>
273262
<%_ } -%> <%_ for variable in allVariables { -%>
274-
<%= propertyMethodTypes(variable) %>
263+
<%= propertyMethodTypes(variable, current: current) %>
275264
<%_ } %> <%_ %> <%_ for wrapped in wrappedSubscripts { -%>
276265
<%= wrapped.subscriptCases() _%>
277266
<%_ } %> <%_ %>
@@ -293,7 +282,7 @@
293282
switch self { <%_ for method in wrappedMethodsForMethodType { %>
294283
<%= method.intValueCase -%><% } %>
295284
<%_ for variable in allVariables { -%>
296-
<%= propertyMethodTypesIntValue(variable) %>
285+
<%= propertyMethodTypesIntValue(variable, current: current) %>
297286
<%_ } %> <%_ for wrapped in wrappedSubscripts { -%>
298287
<%= wrapped.intValueCase() %>
299288
<%_ } -%>
@@ -327,8 +316,8 @@
327316
}
328317

329318
<%_ for variable in allVariables { -%>
330-
<%= wrapProperty(variable).givenConstructorName() -%> {
331-
<%= wrapProperty(variable).givenConstructor() _%>
319+
<%= wrapProperty(variable, current: current).givenConstructorName() -%> {
320+
<%= wrapProperty(variable, current: current).givenConstructor() _%>
332321
}
333322
<%_ } %> <%_ %>
334323
<%_ for method in wrappedMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%>
@@ -363,7 +352,7 @@
363352
<%= method.verificationProxyConstructorName() -%> { <%= method.verificationProxyConstructor() _%> }
364353
<%_ } %> <%_ -%>
365354
<%_ for variable in allVariables { -%>
366-
<%= propertyTypes(variable) %>
355+
<%= propertyTypes(variable, current: current) %>
367356
<%_ } %> <%_ -%>
368357
<%_ for wrapped in wrappedSubscripts { -%>
369358
<%= wrapped.verifyConstructorName() -%> { <%= wrapped.verifyConstructor() _%> }
@@ -526,10 +515,41 @@
526515
}
527516

528517
<%_ } else { -%>
518+
<%_ } -%>
529519
// sourcery:end
520+
<%_ return bufferStr -%>
530521
<%_ } -%>
531-
<% } -%>
532-
<%_ if mockedCount == 0 { -%>
522+
523+
<%# ================================================== SETUP -%><%_ -%>
524+
<%_ var all = types.all
525+
all += types.protocols.map { $0 }
526+
all += types.protocolCompositions.map { $0 }
527+
-%>
528+
529+
<%_
530+
531+
let content = try await withThrowingTaskGroup(of: String.self) { group in
532+
for type in all {
533+
group.addTask {
534+
let methodRegistrar = MethodRegistrar()
535+
let subscriptRegistrar = SubscriptRegistrar()
536+
return await generate(type: type, methodRegistrar: methodRegistrar, subscriptRegistrar: subscriptRegistrar)
537+
}
538+
}
539+
var fullContent = ""
540+
for try await content in group {
541+
fullContent.append(content)
542+
}
543+
return fullContent
544+
}
545+
if content.isEmpty {
546+
-%>
533547
// SwiftyMocky: no AutoMockable found.
534548
// Please define and inherit from AutoMockable, or annotate protocols to be mocked
535-
<%_ } -%>
549+
<%_
550+
} else {
551+
-%>
552+
<%= content -%>
553+
<%_
554+
}
555+
-%>

0 commit comments

Comments
 (0)