diff --git a/Sources/DangerShellExecutor/ShellExecutor.swift b/Sources/DangerShellExecutor/ShellExecutor.swift index 45230260..e1fcad6d 100644 --- a/Sources/DangerShellExecutor/ShellExecutor.swift +++ b/Sources/DangerShellExecutor/ShellExecutor.swift @@ -1,4 +1,7 @@ import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif public enum SpawnError: Error { case commandFailed(command: String, exitCode: Int32, stdout: String, stderr: String) @@ -62,16 +65,19 @@ public struct ShellExecutor: ShellExecuting { with: arguments, environmentVariables: environmentVariables, outputFile: outputFile) + do { + let pipe = Pipe() + task.standardOutput = pipe + try task.run() - let pipe = Pipe() - task.standardOutput = pipe - task.launch() + let data = pipe.fileHandleForReading.readDataToEndOfFile() - let data = pipe.fileHandleForReading.readDataToEndOfFile() + task.waitUntilExit() - task.waitUntilExit() - - return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines) + return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines) + } catch { + return error.localizedDescription + } } // Similar to above, but can throw, and throws with most of @@ -90,7 +96,7 @@ public struct ShellExecutor: ShellExecuting { task.standardOutput = stdout let stderr = Pipe() task.standardError = stderr - task.launch() + try task.run() // Pull out the STDOUT as a string because we'll need that regardless let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile() @@ -131,10 +137,10 @@ public struct ShellExecutor: ShellExecuting { let script = "\(command) \(arguments.joined(separator: " "))" + scriptOutputFile let task = Process() - task.launchPath = "/bin/sh" + task.executableURL = URL(fileURLWithPath: "/bin/sh") task.arguments = ["-c", script] task.environment = mergeEnvs(localEnv: environmentVariables, processEnv: ProcessInfo.processInfo.environment) - task.currentDirectoryPath = FileManager.default.currentDirectoryPath + task.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath) return task } diff --git a/Sources/Runner/Commands/RunDangerJS.swift b/Sources/Runner/Commands/RunDangerJS.swift index 1d9df3aa..8058764a 100644 --- a/Sources/Runner/Commands/RunDangerJS.swift +++ b/Sources/Runner/Commands/RunDangerJS.swift @@ -1,6 +1,9 @@ import Foundation import Logger import RunnerLib +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif func runDangerJSCommandToRunDangerSwift(_ command: DangerCommand, logger: Logger) throws -> Int32 { guard let dangerJS = try? getDangerCommandPath(logger: logger) else { @@ -22,7 +25,7 @@ func runDangerJSCommandToRunDangerSwift(_ command: DangerCommand, logger: Logger let proc = Process() proc.environment = ProcessInfo.processInfo.environment - proc.launchPath = dangerJS + proc.executableURL = URL(fileURLWithPath: dangerJS) let dangerOptionsIndexes = DangerSwiftOption.allCases .compactMap { option -> (DangerSwiftOption, Int)? in @@ -62,8 +65,8 @@ func runDangerJSCommandToRunDangerSwift(_ command: DangerCommand, logger: Logger proc.standardOutput = standardOutput proc.standardError = standardOutput - logger.debug("Running: \(proc.launchPath!) \(proc.arguments!.joined(separator: " ")) ") - proc.launch() + logger.debug("Running: \(proc.executableURL!) \(proc.arguments!.joined(separator: " ")) ") + try proc.run() proc.waitUntilExit() return proc.terminationStatus diff --git a/Sources/Runner/Commands/Runner.swift b/Sources/Runner/Commands/Runner.swift index cd14ef65..3bdcce6f 100644 --- a/Sources/Runner/Commands/Runner.swift +++ b/Sources/Runner/Commands/Runner.swift @@ -3,6 +3,9 @@ import DangerShellExecutor import Foundation import Logger import RunnerLib +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif // swiftlint:disable:next function_body_length func runDanger(version dangerSwiftVersion: String, logger: Logger) throws { @@ -152,19 +155,19 @@ func runDanger(version dangerSwiftVersion: String, logger: Logger) throws { // Create a process to eval the Swift file let proc = Process() - proc.launchPath = swiftC + proc.executableURL = URL(fileURLWithPath: swiftC) proc.arguments = args let standardOutput = FileHandle.standardOutput if let cwdOptionIndex = CommandLine.arguments.firstIndex(of: DangeSwiftRunnerOption.cwd.rawValue), (cwdOptionIndex + 1) < CommandLine.arguments.count, let directoryURL = URL(string: CommandLine.arguments[cwdOptionIndex + 1]) { - proc.currentDirectoryPath = directoryURL.absoluteString + proc.currentDirectoryURL = directoryURL } proc.standardOutput = standardOutput proc.standardError = standardOutput - proc.launch() + try proc.run() proc.waitUntilExit() logger.debug("Completed evaluation")