Add $ method to ShellContextType.

This commit is contained in:
Kare Morstol
2015-08-05 21:10:00 +02:00
parent ee48f4c5a2
commit 6f967c0683
+32 -7
View File
@@ -7,17 +7,42 @@
import Foundation
public func runLater (shellcommand: String) -> NSTask {
let task = NSTask()
task.arguments = ["-c", shellcommand]
task.launchPath = "/bin/bash"
return task
}
/** Print message to standard error and halt execution. */
@noreturn public func printErrorAndExit <T> (errormessage: T, errorcode: Int32 = EXIT_FAILURE) {
main.stderror.writeln("SwiftShell: \(errormessage)")
exit(errorcode)
}
extension ShellContextType {
func runLater (shellcommand: String, args: [String]) -> NSTask {
let task = NSTask()
task.arguments = args
task.launchPath = shellcommand
task.environment = main.env
return task
}
func runLater (bash bashcommand: String) -> NSTask {
return runLater("/bin/bash", args: ["-c", bashcommand])
}
/** Shortcut for in-line command, returns output as String. */
public func $ (shellcommand: String, args: String ...) -> String {
let task = runLater(shellcommand, args: args)
// avoids implicit reading of the main script's standardInput
task.standardInput = NSPipe ()
let output = NSPipe ()
task.standardOutput = output
task.standardError = output
task.launch()
task.waitUntilExit()
return output.fileHandleForReading.read(encoding: self.encoding)
}
}