-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateCommand.swift
More file actions
86 lines (68 loc) · 2.68 KB
/
GenerateCommand.swift
File metadata and controls
86 lines (68 loc) · 2.68 KB
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
//
// GenerateCommand.swift
// PureSQL
//
// Created by Wes Wickwire on 5/21/25.
//
import ArgumentParser
import Compiler
import Foundation
import SwiftSyntax
struct GenerateCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(commandName: "generate")
@Option(name: .shortAndLong, help: "The directory containing the puresql.yaml")
var path: String = FileManager.default.currentDirectoryPath
@Option(name: .long, help: "If set, the output file overriden to it")
var overrideOutput: String?
@Flag(help: "If set, any diagnostic message will not be colorized")
var dontColorize = false
@Flag(help: "If set, core parts of the compilation will be timed")
var time = false
@Flag(help: "If true, the directory the output exists in will not be created if it doesn't exist")
var skipDirectoryCreate = false
@Flag(help: "If true, it will emit diagnostics that Xcode can understand")
var xcodeDiagnosticReporter = false
@Flag(help: "If true, the output will be dumped to stdout and not not be written to disk")
var dump = false
mutating func run() async throws {
let config = try Config(at: path)
var project = try config.project(at: path)
if let overrideOutput, let url = URL(string: overrideOutput) {
project.generatedOutputFile = url
}
let options = GenerationOptions(
databaseName: config.databaseName ?? "DB",
imports: config.additionalImports ?? [],
createDirectoryIfNeeded: !skipDirectoryCreate,
tableNamePattern: config.tableNamePattern
)
try await generate(
language: SwiftLanguage.self,
options: options,
project: project
)
}
private func generate<Lang: Language>(
language: Lang.Type,
options: GenerationOptions,
project: Project
) async throws {
let driver = Driver()
await driver.logTimes(time)
if xcodeDiagnosticReporter {
await driver.add(reporter: XcodeDiagnosticReporter())
} else {
await driver.add(reporter: StdoutDiagnosticReporter(dontColorize: dontColorize))
}
try await driver.compile(
migrationsPath: project.migrationsDirectory.path,
queriesPath: project.queriesDirectory.path
)
try await driver.generate(
language: Lang.self,
to: dump ? nil : project.generatedOutputFile.path,
options: options
)
print("Generated output to \(project.generatedOutputFile.path)")
}
}