-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.swift
More file actions
executable file
·91 lines (76 loc) · 2.22 KB
/
gen.swift
File metadata and controls
executable file
·91 lines (76 loc) · 2.22 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
#!/usr/bin/env swift
import Foundation
struct ArgumentsParser {
var clearOnly: Bool = false
init(_ args: [String]) {
for a in args {
switch a {
case "--clear", "-c":
self.clearOnly = true
default:
break
}
}
}
}
private func clear(fm: FileManager) throws {
let binDir = "\(fm.currentDirectoryPath)/bin"
do {
let files = try fm.contentsOfDirectory(atPath: binDir)
for f in files where f != ".keep" {
try fm.removeItem(atPath: "\(binDir)/\(f)")
}
} catch {
throw error
}
}
private func shell(_ command: String) {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.standardInput = nil
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
print("\(output)\n")
}
private func main(_ args: ArgumentsParser) {
let fm = FileManager.default
do {
try clear(fm: fm)
if args.clearOnly {
return
}
} catch {
print(error)
return
}
let currentDir = fm.currentDirectoryPath
do {
let dirs = try fm.contentsOfDirectory(atPath: currentDir)
for d in dirs where fm.fileExists(atPath: "\(currentDir)/\(d)/Package.swift") {
shell("""
cd \(currentDir)/\(d) && \
swift build -c release
""")
try fm.createSymbolicLink(
at: URL(fileURLWithPath: "\(currentDir)/bin/\(d)"),
withDestinationURL: URL(fileURLWithPath: "\(currentDir)/\(d)/.build/release/\(d)")
)
}
let shFiles = try fm.contentsOfDirectory(atPath: "\(currentDir)/sh")
for s in shFiles {
try fm.createSymbolicLink(
at: URL(fileURLWithPath: "\(currentDir)/bin/\(s)"),
withDestinationURL: URL(fileURLWithPath: "\(currentDir)/\(s)")
)
}
} catch {
print(error)
}
}
main(ArgumentsParser(CommandLine.arguments))