forked from leancloud/objc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilding.swift
executable file
·379 lines (233 loc) · 8.45 KB
/
building.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/swift
import Foundation
enum Result {
case success(Any?)
case fail(String)
}
let timerQueue: DispatchQueue = DispatchQueue(label: "timerQueue")
var timeoutWorkItem: DispatchWorkItem?
var isTerminating: Bool = false
func script_processor(launchPath: String, arguments: [String], needOutput: Bool) -> Result {
let task: Process = Process()
task.launchPath = launchPath
task.arguments = arguments
let outputPipe: Pipe? = needOutput ? Pipe() : nil
let errorPipe: Pipe = Pipe()
task.standardOutput = outputPipe
task.standardError = errorPipe
task.launch()
timeoutWorkItem = DispatchWorkItem {
isTerminating = true
task.terminate()
}
let timeout: Int = 600
timerQueue.asyncAfter(
deadline: .now() + .seconds(timeout),
execute: timeoutWorkItem!
)
task.waitUntilExit()
timeoutWorkItem?.cancel()
timeoutWorkItem = nil
if isTerminating {
isTerminating = false
return .fail(
"""
❌❌❌
Timeout with Command: [\(arguments.joined(separator: " "))].
In general, there are two cases.
1. Task Process Deadlock
2. The running time of the Task Process is more than Timeout('\(timeout)' seconds)
If it's the secondary case, you can change the Timeout.
"""
)
}
var outputString: String? = nil
if let outputData: Data = outputPipe?.fileHandleForReading.readDataToEndOfFile(),
let outputStr: String = String.init(data: outputData, encoding: .utf8),
!outputStr.isEmpty {
outputString = outputStr
}
if task.terminationStatus == 0 {
if outputString == nil {
outputString =
"""
✅✅✅
Command [\(launchPath) \(arguments.joined(separator: " "))] Success.
"""
}
return .success(outputString)
} else {
let errorData: Data = errorPipe.fileHandleForReading.readDataToEndOfFile()
let errorString: String = String.init(data: errorData, encoding: .utf8)!
if let outputStr: String = outputString {
return .fail(
"""
❌❌❌
\(outputStr)
\(errorString)
"""
)
} else {
return .fail(
"""
❌❌❌
\(errorString)
"""
)
}
}
}
func bash(command: String, arguments: [String]) -> Result {
let commandPathResult: Result = script_processor(
launchPath: "/bin/bash",
arguments: [ "-l", "-c", "which \(command)" ],
needOutput: true
)
switch commandPathResult {
case .success(let string):
guard let path: String = (string as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) else {
return .fail(
"""
❌❌❌
Not found a path for '\(command)'.
"""
)
}
return script_processor(
launchPath: path,
arguments: arguments,
needOutput: true
)
case .fail(_):
return commandPathResult
}
}
func xcodebuild_list(projectPath: String) -> Result {
let arguments: [String] = [
"-list",
"-project", projectPath
]
let result: Result = bash(
command: "xcodebuild",
arguments: arguments
)
switch result {
case .success(let string):
guard let output: String = string as? String else {
fatalError()
}
let lines: [String] = output.components(separatedBy: CharacterSet.newlines)
.map { (item: String) -> (String) in
return item.trimmingCharacters(in: CharacterSet.whitespaces) }
return .success(lines)
case .fail(_):
return result
}
}
func get_project_all_valid_schemes(lines: [String]) -> [String] {
guard var startIndex: Int = lines.index(of: "Schemes:") else {
return []
}
guard let endIndex: Int = lines[startIndex...].index(of: "") else {
return []
}
startIndex += 1
guard startIndex < endIndex else {
return []
}
let arraySlice: ArraySlice<String> = lines[startIndex..<endIndex]
.filter { (item: String) -> Bool in
let isFiltered = !(item.hasSuffix("Tests") || item.hasSuffix("Demo"))
return isFiltered }
return Array(arraySlice)
}
func get_project_all_buildConfigurations(lines: [String]) -> [String] {
guard var startIndex: Int = lines.index(of: "Build Configurations:") else {
return []
}
guard let endIndex: Int = lines[startIndex...].index(of: "") else {
return []
}
startIndex += 1
guard startIndex < endIndex else {
return []
}
let arraySlice: ArraySlice<String> = lines[startIndex..<endIndex]
return Array(arraySlice)
}
func xcodebuild_building_schemes(projectPath: String, schemes: [String], buildConfigurations: [String]) -> Result {
for scheme in schemes {
for buildConfiguration in buildConfigurations {
let arguments: [String] = [
"-project", projectPath,
"-scheme", scheme,
"-configuration", buildConfiguration,
"build",
"-quiet"
]
let result: Result = bash(
command: "xcodebuild",
arguments: arguments
)
switch result {
case .success(let string):
guard let output: String = string as? String else {
fatalError()
}
print(output)
case .fail(_):
return result
}
}
}
return .success(
"""
✅✅✅
The Build of All Schemes are Success.
"""
)
}
func check_if_installed_xcodebuild() -> Result {
let result: Result = bash(
command: "xcodebuild",
arguments: ["-version"]
)
return result
}
func main() {
let projectPath: String = "AVOS/AVOS.xcodeproj"
switch check_if_installed_xcodebuild() {
case .success(_):
break
case .fail(let error):
print(error)
return
}
switch xcodebuild_list(projectPath: projectPath) {
case .success(let stringArray):
guard let lines: [String] = stringArray as? [String] else {
fatalError()
}
let schemes: [String] = get_project_all_valid_schemes(lines: lines)
let buildConfigurations: [String] = get_project_all_buildConfigurations(lines: lines)
for _ in 0..<5 {
switch xcodebuild_building_schemes(
projectPath: projectPath,
schemes: schemes,
buildConfigurations: buildConfigurations
) {
case .success(let string):
guard let output: String = string as? String else {
fatalError()
}
print(output)
return
case .fail(let error):
print(error)
}
}
case .fail(let error):
print(error)
}
}
main()