-
Notifications
You must be signed in to change notification settings - Fork 7
/
environment.swift
executable file
·113 lines (95 loc) · 3.67 KB
/
environment.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
#!/usr/bin/env swift sh
// swiftlint:disable all
//
// This script will set up development environment. Much wow.
//
import Foundation
import ArgumentParser // apple/swift-argument-parser ~> 1.0.0
enum ScriptError: Error {
case parseError(String)
case runCommandFail(String)
}
struct EnvironmentScript: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "environment.swift",
abstract: "This script will set up the development environment by downloading all dependencies and generating all code."
)
@Argument(help: "Your API ID from https://my.telegram.org/")
var apiId: Int?
@Argument(help: "Your API hash from https://my.telegram.org/")
var apiHash: String?
func run() throws {
if let apiId, let apiHash {
log("Installing dependencies...")
try install(command: "swiftlint", from: "swiftlint", as: "SwiftLint")
try install(command: "gyb", from: "ggoraa/apps/gyb", as: "GYB")
log("Running GYB...")
try run(command: "./gyb.sh", with: [String(apiId), apiHash])
log("Finished environment setup!")
} else {
log("Please, provide <api_id> and <api_hash> arguments, obtained at https://my.telegram.org/")
}
}
func log(_ message: String) {
print(">>> " + message)
}
func sectionStart(_ message: String) {
}
/// Installs a specified command if not available
/// - Parameters:
/// - command: Command to be installed
/// - brew: Name of the command in Homebrew
/// - display: How this command's name is displayed in logs
func install(command: String, from brew: String, as display: String) throws {
if try runWithOutput(command: "which", with: [command]).contains(command) {
log("\(display) is installed")
} else {
log("\(display) was not found, installing...")
print("\n--- Homebrew output ---\n")
try run(command: "brew", with: ["install", brew])
print("\n------\n")
}
}
/// Runs a supplied shell command.
/// - Parameters:
/// - command: A command to run.
/// - args: Args supplied to it
/// - Throws: Any error, like being unable to parse command's response or a run failure.
/// - Returns: Command's output
@discardableResult
func run(command: String, with args: [String]) throws -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = [command] + args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
/// Runs a supplied shell command.
/// - Parameters:
/// - command: A command to run.
/// - args: Args supplied to it
/// - Throws: Any error, like being unable to parse command's response or a run failure.
/// - Returns: Command's output
@discardableResult
func runWithOutput(command: String, with args: [String]) throws -> String {
let which = Process()
which.executableURL = URL(fileURLWithPath: "/usr/bin/env")
which.arguments = [command] + args
var pipe = Pipe()
which.standardOutput = pipe
do {
try which.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
return output
} else {
throw ScriptError.parseError("Unable to parse '\(command)' command's output")
}
} catch {
throw ScriptError.runCommandFail("Unable to run command \(command)")
}
}
}
EnvironmentScript.main()
// swiftlint:enable all