Add $(bash:) . Refactor command.swift.
Had to add leading spaces (shudder) to the documentation comment to get Xcode to render it properly. Hopefully this is just a temporary fix.
This commit is contained in:
+40
-12
@@ -16,24 +16,28 @@ import Foundation
|
||||
|
||||
extension ShellContextType {
|
||||
|
||||
func runLater (shellcommand: String, args: [String]) -> NSTask {
|
||||
func setupTask (executable: String, args: [String]) -> NSTask {
|
||||
let task = NSTask()
|
||||
task.arguments = args
|
||||
task.launchPath = shellcommand
|
||||
task.environment = main.env
|
||||
task.launchPath = executable
|
||||
|
||||
task.environment = self.env
|
||||
task.currentDirectoryPath = self.currentdirectory
|
||||
|
||||
task.standardInput = self.stdin
|
||||
task.standardOutput = self.stdout
|
||||
task.standardError = self.stderror
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
func runLater (bash bashcommand: String) -> NSTask {
|
||||
return runLater("/bin/bash", args: ["-c", bashcommand])
|
||||
func setupTask (bash bashcommand: String) -> NSTask {
|
||||
return setupTask("/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
|
||||
private func outputFrom$ (task: NSTask) -> String {
|
||||
// avoids implicit reading from standardInput
|
||||
task.standardInput = NSPipe ()
|
||||
|
||||
let output = NSPipe ()
|
||||
@@ -41,8 +45,32 @@ extension ShellContextType {
|
||||
task.standardError = output
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
var outputstring = output.fileHandleForReading.read(encoding: self.encoding)
|
||||
|
||||
return output.fileHandleForReading.read(encoding: self.encoding)
|
||||
// if output is single-line, trim it.
|
||||
if outputstring.hasSuffix("\n") && outputstring.characters.indexOf("\n") == outputstring.endIndex.predecessor() {
|
||||
outputstring = outputstring.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
|
||||
}
|
||||
|
||||
return outputstring
|
||||
}
|
||||
|
||||
/**
|
||||
Shortcut for shell command, returns output and errors as a String.
|
||||
|
||||
- parameter args: the arguments, one string for each.
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func $ (executable: String, _ args: String ...) -> String {
|
||||
return outputFrom$(setupTask(executable, args: args))
|
||||
}
|
||||
|
||||
/**
|
||||
Shortcut for bash shell command, returns output and errors as a String.
|
||||
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func $ (bash bashcommand: String) -> String {
|
||||
return outputFrom$(setupTask(bash: bashcommand))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,4 +11,16 @@ import XCTest
|
||||
|
||||
class Command_Tests: XCTestCase {
|
||||
|
||||
func test$BashCommand () {
|
||||
XCTAssertEqual( main.$(bash:"echo one"), "one" )
|
||||
}
|
||||
|
||||
func testSingleline$Command () {
|
||||
XCTAssertEqual( main.$("/bin/echo", "one", "two"), "one two" )
|
||||
}
|
||||
|
||||
func testMultiline$Command () {
|
||||
XCTAssertEqual( main.$("/bin/echo", "one\ntwo"), "one\ntwo\n" )
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ class Context_Tests: XCTestCase {
|
||||
main.currentdirectory = "/tmp"
|
||||
|
||||
XCTAssertEqual( main.currentdirectory, "/private/tmp" )
|
||||
XCTAssertEqual( main.$("/bin/pwd"), "/tmp\n" )
|
||||
XCTAssertEqual( main.$("/bin/pwd"), "/tmp" )
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user