From 1f2b7e074d58aafa752fb414ef555ddb4a9d9f6f Mon Sep 17 00:00:00 2001 From: Kent Hinson Date: Mon, 25 Apr 2016 19:29:27 -0700 Subject: [PATCH 1/2] Added stop func to stop the task early. added async callbacks for ReadableStream.onOutput and AsyncShellTask.onCompletion allows for getting callbacks with data as the AsyncShellTask is running. --- Sources/SwiftShell/Command.swift | 14 ++++++++++++++ Sources/SwiftShell/Stream.swift | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftShell/Command.swift b/Sources/SwiftShell/Command.swift index 450809e..bca66be 100644 --- a/Sources/SwiftShell/Command.swift +++ b/Sources/SwiftShell/Command.swift @@ -242,6 +242,11 @@ public final class AsyncShellTask { exit(error, file: file, line: line) } } + + /** Terminate task early. */ + public func stop(){ + task.terminate() + } /** Wait for this shell task to finish. @@ -261,6 +266,15 @@ public final class AsyncShellTask { } } +extension AsyncShellTask { + public func onCompletion ( handler: ((AsyncShellTask) -> ())? ) -> AsyncShellTask { + task.terminationHandler = { (NSTask) in + handler?(self) + } + return self + } +} + extension ShellRunnable { /** diff --git a/Sources/SwiftShell/Stream.swift b/Sources/SwiftShell/Stream.swift index e2a3b77..103c735 100644 --- a/Sources/SwiftShell/Stream.swift +++ b/Sources/SwiftShell/Stream.swift @@ -46,11 +46,24 @@ public final class ReadableStream : Streamable { /** Let ReadableStream run commands using itself as stdin. */ extension ReadableStream: ShellRunnable { - public var shellcontext: ShellContextType { - var context = ShellContext(main) - context.stdin = self - return context - } + public var shellcontext: ShellContextType { + var context = ShellContext(main) + context.stdin = self + return context + } +} + +/** Callback with when standardout has data.*/ +extension ReadableStream { + public func onOutput ( handler: ((String) -> ())? ) { + if let h = handler{ + filehandle.readabilityHandler = {(NSFileHandle) in + if let output = self.readSome(){ + h(output) + } + } + } + } } /** An output stream, like standard output or a writeable file. */ From 34e7e06a418fd5cb2697e108ed3baf2ad56660e6 Mon Sep 17 00:00:00 2001 From: Kent Hinson Date: Mon, 25 Apr 2016 19:34:51 -0700 Subject: [PATCH 2/2] Fixed extension comment. --- Sources/SwiftShell/Stream.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftShell/Stream.swift b/Sources/SwiftShell/Stream.swift index 103c735..f3675a5 100644 --- a/Sources/SwiftShell/Stream.swift +++ b/Sources/SwiftShell/Stream.swift @@ -53,7 +53,7 @@ extension ReadableStream: ShellRunnable { } } -/** Callback with when standardout has data.*/ +/** Callback with when ReadableStream has data.*/ extension ReadableStream { public func onOutput ( handler: ((String) -> ())? ) { if let h = handler{