-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupTarget.swift
139 lines (113 loc) Β· 3.74 KB
/
setupTarget.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
#!/usr/bin/env swift
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/*
Helper functions
*/
@discardableResult func runProcess(_ command: String) -> String? {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = command.split(separator: " ").map { String($0) }
let pipe = Pipe()
process.standardOutput = pipe
try? process.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: String.Encoding.utf8)
}
func bold(_ text: String) -> String {
"\u{001B}[1m" + text + "\u{001B}[0m"
}
func printBold(_ text: String) {
print(bold(text))
}
func loadFile(_ name: String) -> String {
try! String(contentsOfFile: name, encoding: .utf8)
}
func writeText(_ text: String, to fileName: String) {
try! text.write(to: URL(fileURLWithPath: fileName), atomically: true, encoding: .utf8)
}
func runRename(for pattern: String) {
// Workaround for sed issue.
let command = "LC_CTYPE=C LANG=C find . -type f -not -path '*/\\.git/*' -exec sed -i '' -e \(pattern) {} +"
writeText(command, to: "rename.sh")
runProcess("bash rename.sh")
runProcess("rm rename.sh")
}
func escapeForRegex(_ text: String) -> String {
text.replacingOccurrences(of: ".", with: "\\.").replacingOccurrences(of: "/", with: "\\/")
}
/*
Validate required tools are installed
*/
["xcodegen"].forEach { tool in
let output = runProcess("which \(tool)")!
if output.contains("not found") || output.isEmpty {
printBold("\(tool) is not installed, please install it first!")
exit(1)
}
}
/*
Gather user input
*/
printBold("Hello π")
print("Welcome in new framework setup script. I will take you thought the whole proces.")
printBold("\nβ‘οΈ Provide framework name(without spaces):")
var name = readLine(strippingNewline: true)!
printBold("\nβ‘οΈ Provide bundle id prefix you would like to use(follow template: com.company.projectName)")
var bundleId = readLine(strippingNewline: true)!
/*
Update template
*/
print("β
")
print("\nRenaming directories")
let command = "find . -name '*CoreFramework*' -exec bash -c 'mv \"$0\" \"${0/CoreFramework/\(name)}\"' {} +"
writeText(command, to: "rename.sh")
runProcess("bash rename.sh")
runProcess("bash rename.sh")
runProcess("rm rename.sh")
print("β
")
print("Setting up the target using new name: \(name)")
runRename(for: "s/CoreFramework/\(name)/g")
print("β
")
print("Updating bundle id's with value: \(bundleId)")
let escapedBundleId = escapeForRegex(bundleId)
runRename(for: "s/com\\.xcodegenDemo/\(escapedBundleId)/g")
print("β
")
print("Running xcodegen")
runProcess("xcodegen generate")
print("β
")
print("Remove setup script")
//runProcess("rm setupTarget.swift")
//runProcess("rm README.md")
print("β
")
printBold("\nWould you like to send anonymous statistical data? (y/n)")
var shareStats = readLine(strippingNewline: true)!
guard shareStats == "y" else {
exit(0)
}
extension String {
var urlEncoded: String {
self.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
}
}
var semaphore = DispatchSemaphore (value: 0)
let params = "app_name=\(name.urlEncoded)&bundle_id=\(bundleId.urlEncoded)"
let urlString = "https://hooks.zapier.com/hooks/catch/4193197/3ro9i2g/?\(params)"
var request = URLRequest(url: URL(string: urlString)!, timeoutInterval: 50)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let _ = data else {
print("Statistical data not sent.")
print(String(describing: error))
semaphore.signal()
return
}
print("Statistical data sent.")
semaphore.signal()
}
task.resume()
semaphore.wait()
printBold("π You are all set here. π")
printBold("Happy coding!")