From 553fa09d340588bc437e86ebe0a4853a8673f203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Tue, 13 Mar 2018 00:22:06 +0100 Subject: [PATCH 1/2] Added URL API to Foundation.Process --- Foundation/Process.swift | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/Foundation/Process.swift b/Foundation/Process.swift index 849bfd90d1..2f7a104cca 100644 --- a/Foundation/Process.swift +++ b/Foundation/Process.swift @@ -114,6 +114,7 @@ open class Process: NSObject { static var done = false static let lock = NSLock() } + Once.lock.synchronized { if !Once.done { let thread = Thread { @@ -164,26 +165,52 @@ open class Process: NSObject { } - // these methods can only be set before a launch + // These methods can only be set before a launch. + open var launchPath: String? open var arguments: [String]? open var environment: [String : String]? // if not set, use current open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath - // standard I/O channels; could be either a FileHandle or a Pipe + open var executableURL: URL? { + get { + guard let launchPath = self.launchPath else { + return nil + } + + return URL(fileURLWithPath: launchPath) + } + set { + self.launchPath = newValue?.path + } + } + + open var currentDirectoryURL: URL { + get { + return URL(fileURLWithPath: self.currentDirectoryPath) + } + set { + self.currentDirectoryPath = newValue.path + } + } + + // Standard I/O channels; could be either a FileHandle or a Pipe + open var standardInput: Any? { willSet { precondition(newValue is Pipe || newValue is FileHandle, "standardInput must be either Pipe or FileHandle") } } + open var standardOutput: Any? { willSet { precondition(newValue is Pipe || newValue is FileHandle, "standardOutput must be either Pipe or FileHandle") } } + open var standardError: Any? { willSet { precondition(newValue is Pipe || newValue is FileHandle, @@ -198,7 +225,8 @@ open class Process: NSObject { private var processLaunchedCondition = NSCondition() - // actions + // Actions + open func launch() { self.processLaunchedCondition.lock() From c76e64e186468608c888c729422d955e722cb2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Tue, 13 Mar 2018 00:29:23 +0100 Subject: [PATCH 2/2] Added test for Process URL API --- TestFoundation/TestProcess.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/TestFoundation/TestProcess.swift b/TestFoundation/TestProcess.swift index 8ebc3cef00..eb7c10f097 100644 --- a/TestFoundation/TestProcess.swift +++ b/TestFoundation/TestProcess.swift @@ -45,11 +45,15 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" - process.arguments = ["-c", "exit 0"] + let executablePath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: executablePath) + + XCTAssertEqual(executablePath, process.launchPath) + process.arguments = ["-c", "exit 0"] process.launch() process.waitUntilExit() + XCTAssertEqual(process.terminationStatus, 0) XCTAssertEqual(process.terminationReason, .exit) }