-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathbuild.swift
More file actions
executable file
·207 lines (174 loc) · 5.26 KB
/
build.swift
File metadata and controls
executable file
·207 lines (174 loc) · 5.26 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env swift
import Foundation
// Usage: build.swift platforms [spm|xcode]
func execute(commandPath: String, arguments: [String]) throws {
let task = Process()
task.executableURL = .init(filePath: commandPath)
task.arguments = arguments
print("Launching command: \(commandPath) \(arguments.joined(separator: " "))")
try task.run()
task.waitUntilExit()
guard task.terminationStatus == 0 else {
throw TaskError.code(task.terminationStatus)
}
}
enum TaskError: Error {
case code(Int32)
}
enum Platform: String, CustomStringConvertible {
case iOS_18
case tvOS_18
case macOS_15
case watchOS_11
var destination: String {
switch self {
case .iOS_18:
"platform=iOS Simulator,OS=18.4,name=iPhone 16"
case .tvOS_18:
"platform=tvOS Simulator,OS=18.2,name=Apple TV"
case .macOS_15:
"platform=OS X"
case .watchOS_11:
"OS=11.2,name=Apple Watch Series 10 (46mm)"
}
}
var sdk: String {
switch self {
case .iOS_18:
"iphonesimulator"
case .tvOS_18:
"appletvsimulator"
case .macOS_15:
"macosx15.5"
case .watchOS_11:
"watchsimulator"
}
}
var derivedDataPath: String {
".build/derivedData/" + description
}
var scheme: String {
switch self {
case .iOS_18:
"Valet iOS"
case .tvOS_18:
"Valet tvOS"
case .macOS_15:
"Valet Mac"
case .watchOS_11:
"Valet watchOS"
}
}
var description: String {
rawValue
}
}
enum Task: String, CustomStringConvertible {
case spm
case xcode
var description: String {
rawValue
}
var shouldUseLegacyBuildSystem: Bool {
switch self {
case .spm:
false
case .xcode:
// The new build system choked on our XCTest framework.
// Once this project compiles with the new build system,
// we can change this to false.
true
}
}
var configuration: String {
switch self {
case .spm:
"Release"
case .xcode:
"Debug"
}
}
func scheme(for platform: Platform) -> String {
switch self {
case .spm:
"Valet"
case .xcode:
platform.scheme
}
}
func shouldTest(on platform: Platform) -> Bool {
switch self {
case .spm:
// Our Package isn't set up with unit test targets, because SPM can't run unit tests in a codesigned environment.
false
case .xcode:
true
}
}
}
guard CommandLine.arguments.count > 2 else {
print("Usage: build.swift platforms [spm|xcode]")
throw TaskError.code(1)
}
let rawPlatforms = CommandLine.arguments[1].components(separatedBy: ",")
let rawTask = CommandLine.arguments[2]
guard let task = Task(rawValue: rawTask) else {
print("Received unknown task \(rawTask)")
throw TaskError.code(1)
}
let platforms = try rawPlatforms.map { rawPlatform -> Platform in
guard let platform = Platform(rawValue: rawPlatform) else {
print("Received unknown platform type \(rawPlatform)")
throw TaskError.code(1)
}
return platform
}
for platform in platforms {
var deletedXcodeproj = false
var xcodeBuildArguments: [String] = []
// If necessary, delete Valet.xcodeproj, otherwise xcodebuild won't generate the SPM scheme.
// If deleted, the xcodeproj will be restored by git at the end of the loop.
if task == .spm {
do {
print("Deleting Valet.xcodeproj, any uncommitted changes will be lost.")
try execute(commandPath: "/bin/rm", arguments: ["-r", "Valet.xcodeproj"])
deletedXcodeproj = true
} catch {
print("Could not delete Valet.xcodeproj due to error: \(error)")
throw TaskError.code(1)
}
}
xcodeBuildArguments.append(contentsOf: [
"-scheme", task.scheme(for: platform),
"-sdk", platform.sdk,
"-configuration", task.configuration,
"-PBXBuildsContinueAfterErrors=0",
])
if !platform.destination.isEmpty {
xcodeBuildArguments.append("-destination")
xcodeBuildArguments.append(platform.destination)
}
if task.shouldUseLegacyBuildSystem {
xcodeBuildArguments.append("-UseModernBuildSystem=0")
}
let shouldTest = task.shouldTest(on: platform)
if shouldTest {
xcodeBuildArguments.append("-enableCodeCoverage")
xcodeBuildArguments.append("YES")
xcodeBuildArguments.append("-derivedDataPath")
xcodeBuildArguments.append(platform.derivedDataPath)
}
xcodeBuildArguments.append("build")
if shouldTest {
xcodeBuildArguments.append("test")
}
try execute(commandPath: "/usr/bin/xcodebuild", arguments: xcodeBuildArguments)
if deletedXcodeproj {
do {
print("Restoring Valet.xcodeproj")
try execute(commandPath: "/usr/bin/git", arguments: ["restore", "Valet.xcodeproj"])
} catch {
print("Failed to reset Valet.xcodeproj to last committed version due to error: \(error)")
}
}
}