-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathBuildConfiguration.swift
101 lines (87 loc) · 3 KB
/
BuildConfiguration.swift
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Foundation
import SwiftyJSON
/// A build configuration for a target.
package struct BuildConfiguration: Codable, Equatable, JSONConvertible {
package enum MacroExpressionValue: Sendable, Codable, Equatable {
case bool(Bool)
case string(String)
case stringList([String])
}
package struct ImpartedBuildProperties: Sendable, Codable, Equatable {
package var buildSettings: [String: MacroExpressionValue]
package init(buildSettings: [String: MacroExpressionValue]) {
self.buildSettings = buildSettings
}
}
package var name: String
package var buildSettings: [String: MacroExpressionValue]
package var baseConfigurationFileReferenceGUID: String?
package var impartedBuildProperties: ImpartedBuildProperties
}
extension BuildConfiguration.MacroExpressionValue {
package enum DecodingError: Error {
case unknownBuildSettingsValue
}
package func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .bool(let value):
try container.encode(value ? "YES" : "NO")
case .string(let value):
try container.encode(value)
case .stringList(let value):
try container.encode(value)
}
}
package init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
if let stringValue = try? container.decode(String.self) {
if stringValue == "YES" {
self = .bool(true)
} else if stringValue == "NO" {
self = .bool(false)
} else {
self = .string(stringValue)
}
} else if let arrayValue = try? container.decode([String].self) {
self = .stringList(arrayValue)
} else {
throw DecodingError.unknownBuildSettingsValue
}
}
}
extension BuildConfiguration.MacroExpressionValue: ExpressibleByStringLiteral {
package init(stringLiteral value: String) {
self = .string(value)
}
}
extension BuildConfiguration.MacroExpressionValue: ExpressibleByBooleanLiteral {
package init(booleanLiteral value: Bool) {
self = .bool(value)
}
}
extension BuildConfiguration.MacroExpressionValue: ExpressibleByArrayLiteral {
package init(arrayLiteral elements: String...) {
self = .stringList(elements)
}
}
extension BuildConfiguration.MacroExpressionValue? {
package mutating func append(_ appendingValues: [String]) {
self = .stringList(self.values + appendingValues)
}
package mutating func append(_ appendingValue: String) {
append([appendingValue])
}
private var values: [String] {
switch self {
case .bool(let value):
return [value ? "YES" : "NO"]
case .string(let value):
return [value]
case .stringList(let value):
return value
case .none:
return ["$(inherited)"]
}
}
}