From e7fb2de10b9c2327decb354d22eb03e604ec967c Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Thu, 28 Aug 2014 21:04:28 +0200 Subject: [PATCH] Added documentation. --- SwiftShell/Command.swift | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/SwiftShell/Command.swift b/SwiftShell/Command.swift index b8052a5..bf44288 100644 --- a/SwiftShell/Command.swift +++ b/SwiftShell/Command.swift @@ -10,25 +10,42 @@ import Foundation private func newtask (shellcommand: String) -> NSTask { let task = NSTask() - task.arguments = ["-c",shellcommand] + task.arguments = ["-c", shellcommand] task.launchPath = "/bin/bash" - // avoids implicit reading of the main script's standardInput - task.standardInput = NSPipe () - return task } +/** +Run a shellcommand synchronously with no standard input. + +:returns: Standard output +*/ public func run (shellcommand: String) -> ReadableStreamType { let task = newtask(shellcommand) + + // avoids implicit reading of the main script's standardInput + task.standardInput = NSPipe () let output = NSPipe () task.standardOutput = output task.launch() + + // necessary for now to ensure one shellcommand is finished before another begins. + // uncontrolled asynchronous shell processes could be messy. + // but shell commands on the same line connected with the pipe operator should preferably be asynchronous. task.waitUntilExit() + return output.fileHandleForReading } +/** +Run a shell command synchronously. + +:param: input Standard input. + +:returns: Standard output +*/ public func run (shellcommand: String)(input: ReadableStreamType) -> ReadableStreamType { let task = newtask(shellcommand) task.standardInput = input as File @@ -37,5 +54,6 @@ public func run (shellcommand: String)(input: ReadableStreamType) -> ReadableStr task.standardOutput = output task.launch() task.waitUntilExit() + return output.fileHandleForReading }