-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.swift
executable file
·98 lines (80 loc) · 2.72 KB
/
tasks.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
#!/usr/bin/swift
import Foundation
// Constants
let cmd = "xcodebuild"
let lintPaths = "Sources/ Tests/"
let lintFlags = "--quiet --fix"
let testBuild = "xcodebuild build-for-testing"
let testFlags = "-destination 'platform=iOS Simulator,name=iPhone 16,OS=18.3.1' -quiet"
// Maps option keys to the corresponding test schemes.
let testSchemes: [String: String] = [
"pdkit": "PDKitTests",
"patchdata": "PatchDataTests",
"patchday": "PatchDayTests"
]
// Run a shell command (helper).
func run(_ command: String) -> Int32 {
print("🔹 Running: \(command)")
let process = Process()
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
process.arguments = ["-c", command]
process.launchPath = "/bin/zsh"
process.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
let output = String(data: data, encoding: .utf8) ?? ""
if !output.isEmpty {
print(output)
}
if process.terminationStatus != 0 {
print("❌ Command failed: \(command)")
}
return process.terminationStatus
}
// Run a task.
func runTask(_ taskName: String) -> Int32 {
guard let task = tasks[taskName] else {
print("❌ Unknown task: \(taskName). Available tasks: \(tasks.keys.joined(separator: ", "))")
return 1
}
return task()
}
// Run tests.
func runTest(_ scheme: String) -> Int32 {
return build(forTesting: true) == 0 ? run("\(cmd) test -scheme \(scheme) \(testFlags)") : 1
}
// Build PatchDay
func build(forTesting: Bool = false) -> Int32 {
let buildCommand = forTesting ? "\(testBuild) -scheme Tests \(testFlags)" : cmd
return run(buildCommand)
}
// All tasks (commands).
var tasks: [String: () -> Int32] = [
"build": {
let args = CommandLine.arguments.dropFirst(2) // Get arguments after `build`
let isTestBuild = args.contains("--test")
return build(forTesting: isTestBuild)
},
"test": {
let args = CommandLine.arguments.dropFirst(2) // Get test names after `test`
let schemes = args.isEmpty ? Array(testSchemes.values) : args.compactMap { testSchemes[$0] }
if schemes.isEmpty {
print("❌ Invalid test names. Available tests: \(testSchemes.keys.joined(separator: ", "))")
return 1
}
for scheme in schemes {
let res = runTest(scheme)
if res != 0 { return res }
}
return 0
},
"lint": { run("swiftlint lint \(lintPaths) \(lintFlags)") }
]
// ~ Main ~
if CommandLine.arguments.count < 2 {
print("❌ No command provided. Available commands: \(tasks.keys.joined(separator: ", "))")
exit(1)
}
exit(runTask(CommandLine.arguments[1]))