|
| 1 | +import Foundation |
| 2 | +import TSCBasic |
| 3 | + |
| 4 | +public struct BuildkiteScriptBuilder { |
| 5 | + |
| 6 | + /// A list of files to run `source` against prior to executing the rest of the script. |
| 7 | + var dependencies = [String]() |
| 8 | + |
| 9 | + /// A list of key/value pairs representing environment variables that should be defined at the start of the script. |
| 10 | + var environmentVariables: [String: Value] = [:] |
| 11 | + |
| 12 | + /// A list of commands to run in the script. |
| 13 | + var commands = [Command]() |
| 14 | + |
| 15 | + public init() { |
| 16 | + self.environmentVariables["BUILDKITE"] = Value(wrapping: "true") |
| 17 | + } |
| 18 | + |
| 19 | + /// Add another dependency to the build script. |
| 20 | + /// |
| 21 | + /// Each dependency will be placed in a `source $DEPENDENCY` block at the top of the emitted build script |
| 22 | + public mutating func addDependency(atPath path: String) { |
| 23 | + self.dependencies.append(path) |
| 24 | + } |
| 25 | + |
| 26 | + /// Add an environment variable pair to the build script. |
| 27 | + /// |
| 28 | + /// If there's an existing environment variable with the same name, it will be overwritten. |
| 29 | + public mutating func addEnvironmentVariable(named key: String, value: String) { |
| 30 | + self.environmentVariables[key] = Value(wrapping: value) |
| 31 | + } |
| 32 | + |
| 33 | + /// Copy environment variables from the existing environment into the build script based on their prefix. |
| 34 | + public mutating func copyEnvironmentVariables( |
| 35 | + prefixedBy prefix: String, |
| 36 | + from environment: [String: String] = ProcessInfo.processInfo.environment |
| 37 | + ) { |
| 38 | + environment |
| 39 | + .filter { $0.key.starts(with: prefix) } |
| 40 | + .forEach { key, value in |
| 41 | + environmentVariables[key] = Value(wrapping: value) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Add a line to the build script. |
| 46 | + /// |
| 47 | + /// This typically takes the form of a single command (like `cp foo bar`). |
| 48 | + public mutating func addCommand(_ command: String, _ arguments: String...) { |
| 49 | + self.commands.append(Command(command: command, arguments: arguments)) |
| 50 | + } |
| 51 | + |
| 52 | + /// Compile the build script into a single string |
| 53 | + public func build() -> String { |
| 54 | + return [ |
| 55 | + dependencies |
| 56 | + .map(convertDependencyToSource) |
| 57 | + .joined(separator: "\n"), |
| 58 | + environmentVariables |
| 59 | + .sorted { $0.0 < $1.0 } |
| 60 | + .filter { !$0.value.rawValue.isEmpty } |
| 61 | + .map(convertEnvironmentVariableToExport) |
| 62 | + .joined(separator: "\n"), |
| 63 | + commands |
| 64 | + .map(escapeCommand) |
| 65 | + .joined(separator: "\n") |
| 66 | + ].joined(separator: "\n") |
| 67 | + } |
| 68 | + |
| 69 | + /// Helper that takes a path like `~/.bashrc` and make it into a bash `source` command. |
| 70 | + /// |
| 71 | + /// Escapes spaces in paths automatically. |
| 72 | + /// |
| 73 | + /// Example: |
| 74 | + /// |
| 75 | + /// ``` |
| 76 | + /// # Given a path of `~/.bashrc`: |
| 77 | + /// source ~/.bashrc |
| 78 | + /// ``` |
| 79 | + func convertDependencyToSource(_ path: String) -> String { |
| 80 | + "source \(path.escapingSpaces)".trimmingWhitespace |
| 81 | + } |
| 82 | + |
| 83 | + /// Helper that takes an environment variable key/value pair to an `export` statement. |
| 84 | + /// |
| 85 | + /// Automatically quote-wraps the value and escapes quotes as needed. |
| 86 | + /// |
| 87 | + /// Example: |
| 88 | + /// |
| 89 | + /// ``` |
| 90 | + /// # Given `foo:bar` |
| 91 | + /// export foo="bar" |
| 92 | + /// ``` |
| 93 | + func convertEnvironmentVariableToExport(_ pair: (String, Value)) -> String { |
| 94 | + return "export \(pair.0)=\"\(pair.1.escapedRepresentation)\"".trimmingWhitespace |
| 95 | + } |
| 96 | + |
| 97 | + /// Helper that wraps command escape logic for shorthand use in a `map` statement. |
| 98 | + func escapeCommand(_ command: Command) -> String { |
| 99 | + command.escapedText |
| 100 | + } |
| 101 | + |
| 102 | + /// An object representing the `value` in an environment variable's key/value pair. |
| 103 | + /// |
| 104 | + /// Mostly just a way to organize escaping |
| 105 | + struct Value: Equatable { |
| 106 | + let rawValue: String |
| 107 | + |
| 108 | + init(wrapping: String) { |
| 109 | + self.rawValue = wrapping |
| 110 | + } |
| 111 | + |
| 112 | + /// An version of this value suitable for placement in a shell script |
| 113 | + var escapedRepresentation: String { |
| 114 | + rawValue |
| 115 | + .escapingCodeQuotes |
| 116 | + .escapingDoubleQuotes |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// An object representing one command in a shell script |
| 121 | + struct Command { |
| 122 | + /// The underlying command. If you want to handle escaping yourself, put the entire command here. |
| 123 | + let command: String |
| 124 | + |
| 125 | + /// The arguments for the command. |
| 126 | + let arguments: [String] |
| 127 | + |
| 128 | + init(command: String, arguments: [String] = []) { |
| 129 | + self.command = command |
| 130 | + self.arguments = arguments |
| 131 | + } |
| 132 | + |
| 133 | + init(_ command: String, _ arguments: String...) { |
| 134 | + self.init(command: command, arguments: arguments) |
| 135 | + } |
| 136 | + |
| 137 | + /// The escaped command text, sutible for placement in a shell script |
| 138 | + var escapedText: String { |
| 139 | + "\(command) \(escapedArguments.joined(separator: " "))".trimmingWhitespace |
| 140 | + } |
| 141 | + |
| 142 | + /// A helper to print only the escaped arguments |
| 143 | + var escapedArguments: [String] { |
| 144 | + arguments.map { $0.spm_shellEscaped() } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +extension String { |
| 150 | + var escapingSpaces: String { |
| 151 | + replacingOccurrences(of: " ", with: "\\ ") |
| 152 | + } |
| 153 | + |
| 154 | + var escapingCodeQuotes: String { |
| 155 | + replacingOccurrences(of: "`", with: "\\`") |
| 156 | + } |
| 157 | + |
| 158 | + var escapingDoubleQuotes: String { |
| 159 | + replacingOccurrences(of: "\"", with: "\\\"") |
| 160 | + } |
| 161 | + |
| 162 | + var trimmingWhitespace: String { |
| 163 | + trimmingCharacters(in: .whitespacesAndNewlines) |
| 164 | + } |
| 165 | +} |
0 commit comments