0001    import Foundation
0002    
0003    /// The result of a task execution
0004    public typealias ChoreResult
ChoreTask.swift:10
private func chore_task(command: String, _ arguments: [String] = [String](), stdin: String = "") -> ChoreResult {
ChoreTask.swift:70
public prefix func > (command: String) -> ChoreResult {
ChoreTask.swift:80
public prefix func > (command: [String]) -> ChoreResult {
ChoreTask.swift:102
public func | (left: ChoreResult, right: String) -> ChoreResult {
ChoreTask.swift:102
public func | (left: ChoreResult, right: String) -> ChoreResult {
ChoreTask.swift:113
public func | (left: ChoreResult, right: [String]) -> ChoreResult {
ChoreTask.swift:113
public func | (left: ChoreResult, right: [String]) -> ChoreResult {
ChoreTask.swift:129
public func | (left: ChoreResult, right: ((String) -> String)) -> ChoreResult {
ChoreTask.swift:129
public func | (left: ChoreResult, right: ((String) -> String)) -> ChoreResult {
ChoreTask.swift:144
public func | (left: (() -> String), right: String) -> ChoreResult {
ChoreTask.swift:155
public func | (left: (() -> String), right: [String]) -> ChoreResult {
ChoreTask.swift:166
public func | (left: String, right: String) -> ChoreResult {
ChoreTask.swift:177
public func | (left: String, right: [String]) -> ChoreResult {
= (result: Int32, stdout: String, stderr: String) 0005 0006 private func string_trim
ChoreTask.swift:56
    let stderr = string_trim(NSString(data: stderrHandle.readDataToEndOfFile(), encoding: NSUTF8StringEncoding)) ?? ""
ChoreTask.swift:57
    let stdout = string_trim(NSString(data: stdoutHandle.readDataToEndOfFile(), encoding: NSUTF8StringEncoding)) ?? ""
(string: NSString!) -> String { 0007 return string.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet()) ?? "" 0008 } 0009 0010 private func chore_task
ChoreTask.swift:17
        task.launchPath = (chore_task("/usr/bin/which", [task.launchPath!])).stdout
ChoreTask.swift:71
    return chore_task(command)
ChoreTask.swift:85
            return chore_task(command[0])
ChoreTask.swift:90
    return chore_task(command[0], Array(command[1..<command.count]))
ChoreTask.swift:119
    return chore_task(right[0], arguments, stdin: left.stdout)
(command: String, _ arguments: [String] = [String](), stdin: String = "") -> ChoreResult { 0011 let task = NSTask() 0012 0013 task.launchPath = command 0014 task.arguments = arguments 0015 0016 if !(task.launchPath! as NSString).absolutePath { 0017 task.launchPath = (chore_task("/usr/bin/which", [task.launchPath!])).stdout 0018 } 0019 0020 var isDirectory: ObjCBool = false 0021 0022 if !NSFileManager.defaultManager().fileExistsAtPath(task.launchPath!, isDirectory: &isDirectory) { 0023 return (255, "", String(format: "%@: launch path not accessible", task.launchPath!)) 0024 } 0025 0026 if (isDirectory) { 0027 return (255, "", String(format: "%@: launch path is a directory", task.launchPath!)) 0028 } 0029 0030 if !NSFileManager.defaultManager().isExecutableFileAtPath(task.launchPath!) { 0031 return (255, "", String(format: "%@: launch path not executable", task.launchPath!)) 0032 } 0033 0034 if stdin.characters.count > 0 { 0035 let stdinPipe = NSPipe() 0036 task.standardInput = stdinPipe 0037 let stdinHandle = stdinPipe.fileHandleForWriting 0038 0039 if let data = stdin.dataUsingEncoding(NSUTF8StringEncoding) { 0040 stdinHandle.writeData(data) 0041 stdinHandle.closeFile() 0042 } 0043 } 0044 0045 let stderrPipe = NSPipe() 0046 task.standardError = stderrPipe 0047 let stderrHandle = stderrPipe.fileHandleForReading 0048 0049 let stdoutPipe = NSPipe() 0050 task.standardOutput = stdoutPipe 0051 let stdoutHandle = stdoutPipe.fileHandleForReading 0052 0053 task.launch() 0054 task.waitUntilExit() 0055 0056 let stderr = string_trim(NSString(data: stderrHandle.readDataToEndOfFile(), encoding: NSUTF8StringEncoding)) ?? "" 0057 let stdout = string_trim(NSString(data: stdoutHandle.readDataToEndOfFile(), encoding: NSUTF8StringEncoding)) ?? "" 0058 0059 return (task.terminationStatus, stdout, stderr) 0060 } 0061 0062 prefix operator > {} 0063 0064 /** 0065 Executes a command. 0066 0067 :param: command The command to execute. 0068 :returns: A tuple containing the exit code, stdout and stderr output. 0069 */ 0070 public prefix func > (command: String) -> ChoreResult { 0071 return chore_task(command) 0072 } 0073 0074 /** 0075 Executes a command with arguments. 0076 0077 :param: command The command to execute and its arguments. 0078 :returns: A tuple containing the exit code, stdout and stderr output. 0079 */ 0080 public prefix func > (command: [String]) -> ChoreResult { 0081 switch command.count { 0082 case 0: 0083 return (0, "", "") 0084 case 1: 0085 return chore_task(command[0]) 0086 default: 0087 break 0088 } 0089 0090 return chore_task(command[0], Array(command[1..<command.count])) 0091 } 0092 0093 infix operator | {} 0094 0095 /** 0096 Executes a command with standard input from another command. 0097 0098 :param: left The result of a previous command. 0099 :param: right The command to execute. 0100 :returns: A tuple containing the exit code, stdout and stderr output. 0101 */ 0102 public func | (left: ChoreResult, right: String) -> ChoreResult { 0103 return left|[right] 0104 } 0105 0106 /** 0107 Executes a command with standard input from another command. 0108 0109 :param: left The result of a previous command. 0110 :param: right The command to execute and its arguments. 0111 :returns: A tuple containing the exit code, stdout and stderr output. 0112 */ 0113 public func | (left: ChoreResult, right: [String]) -> ChoreResult { 0114 if left.result != 0 { 0115 return left 0116 } 0117 0118 let arguments = right.count >= 2 ? Array(right[1..<right.count]) : [String]() 0119 return chore_task(right[0], arguments, stdin: left.stdout) 0120 } 0121 0122 /** 0123 Executes a closure with input from a previous command. 0124 0125 :param: left The result of a previous command. 0126 :param: right The closure to execute. 0127 :returns: A tuple containing the exit code, stdout and stderr output. 0128 */ 0129 public func | (left: ChoreResult, right: ((String) -> String)) -> ChoreResult { 0130 if left.result != 0 { 0131 return left 0132 } 0133 0134 return (0, right(left.stdout), "") 0135 } 0136 0137 /** 0138 Executes a command with input from a closure. 0139 0140 :param: left The closure to execute. 0141 :param: right The command to execute. 0142 :returns: A tuple containing the exit code, stdout and stderr output. 0143 */ 0144 public func | (left: (() -> String), right: String) -> ChoreResult { 0145 return (0, left(), "")|right 0146 } 0147 0148 /** 0149 Executes a command with input from a closure. 0150 0151 :param: left The closure to execute. 0152 :param: right The command to execute and its arguments. 0153 :returns: A tuple containing the exit code, stdout and stderr output. 0154 */ 0155 public func | (left: (() -> String), right: [String]) -> ChoreResult { 0156 return (0, left(), "")|right 0157 } 0158 0159 /** 0160 Executes a command with input from a string. 0161 0162 :param: left The string to use a stdin. 0163 :param: right The command to execute. 0164 :returns: A tuple containing the exit code, stdout and stderr output. 0165 */ 0166 public func | (left: String, right: String) -> ChoreResult { 0167 return (0, left, "")|right 0168 } 0169 0170 /** 0171 Executes a command with input from a string. 0172 0173 :param: left The string to use a stdin. 0174 :param: right The command to execute and its arguments. 0175 :returns: A tuple containing the exit code, stdout and stderr output. 0176 */ 0177 public func | (left: String, right: [String]) -> ChoreResult { 0178 return (0, left, "")|right 0179 } 0180