|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Provides convenience APIs for launching and gathering output from a subprocess |
| 4 | +public class ProcessRunner { |
| 5 | + private static let serialQueue = DispatchQueue(label: "\(ProcessRunner.self)") |
| 6 | + |
| 7 | + let process: Process |
| 8 | + var launched = false |
| 9 | + |
| 10 | + public init( |
| 11 | + launchPath: String, |
| 12 | + arguments: [String], |
| 13 | + workingDirectory: String?, |
| 14 | + environment: [String: String] = [:] |
| 15 | + ) { |
| 16 | + process = Process() |
| 17 | + process.launchPath = launchPath |
| 18 | + process.arguments = arguments |
| 19 | + if let workingDirectory { |
| 20 | + process.currentDirectoryURL = URL(fileURLWithPath: workingDirectory) |
| 21 | + } |
| 22 | + process.environment = environment.merging(ProcessInfo.processInfo.environment) { (current, _) in current } |
| 23 | + } |
| 24 | + |
| 25 | + public func run( |
| 26 | + input: String? = nil, |
| 27 | + captureStdout: Bool = true, |
| 28 | + captureStderr: Bool = true |
| 29 | + ) -> ProcessResult { |
| 30 | + let group = DispatchGroup() |
| 31 | + |
| 32 | + let inPipe = Pipe() |
| 33 | + if input != nil { |
| 34 | + process.standardInput = inPipe |
| 35 | + } |
| 36 | + |
| 37 | + var outData = Data() |
| 38 | + if captureStdout { |
| 39 | + let outPipe = Pipe() |
| 40 | + process.standardOutput = outPipe |
| 41 | + addHandler(pipe: outPipe, group: group) { outData.append($0) } |
| 42 | + } |
| 43 | + |
| 44 | + var errData = Data() |
| 45 | + if captureStderr { |
| 46 | + let errPipe = Pipe() |
| 47 | + process.standardError = errPipe |
| 48 | + addHandler(pipe: errPipe, group: group) { errData.append($0) } |
| 49 | + } |
| 50 | + |
| 51 | + ProcessRunner.serialQueue.sync { |
| 52 | + process.launch() |
| 53 | + launched = true |
| 54 | + } |
| 55 | + |
| 56 | + if let input = input { |
| 57 | + guard let data = input.data(using: .utf8) else { |
| 58 | + return ProcessResult( |
| 59 | + status: 1, |
| 60 | + stdout: Data(), |
| 61 | + stderr: "Invalid input".data(using: .utf8)! |
| 62 | + ) |
| 63 | + } |
| 64 | + inPipe.fileHandleForWriting.write(data) |
| 65 | + inPipe.fileHandleForWriting.closeFile() |
| 66 | + } |
| 67 | + |
| 68 | + process.waitUntilExit() |
| 69 | + if captureStdout || captureStderr { |
| 70 | + // Make sure we've received all stdout/stderr |
| 71 | + group.wait() |
| 72 | + } |
| 73 | + |
| 74 | + return ProcessResult( |
| 75 | + status: process.terminationStatus, |
| 76 | + stdout: outData, |
| 77 | + stderr: errData |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + public func terminate() { |
| 82 | + ProcessRunner.serialQueue.sync { |
| 83 | + if launched { |
| 84 | + process.terminate() |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private func addHandler( |
| 90 | + pipe: Pipe, |
| 91 | + group: DispatchGroup, |
| 92 | + addData: @escaping (Data) -> Void |
| 93 | + ) { |
| 94 | + group.enter() |
| 95 | + pipe.fileHandleForReading.readabilityHandler = { fileHandle in |
| 96 | + // Apparently using availableData can cause various issues |
| 97 | + let newData = fileHandle.readData(ofLength: Int.max) |
| 98 | + if newData.count == 0 { |
| 99 | + pipe.fileHandleForReading.readabilityHandler = nil; |
| 100 | + group.leave() |
| 101 | + } else { |
| 102 | + addData(newData) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// The exit code and output (if redirected) from a subprocess that has |
| 109 | +/// terminated |
| 110 | +public struct ProcessResult { |
| 111 | + public let status: Int32 |
| 112 | + public let stdout: Data |
| 113 | + public let stderr: Data |
| 114 | + |
| 115 | + public var stdoutStr: String? { |
| 116 | + return String(data: stdout, encoding: .utf8) |
| 117 | + } |
| 118 | + public var stderrStr: String? { |
| 119 | + return String(data: stderr, encoding: .utf8) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func run(_ executable: String, _ arguments: String..., workingDirectory: String? = nil) { |
| 124 | + let runner = ProcessRunner( |
| 125 | + launchPath: executable, |
| 126 | + arguments: arguments, |
| 127 | + environment: [:], |
| 128 | + workingDirectory: workingDirectory |
| 129 | + ) |
| 130 | + runner.run() |
| 131 | +} |
| 132 | + |
| 133 | +struct CrossRepoPR { |
| 134 | + let org: String |
| 135 | + let repo: String |
| 136 | + let prNum: String |
| 137 | +} |
| 138 | + |
| 139 | +let crossRepoPrs = [ |
| 140 | + CrossRepoPR(org: "swiftlang", repo: "swift-syntax", prNum: "2859") |
| 141 | +] |
| 142 | + |
| 143 | +for crossRepoPr in crossRepoPrs { |
| 144 | + run( |
| 145 | + "git", |
| 146 | + "clone", |
| 147 | + "https://github.com/\(crossRepoPr.org)/\(crossRepoPrs.repo).git", |
| 148 | + "\(crossRepoPr.repo)", |
| 149 | + workingDirectory: ".." |
| 150 | + ) |
| 151 | + run("git", "fetch", "origin", "pull/\(crossRepoPr.prNum)/merge:pr_merge", workingDirectory: "../swift-syntax") |
| 152 | + run("git", "checkout", "main", workingDirectory: "../swift-syntax") |
| 153 | + run("git", "reset", "--hard", "pr_merge", workingDirectory: "../swift-syntax") |
| 154 | + run( |
| 155 | + "swift", |
| 156 | + "package", |
| 157 | + "config", |
| 158 | + "set-mirror", |
| 159 | + "--package-url", |
| 160 | + "https://github.com/swiftlang/swift-syntax.git", |
| 161 | + "--mirror-url", |
| 162 | + URL(fileURLWithPath: "../swift-syntax").resolveSymlinksInPath().path |
| 163 | + ) |
| 164 | +} |
0 commit comments