Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand --serialParse flag to also apply to Composer.uniqueTypesAndFunctions #1301

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ extension Sourcery {
let uniqueTypeStart = currentTimestamp()

// ! All files have been scanned, time to join extensions with base class
let (types, functions, typealiases) = Composer.uniqueTypesAndFunctions(parserResult)
let (types, functions, typealiases) = Composer.uniqueTypesAndFunctions(parserResult, serial: serialParse)


let filesThatHadToBeParsed = allResults
Expand Down
4 changes: 2 additions & 2 deletions SourceryRuntime/Sources/Common/Array+Parallel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public extension Array {
}
}

func parallelPerform(transform: (Element) -> Void) {
func parallelPerform(_ work: (Element) -> Void) {
DispatchQueue.concurrentPerform(iterations: count) { idx in
transform(self[idx])
work(self[idx])
}
}
}
15 changes: 12 additions & 3 deletions SourceryRuntime/Sources/Common/Composer/Composer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public enum Composer {
/// - filters out any private types and extensions
///
/// - Parameter parserResult: Result of parsing source code.
/// - Parameter serial: Whether to process results serially instead of concurrently
/// - Returns: Final types and extensions of unknown types.
public static func uniqueTypesAndFunctions(_ parserResult: FileParserResult) -> (types: [Type], functions: [SourceryMethod], typealiases: [Typealias]) {
public static func uniqueTypesAndFunctions(_ parserResult: FileParserResult, serial: Bool = false) -> (types: [Type], functions: [SourceryMethod], typealiases: [Typealias]) {
let composed = ParserResultsComposed(parserResult: parserResult)

let resolveType = { (typeName: TypeName, containingType: Type?) -> Type? in
Expand All @@ -31,7 +32,7 @@ public enum Composer {
return composed.resolveType(typeName: typeName, containingType: containingType, method: method)
}

composed.types.parallelPerform { type in
let processType = { (type: Type) in
type.variables.forEach {
resolveVariableTypes($0, of: type, resolve: resolveType)
}
Expand All @@ -55,10 +56,18 @@ public enum Composer {
}
}

composed.functions.parallelPerform { function in
let processFunction = { (function: SourceryMethod) in
resolveMethodTypes(function, of: nil, resolve: methodResolveType)
}

if serial {
composed.types.forEach(processType)
composed.functions.forEach(processFunction)
} else {
composed.types.parallelPerform(processType)
composed.functions.parallelPerform(processFunction)
}

updateTypeRelationships(types: composed.types)

return (
Expand Down
2 changes: 1 addition & 1 deletion SourceryRuntime/Sources/Common/TemplateContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class TemplateContext: NSObject, SourceryModel, NSCoding, Diffable
let fileParserResultCopy: FileParserResult? = nil
// fileParserResultCopy = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(NSKeyedArchiver.archivedData(withRootObject: parserResult)) as? FileParserResult

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to put a comment here for future reference, that SwiftSyntax may misbehave when used from background threads (see a relevant comment here), thus false is hardcoded.

let composed = Composer.uniqueTypesAndFunctions(parserResult)
let composed = Composer.uniqueTypesAndFunctions(parserResult, serial: false)
self.types = .init(types: composed.types, typealiases: composed.typealiases)
self.functions = composed.functions

Expand Down
Loading