-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.swift
executable file
·604 lines (546 loc) · 19.5 KB
/
main.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/swift
import Foundation
struct TaskError: Error, CustomStringConvertible {
var description: String {
return self._description
}
let _description: String
init(description: String = "",
_ file: String = #file,
_ function: String = #function,
_ line: Int = #line)
{
self._description = """
------ Error ------
file: \(file)
function: \(function)
line: \(line)
description: \(description)
------ End --------
"""
}
}
class Task {
let task: Process = Process()
init(launchPath: String, arguments: [String] = []) {
self.task.launchPath = launchPath
if !arguments.isEmpty {
self.task.arguments = arguments
}
self.task.standardOutput = Pipe()
self.task.standardError = Pipe()
}
func excute(
printOutput: Bool = true,
_ completion: ((Process) -> Void)? = nil)
-> Bool
{
var success: Bool = false
let group = DispatchGroup()
group.enter()
do {
self.task.terminationHandler = {
success = ($0.terminationStatus == 0)
if let error = String(
data: ($0.standardError as! Pipe).fileHandleForReading.readDataToEndOfFile(),
encoding: .utf8) {
print(error)
}
if printOutput, let output = String(
data: ($0.standardOutput as! Pipe).fileHandleForReading.readDataToEndOfFile(),
encoding: .utf8) {
print(output)
}
completion?($0)
group.leave()
}
print("Run Task: \(self.task.arguments?[0] ?? "") \(self.task.arguments?[1] ?? "") ...")
try self.task.run()
self.task.waitUntilExit()
} catch {
print(error)
group.leave()
}
group.wait()
return success
}
}
class OpenTask: Task {
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/env",
arguments: ["open"] + arguments)
}
static func url(_ urlString: String) throws {
guard let _ = URL(string: urlString),
OpenTask(arguments: [urlString]).excute() else {
throw TaskError()
}
}
}
class XcodebuildTask: Task {
static let projectPath = "./LeanCloud.xcodeproj"
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/xcrun",
arguments: ["xcodebuild"] + arguments)
}
static func version() throws {
guard XcodebuildTask(arguments: ["-version"]).excute() else {
throw TaskError()
}
}
struct Xcodeproj: Decodable {
let project: Project
struct Project: Decodable {
let configurations: [String]
let name: String
let schemes: [String]
let targets: [String]
}
}
static func getXcodeproj(name: String) throws -> Xcodeproj {
var project: Xcodeproj!
var taskError: Error?
_ = XcodebuildTask(arguments: ["-list", "-project", name, "-json"])
.excute(printOutput: false, {
do {
let data = ($0.standardOutput as! Pipe).fileHandleForReading.readDataToEndOfFile()
project = try JSONDecoder().decode(Xcodeproj.self, from: data)
} catch {
taskError = error
}
})
if let error = taskError {
throw error
} else {
return project
}
}
static func building(
project: String,
scheme: String,
configuration: String,
destination: String? = nil)
throws
{
var arguments: [String] = [
"-project", project,
"-scheme", scheme,
"-configuration", configuration]
if let destination = destination {
arguments += ["-destination", destination]
}
arguments += ["clean", "build", "-quiet"]
let success = XcodebuildTask(arguments: arguments).excute {
let argumentsString = String(
data: try! JSONSerialization.data(
withJSONObject: ($0.arguments ?? []),
options: [.prettyPrinted]),
encoding: .utf8)
print("""
------ Build Task ------
Completion Status: \($0.terminationStatus == 0 ? "Complete Success 🎉" : "\($0.terminationStatus)")
Launch Path: \($0.launchPath ?? "")
Arguments: \(argumentsString ?? "")
------ End -------------
""")
}
if !success {
throw TaskError()
}
}
enum Platform: String {
case iOS
case macOS
case tvOS
case watchOS
}
static func building(
project: String = XcodebuildTask.projectPath,
platforms: [Platform] = [.iOS, .macOS, .tvOS, .watchOS])
throws
{
try version()
let xcodeproj = try getXcodeproj(name: project)
let start = Date()
try platforms.forEach { (platform) in
try xcodeproj.project.configurations.forEach { (configuration) in
try building(
project: project,
scheme: "LeanCloud",
configuration: configuration,
destination: platform == .macOS
? "platform=\(platform.rawValue)"
: "generic/platform=\(platform.rawValue)")
}
}
print("\nBuilding Time Cost: \(Date().timeIntervalSince(start) / 60.0) minutes.\n")
}
}
class GitTask: Task {
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/env",
arguments: ["git"] + arguments)
}
static func commitAll(with message: String) throws {
guard GitTask(arguments: ["commit", "-a", "-m", message]).excute() else {
throw TaskError()
}
}
static func lastReleasableMessage() -> String? {
var message: String?
_ = GitTask(
arguments: ["log", "-16", "--pretty=%B|cat"])
.excute(printOutput: false) {
let data = ($0.standardOutput as! Pipe).fileHandleForReading.readDataToEndOfFile()
message = String(data: data, encoding: .utf8)?
.components(separatedBy: .newlines)
.map({ s in s.trimmingCharacters(in: .whitespacesAndNewlines) })
.first(where: { (s) -> Bool in
s.hasPrefix("release") ||
s.hasPrefix("feat") ||
s.hasPrefix("fix") ||
s.hasPrefix("refactor") ||
s.hasPrefix("docs")
})
}
return message
}
}
class HubTask: Task {
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/env",
arguments: ["hub"] + arguments)
}
static func version() throws {
guard HubTask(arguments: ["version"]).excute() else {
throw TaskError()
}
}
enum ReleaseDrafterLabel: String {
case breakingChanges = "feat!"
case newFeatures = "feat"
case bugFixes = "fix"
case maintenanceRefactor = "refactor"
case maintenanceDocs = "docs"
}
static func pullRequest(with message: String) throws {
try version()
var label: String?
if message.hasPrefix(ReleaseDrafterLabel.breakingChanges.rawValue) {
label = ReleaseDrafterLabel.breakingChanges.rawValue
} else if message.hasPrefix(ReleaseDrafterLabel.newFeatures.rawValue) {
label = ReleaseDrafterLabel.newFeatures.rawValue
} else if message.hasPrefix(ReleaseDrafterLabel.bugFixes.rawValue) {
label = ReleaseDrafterLabel.bugFixes.rawValue
} else if message.hasPrefix(ReleaseDrafterLabel.maintenanceRefactor.rawValue) {
label = ReleaseDrafterLabel.maintenanceRefactor.rawValue
} else if message.hasPrefix(ReleaseDrafterLabel.maintenanceDocs.rawValue) {
label = ReleaseDrafterLabel.maintenanceDocs.rawValue
}
guard HubTask(arguments: [
"pull-request",
"--base", "leancloud:master",
"--message", message,
"--force", "--push", "--browse"]
+ (label != nil ? ["--labels", label!] : []))
.excute() else {
throw TaskError()
}
}
}
class PodTask: Task {
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/env",
arguments: ["pod"] + arguments)
}
static func version() throws {
guard PodTask(arguments: ["--version"]).excute() else {
throw TaskError()
}
}
static func trunkPush(
path: String,
repoUpdate: Bool,
wait: Bool)
throws
{
if repoUpdate {
_ = PodTask(arguments: ["repo", "update"]).excute()
}
if PodTask(arguments: ["trunk", "push", path, "--allow-warnings"]).excute() {
if wait {
print("wait for 10 minutes ...")
sleep(60 * 10)
}
} else {
print("[?] try pod trunk push \(path) again? [yes/no]")
if let input = readLine()?.trimmingCharacters(in: .whitespaces).lowercased(),
["y", "ye", "yes"].contains(input) {
try PodTask.trunkPush(
path: path,
repoUpdate: repoUpdate,
wait: wait)
} else {
throw TaskError()
}
}
}
static func trunkPush(paths: [String]) throws {
try version()
for (index, path) in paths.enumerated() {
try PodTask.trunkPush(
path: path,
repoUpdate: (index != 0),
wait: (index != (paths.count - 1)))
}
}
}
class VersionUpdater {
static let versionFilePath: String = "./.version"
static let packageVersionFilePath: String = "./Sources/Foundation/Version.swift"
static let podspecFilePath: String = "./LeanCloud.podspec"
static func checkFileExists(path: String) throws {
guard FileManager.default.fileExists(atPath: path) else {
throw TaskError(description: "\(path) not found.")
}
}
struct Version {
let major: Int
let minor: Int
let revision: Int
let tag: (category: String, number: Int)?
var versionString: String {
var string = "\(major).\(minor).\(revision)"
if let tag = tag {
string = "\(string)-\(tag.category).\(tag.number)"
}
return string
}
init(string: String) throws {
var versionString: String = string
var tag: (String, Int)?
if versionString.contains("-") {
let components = versionString.components(separatedBy: "-")
guard components.count == 2 else {
throw TaskError(description: "invalid semantic version: \(string).")
}
versionString = components[0]
let tagComponents = components[1].components(separatedBy: ".")
guard tagComponents.count == 2,
let tagNumber = Int(tagComponents[1]) else {
throw TaskError(description: "invalid semantic version: \(string).")
}
tag = (tagComponents[0], tagNumber)
}
let numbers = versionString.components(separatedBy: ".")
guard numbers.count == 3,
let major = Int(numbers[0]),
let minor = Int(numbers[1]),
let revision = Int(numbers[2]) else {
throw TaskError(description: "invalid semantic version: \(string).")
}
self.major = major
self.minor = minor
self.revision = revision
self.tag = tag
}
}
static func currentVersion() throws -> Version {
let path = versionFilePath
try checkFileExists(path: path)
return try Version(string: String((try String(contentsOfFile: path))
.trimmingCharacters(in: .whitespacesAndNewlines)))
}
static func newVersion(_ newVersion: Version, replace oldVersion: Version) throws {
let paths = [
versionFilePath,
packageVersionFilePath,
podspecFilePath]
for path in paths {
try checkFileExists(path: path)
try (try String(contentsOfFile: path))
.replacingOccurrences(of: oldVersion.versionString, with: newVersion.versionString)
.write(toFile: path, atomically: true, encoding: .utf8)
}
}
}
class JazzyTask: Task {
static let APIDocsRepoSwiftDirectory = "../api-docs/api/swift"
static let APIDocsTempDirectory = "./api-docs"
convenience init(arguments: [String] = []) {
self.init(
launchPath: "/usr/bin/env",
arguments: ["jazzy"] + arguments)
}
static func version() throws {
guard JazzyTask(arguments: ["--version"]).excute() else {
throw TaskError()
}
}
static func checkAPIDocsRepoSwiftDirectory() throws {
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: APIDocsRepoSwiftDirectory, isDirectory: &isDirectory),
isDirectory.boolValue else {
throw TaskError()
}
}
static func checkAPIDocsTempDirectory() throws {
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: APIDocsTempDirectory, isDirectory: &isDirectory),
isDirectory.boolValue else {
throw TaskError()
}
}
static func generateDocumentation(currentVersion: VersionUpdater.Version) throws {
_ = JazzyTask(arguments: [
"--output", APIDocsTempDirectory,
"--build-tool-arguments",
"-project,\(XcodebuildTask.projectPath),-scheme,LeanCloud,-configuration,Release",
"--author", "LeanCloud",
"--author_url", "https://leancloud.cn",
"--module", "LeanCloud",
"--module-version", currentVersion.versionString,
"--github_url", "https://github.com/leancloud/swift-sdk",
"--github-file-prefix",
"https://github.com/leancloud/swift-sdk/tree/\(currentVersion.versionString)",
"--root-url", "https://leancloud.cn/api-docs/swift/"]
+ (FileManager.default.fileExists(atPath: APIDocsTempDirectory) ? ["--clean"] : []))
.excute()
try checkAPIDocsTempDirectory()
}
static func moveGeneratedDocumentationToRepo() throws {
try FileManager.default.removeItem(atPath: APIDocsRepoSwiftDirectory)
try FileManager.default.moveItem(
atPath: APIDocsTempDirectory,
toPath: APIDocsRepoSwiftDirectory)
}
static func commitPull() throws {
guard GitTask(arguments: [
"-C", APIDocsRepoSwiftDirectory, "pull"])
.excute() else {
throw TaskError()
}
}
static func commitPush() throws {
guard GitTask(arguments: [
"-C", APIDocsRepoSwiftDirectory,
"add", "-A"])
.excute() else {
throw TaskError()
}
guard GitTask(arguments: [
"-C", APIDocsRepoSwiftDirectory,
"commit", "-a", "-m", "update swift sdk docs"])
.excute() else {
throw TaskError()
}
guard GitTask(arguments: [
"-C", APIDocsRepoSwiftDirectory, "push"])
.excute() else {
throw TaskError()
}
}
static func update(currentVersion: VersionUpdater.Version) throws {
try version()
try checkAPIDocsRepoSwiftDirectory()
try commitPull()
try generateDocumentation(currentVersion: currentVersion)
try moveGeneratedDocumentationToRepo()
try commitPush()
try OpenTask.url("https://jenkins.leancloud.cn/job/cn-api-doc-prod-ucloud/build")
}
}
class CLI {
static func help() {
print("""
Actions:\n
b, build Building all schemes
vu, version-update Updating SDK version
pr, pull-request New pull request from current head to base master
pt, pod-trunk Publish all podspecs
adu, api-docs-update Update API Docs
h, help Show help info
""")
}
static func build() throws {
try XcodebuildTask.building()
}
static func versionUpdate() throws {
let currentVersion = try VersionUpdater.currentVersion()
print("""
Current Version is \(currentVersion.versionString)
[?] do you want to update it ? [<new-semantic-version>/no]
""")
if let input = readLine()?.trimmingCharacters(in: .whitespaces).lowercased() {
if !["n", "no", "not"].contains(input.lowercased()) {
let newVersion = try VersionUpdater.Version(string: input)
guard newVersion.versionString != currentVersion.versionString else {
throw TaskError(description: "[!] Version no change")
}
try VersionUpdater.newVersion(newVersion, replace: currentVersion)
try GitTask.commitAll(with: "release: \(newVersion.versionString)")
}
}
}
static func pullRequest() throws {
if let message = GitTask.lastReleasableMessage() {
try HubTask.pullRequest(with: message)
} else {
throw TaskError(description: "Not get a releasable Message.")
}
}
static func podTrunk() throws {
try PodTask.trunkPush(paths: ["LeanCloud.podspec"])
}
static func apiDocsUpdate() throws {
try JazzyTask.update(
currentVersion: try VersionUpdater.currentVersion())
}
static func read() -> [String] {
var args = CommandLine.arguments
args.removeFirst()
return args
}
static func process(action: String) throws {
switch action {
case "b", "build":
try build()
case "vu", "version-update":
try versionUpdate()
case "pr", "pull-request":
try pullRequest()
case "pt", "pod-trunk":
try podTrunk()
case "adu", "api-docs-update":
try apiDocsUpdate()
case "h", "help":
help()
default:
print("[!] Unknown Action: `\(action)`\n")
help()
}
}
static func run() throws {
let args = read()
switch args.count {
case 1:
try process(action: args[0])
default:
print("[!] Unknown Command: `\(args.joined(separator: " "))`\n")
help()
}
}
}
func main() {
do {
try CLI.run()
} catch {
print(error)
}
}
main()