Skip to content
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
10 changes: 6 additions & 4 deletions Sources/SwordCommand/SwordCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import SwordGenerator
import Yams

@main
struct SwordCommand: ParsableCommand {
struct SwordCommand: AsyncParsableCommand {
@Option(parsing: .upToNextOption)
var targets: [String] = []
@Option(parsing: .upToNextOption)
var inputs: [String] = []
@Option
var output: String

mutating func run() throws {
mutating func run() async throws {
try loadLocalPackagesIfNeeded()

// Parse files in current working directory if no inputs were specified.
Expand All @@ -29,15 +29,17 @@ struct SwordCommand: ParsableCommand {
tree: Parser.parse(source: source)
)
}

let parser = SwordParser()
let reporter = SwordReporter(fileUpdater: .standardOutput)
let parser = SwordParser(reporter: reporter)
let renderer = SwordRenderer()
let exporter = SwordExporter(renderer: renderer)
let generator = SwordGenerator(
parser: parser,
reporter: reporter,
exporter: exporter
)
try generator.generate(
try await generator.generate(
sourceFiles: sourceFiles,
targets: targets,
output: output
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwordFoundation/Scope.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum Scope: String, Codable {
public enum Scope: String, Codable, Sendable {
case single
case weakReference
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftSyntax
import SwordFoundation

struct DependencyDescriptor {
let componentName: ComponentName
let type: Type
let interface: Interface?
let injectedInitializers: [Initializer]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

struct ModuleDescriptor {
struct ModuleDescriptor: Hashable {
let name: String
let componentName: ComponentName
let providers: [ProviderDescriptor]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import SwiftSyntax
import SwordFoundation

struct ProviderDescriptor {
struct ProviderDescriptor: Hashable {
let name: String
let isStaticFunction: Bool
let returnType: Type?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package struct SwordRenderer {
bindingGraph: BindingGraph,
imports: [Import]
) -> String {
let sortedImports = imports.sorted { $0.path < $1.path }
let output = SourceFileSyntax {
for `import` in imports {
for `import` in sortedImports {
ImportDeclSyntax(
importKindSpecifier: `import`.kind.map { .identifier($0) },
path: ImportPathComponentListSyntax {
Expand All @@ -24,14 +25,20 @@ package struct SwordRenderer {
}

private func render(_ bindingGraph: BindingGraph, for component: Component) -> CodeBlockItemListSyntax {
CodeBlockItemListSyntax {
let sortedBindings = bindingGraph.bindings(for: component).sorted {
$0.key.value < $1.key.value
}
let sortedSubcomponents = bindingGraph.subcomponents(for: component).sorted {
$0.name.value < $1.name.value
}
return CodeBlockItemListSyntax {
ExtensionDeclSyntax(
leadingTrivia: .newlines(2),
extendedType: IdentifierTypeSyntax(
name: .identifier(component.name.value)
)
) {
for binding in bindingGraph.bindings(for: component) {
for binding in sortedBindings {
if case .registration(
let parameters,
let calledExpression,
Expand Down Expand Up @@ -139,7 +146,7 @@ package struct SwordRenderer {
}
}

for (index, subcomponent) in bindingGraph.subcomponents(for: component).enumerated() {
for (index, subcomponent) in sortedSubcomponents.enumerated() {
FunctionDeclSyntax(
leadingTrivia: index == 0
? [
Expand Down Expand Up @@ -195,7 +202,7 @@ package struct SwordRenderer {
}
}
}
for subcomponent in bindingGraph.subcomponents(for: component) {
for subcomponent in sortedSubcomponents {
render(bindingGraph, for: subcomponent)
}
}
Expand Down
Loading