Skip to content

Commit

Permalink
Merge pull request #634 from 417-72KI/fix-deprecated
Browse files Browse the repository at this point in the history
Replace deprecated properties and functions in `Process`
  • Loading branch information
417-72KI authored Dec 24, 2024
2 parents 808ad84 + 4f78b76 commit b41494a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
26 changes: 16 additions & 10 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 6 additions & 3 deletions Sources/Runner/Commands/RunDangerJS.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions Sources/Runner/Commands/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit b41494a

Please sign in to comment.