-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoostBuildVersion.swift
executable file
·93 lines (76 loc) · 2.89 KB
/
BoostBuildVersion.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
#!/usr/bin/env swift
// This script is initially migrated (from the vChewing Project) by Shiki Suen.
import Cocoa
extension String {
fileprivate mutating func regReplace(pattern: String, replaceWith: String = "") {
// Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914
do {
let regex = try NSRegularExpression(
pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines]
)
let range = NSRange(startIndex..., in: self)
self = regex.stringByReplacingMatches(
in: self, options: [], range: range, withTemplate: replaceWith
)
} catch { return }
}
}
func gitCommitCount(branch: String = "main") throws -> Int {
let process = Process()
let pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["git", "rev-list", "--count", branch]
process.standardOutput = pipe
process.standardError = pipe
try process.run()
process.waitUntilExit()
if process.terminationStatus != 0 {
throw NSError(domain: "GitError", code: Int(process.terminationStatus), userInfo: [
NSLocalizedDescriptionKey: "Failed to get commit count for branch \(branch)",
])
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines),
let count = Int(output) else {
throw NSError(domain: "ParseError", code: 1, userInfo: [
NSLocalizedDescriptionKey: "Failed to parse commit count output",
])
}
return count
}
var verBuild: String
do {
var intVerBuild = try gitCommitCount()
intVerBuild += 3001
verBuild = intVerBuild.description
} catch {
print("Failed to get Git Revision Number.")
exit(1)
}
var verMarket: String?
var strXcodeProjContent: String = ""
var dirXcodeProjectFile = "./UnitedPizzaHelper.xcodeproj/project.pbxproj"
var theDictionary: NSDictionary?
if CommandLine.arguments.count == 2 {
verMarket = CommandLine.arguments[1]
}
// Xcode project file version update.
do {
strXcodeProjContent += try String(contentsOfFile: dirXcodeProjectFile, encoding: .utf8)
} catch {
NSLog(" - Exception happened when reading raw phrases data.")
}
strXcodeProjContent.regReplace(
pattern: #"CURRENT_PROJECT_VERSION = .*$"#, replaceWith: "CURRENT_PROJECT_VERSION = " + verBuild + ";"
)
if let verMarket {
strXcodeProjContent.regReplace(
pattern: #"MARKETING_VERSION = .*$"#, replaceWith: "MARKETING_VERSION = " + verMarket + ";"
)
}
do {
try strXcodeProjContent.write(to: URL(fileURLWithPath: dirXcodeProjectFile), atomically: false, encoding: .utf8)
} catch {
NSLog(" -: Error on writing strings to file: \(error)")
}
NSLog(" - Xcode 專案版本資訊更新完成:\(verBuild)。")