-
Notifications
You must be signed in to change notification settings - Fork 19
/
beak.swift
89 lines (74 loc) · 2.66 KB
/
beak.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
// beak: kareman/SwiftShell @ 5.0.0
// beak: sharplet/Regex @ 2.0.0
// beak: kylef/PathKit @ 1.0.0
import Foundation
import SwiftShell
import Regex
import PathKit
let tool = "beak"
let repo = "https://github.com/yonaskolb/Beak"
/// Formats all the code in the project
public func formatCode() throws {
let formatOptions = "--wraparguments beforefirst --stripunusedargs closure-only --header strip --disable sortedImports,blankLinesAtEndOfScope,blankLinesAtStartOfScope"
try runAndPrint(bash: "swiftformat Sources \(formatOptions)")
try runAndPrint(bash: "swiftformat Tests \(formatOptions)")
}
/// Installs beak
///
/// - Parameters:
/// - directory: The directory to install beak
public func install(directory: String = "/usr/local/bin") throws {
print("🐦 Building Beak...")
let output = run(bash: "swift build --disable-sandbox -c release")
if let error = output.error {
print("Couldn't build:\n\(error)")
return
}
try runAndPrint(bash: "cp -R .build/release/\(tool) \(directory)/\(tool)")
print("🐦 Installed Beak!")
}
/// Updates homebrew formula to a certain version
///
/// - Parameters:
/// - version: The version to release
public func updateBrew(_ version: String) throws {
let releaseTar = "\(repo)/archive/\(version).tar.gz"
let output = run(bash: "curl -L -s \(releaseTar) | shasum -a 256 | sed 's/ .*//'")
guard output.succeeded else {
print("Error retrieving brew SHA")
return
}
let sha = output.stdout
try replaceFile(
regex: "(url \".*/archive/)(.*).tar.gz",
replacement: "$1\(version).tar.gz",
path: "Formula/beak.rb")
try replaceFile(
regex: "sha256 \".*\"",
replacement: "sha256 \"\(sha)\"",
path: "Formula/beak.rb")
run(bash: "git add Formula/beak.rb")
run(bash: "git commit -m \"Updated brew to \(version)\"")
}
/// Releases a new version of Beak
///
/// - Parameters:
/// - version: The version to release
public func release(_ version: String) throws {
try replaceFile(
regex: "public let version: String = \".*\"",
replacement: "public let version: String = \"\(version)\"",
path: "Sources/BeakCLI/BeakCLI.swift")
run(bash: "git add Sources/BeakCLI/BeakCLI.swift")
run(bash: "git commit -m \"Updated to \(version)\"")
run(bash: "git tag \(version)")
print("🐦 Released version \(version)!")
}
func replaceFile(regex: String, replacement: String, path: Path) throws {
let regex = try Regex(string: regex)
let contents: String = try path.read()
let replaced = contents.replacingFirst(matching: regex, with: replacement)
try path.write(replaced)
}
func runMint(package: String, command: String?) {
}