-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.swift
More file actions
63 lines (52 loc) · 1.77 KB
/
Config.swift
File metadata and controls
63 lines (52 loc) · 1.77 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
//
// Config.swift
// PureSQL
//
// Created by Wes Wickwire on 8/5/25.
//
import Foundation
import Yams
public struct Config: Codable {
public let queries: String
public let migrations: String
public let output: String?
public let databaseName: String?
public let additionalImports: [String]?
public let tableNamePattern: String?
enum ConfigError: Error, CustomStringConvertible {
case invalidURL(String)
case notFound(searchPath: String)
var description: String {
switch self {
case .invalidURL(let url):
"Invalid URL '\(url)'"
case .notFound(let searchPath):
"Config does not exist in '\(searchPath)'"
}
}
}
public init(at path: String) throws {
guard var url = URL(string: path) else {
throw ConfigError.invalidURL(path)
}
if url.lastPathComponent != "puresql.yaml" {
url.appendPathComponent("puresql.yaml")
}
guard FileManager.default.fileExists(atPath: url.path) else {
throw ConfigError.notFound(searchPath: url.path)
}
let data = try Data(contentsOf: url)
let decoder = YAMLDecoder()
self = try decoder.decode(Config.self, from: data)
}
public func project(at path: String) throws -> Project {
guard let url = URL(string: path) else {
throw ConfigError.invalidURL(path)
}
return Project(
generatedOutputFile: url.appendingPathComponent(output ?? "Queries.swift"),
migrationsDirectory: url.appendingPathComponent(migrations),
queriesDirectory: url.appendingPathComponent(queries)
)
}
}