-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor utilities under Foundation.Process and CartonHelpers.Process…
… and share implementations (#477) * organizer process utilities * resurrect ProcessResult.checkNonZeroExit * import Foundation * use SIGINT * remove didExit and tweak bundle completion message
- Loading branch information
Showing
11 changed files
with
139 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
struct CartonCoreError: Error & CustomStringConvertible { | ||
init(_ description: String) { | ||
self.description = description | ||
} | ||
var description: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Foundation | ||
|
||
extension Foundation.Process { | ||
// Monitor termination/interrruption signals to forward them to child process | ||
public func setSignalForwarding(_ signalNo: Int32) { | ||
signal(signalNo, SIG_IGN) | ||
let signalSource = DispatchSource.makeSignalSource(signal: signalNo) | ||
signalSource.setEventHandler { [self] in | ||
signalSource.cancel() | ||
kill(processIdentifier, signalNo) | ||
} | ||
signalSource.resume() | ||
} | ||
|
||
public func forwardTerminationSignals() { | ||
setSignalForwarding(SIGINT) | ||
setSignalForwarding(SIGTERM) | ||
} | ||
|
||
public var commandLine: String { | ||
get throws { | ||
guard let executableURL else { | ||
throw CartonCoreError("executableURL is none") | ||
} | ||
|
||
let commandLineArgs: [String] = [ | ||
executableURL.path | ||
] + (arguments ?? []) | ||
|
||
let q = "\"" | ||
let commandLine: String = commandLineArgs | ||
.map { "\(q)\($0)\(q)" } | ||
.joined(separator: " ") | ||
|
||
return commandLine | ||
} | ||
} | ||
|
||
public func checkRun( | ||
printsLoadingMessage: Bool, | ||
forwardExit: Bool = false | ||
) throws { | ||
if printsLoadingMessage { | ||
print("Running \(try commandLine)") | ||
} | ||
|
||
try run() | ||
forwardTerminationSignals() | ||
waitUntilExit() | ||
|
||
if forwardExit { | ||
self.forwardExit() | ||
} | ||
|
||
try checkNonZeroExit() | ||
} | ||
|
||
public func forwardExit() { | ||
exit(terminationStatus) | ||
} | ||
|
||
public func checkNonZeroExit() throws { | ||
if terminationStatus != 0 { | ||
throw CartonCoreError( | ||
"Process failed with status \(terminationStatus).\n" + | ||
"Command line: \(try commandLine)" | ||
) | ||
} | ||
} | ||
|
||
public static func checkRun( | ||
_ executableURL: URL, | ||
arguments: [String], | ||
printsLoadingMessage: Bool = true, | ||
forwardExit: Bool = false | ||
) throws { | ||
let process = Foundation.Process() | ||
process.executableURL = executableURL | ||
process.arguments = arguments | ||
try process.checkRun( | ||
printsLoadingMessage: printsLoadingMessage, | ||
forwardExit: forwardExit | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters