Skip to content

Commit 97e920e

Browse files
committed
Moved name and imports into options
1 parent 81094fa commit 97e920e

4 files changed

Lines changed: 26 additions & 28 deletions

File tree

Sources/Compiler/Driver.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public actor Driver {
6868
public func generate<Lang: Language>(
6969
language: Lang.Type,
7070
to path: Path?,
71-
imports: [String],
72-
databaseName: String,
7371
options: GenerationOptions
7472
) throws {
7573
// An array of all migrations source code
@@ -85,8 +83,6 @@ public actor Driver {
8583
.map { ($0.fileName.split(separator: ".").first?.description, $0.statements) }
8684

8785
let file = try Lang.generate(
88-
imports: imports,
89-
databaseName: databaseName,
9086
migrations: migrations,
9187
queries: queries,
9288
schema: currentSchema,

Sources/Compiler/Gen/Language.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public protocol Language {
2424

2525
/// A file source code containing all of the generated tables, queries and migrations.
2626
static func file(
27-
imports: [String],
28-
databaseName: String,
2927
migrations: [String],
3028
tables: [GeneratedModel],
3129
queries: [(String?, [GeneratedQuery])],
@@ -45,8 +43,6 @@ public protocol Language {
4543

4644
extension Language {
4745
public static func generate(
48-
imports: [String],
49-
databaseName: String,
5046
migrations: [String],
5147
queries: [(String?, [Statement])],
5248
schema: Schema,
@@ -55,8 +51,6 @@ extension Language {
5551
let values = try assemble(queries: queries, schema: schema)
5652

5753
return try file(
58-
imports: imports,
59-
databaseName: databaseName,
6054
migrations: migrations,
6155
tables: values.tables,
6256
queries: values.queries,
@@ -244,10 +238,18 @@ extension Language {
244238
}
245239
}
246240

247-
248-
public typealias GenerationOptions = Set<GenerationOption>
249-
250-
public enum GenerationOption: Hashable {}
241+
public struct GenerationOptions: Sendable {
242+
public var databaseName: String
243+
public var imports: [String]
244+
245+
public init(
246+
databaseName: String? = nil,
247+
imports: [String] = []
248+
) {
249+
self.databaseName = databaseName ?? "DB"
250+
self.imports = imports
251+
}
252+
}
251253

252254
public struct GeneratedModel {
253255
let name: String

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public struct SwiftLanguage: Language {
5757
}
5858

5959
public static func file(
60-
imports: [String],
61-
databaseName: String,
6260
migrations: [String],
6361
tables: [GeneratedModel],
6462
queries: [(String?, [GeneratedQuery])],
@@ -70,7 +68,7 @@ public struct SwiftLanguage: Language {
7068
try ImportDeclSyntax("import Foundation")
7169
try ImportDeclSyntax("import Feather")
7270

73-
for `import` in imports {
71+
for `import` in options.imports {
7472
try ImportDeclSyntax("import \(raw: `import`)")
7573
}
7674

@@ -92,7 +90,7 @@ public struct SwiftLanguage: Language {
9290
}
9391
}
9492

95-
try StructDeclSyntax("struct \(raw: databaseName): Database") {
93+
try StructDeclSyntax("struct \(raw: options.databaseName): Database") {
9694
"let connection: any Feather.Connection"
9795

9896
try declaration(for: migrations, options: options)
@@ -105,7 +103,7 @@ public struct SwiftLanguage: Language {
105103
// This is really only used by the macro since it doesnt have file names
106104
// which really wont happen here but still implement it for completeness.
107105
for query in queries {
108-
try declaration(for: query, databaseName: databaseName, options: options)
106+
try declaration(for: query, databaseName: options.databaseName, options: options)
109107
}
110108
}
111109
}

Sources/FeatherCLI/Feather.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ struct Feather: AsyncParsableCommand {
2525
var additionalImports: String?
2626

2727
mutating func run() async throws {
28-
try await generate(language: SwiftLanguage.self)
28+
let options = GenerationOptions(
29+
databaseName: databaseName,
30+
imports: additionalImports?.split(separator: ",").map(\.description) ?? []
31+
)
32+
33+
try await generate(language: SwiftLanguage.self, options: options)
2934
}
3035

31-
private func generate<Lang: Language>(language: Lang.Type) async throws {
36+
private func generate<Lang: Language>(
37+
language: Lang.Type,
38+
options: GenerationOptions
39+
) async throws {
3240
let driver = Driver()
3341
await driver.add(reporter: StdoutDiagnosticReporter())
3442

@@ -37,13 +45,7 @@ struct Feather: AsyncParsableCommand {
3745
try await driver.generate(
3846
language: Lang.self,
3947
to: output,
40-
imports: additionalImports?.split(separator: ",").map(\.description) ?? [],
41-
databaseName: databaseName,
42-
options: gatherOptions()
48+
options: options
4349
)
4450
}
45-
46-
private func gatherOptions() -> GenerationOptions {
47-
return [] // This originally had options
48-
}
4951
}

0 commit comments

Comments
 (0)