From 7dffa08fbf7cbea10d6b914b68af514c8e763bfe Mon Sep 17 00:00:00 2001 From: Andrew Mu Date: Wed, 27 Feb 2019 21:54:55 -0800 Subject: [PATCH] Stream shell output make it so that the terminal output window `Tools > Terminal Output` will receive streams of messages from scripts that are running resolves https://github.com/muandrew/androidtool-mac/issues/1 --- AndroidTool/ShellTasker.swift | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/AndroidTool/ShellTasker.swift b/AndroidTool/ShellTasker.swift index 2818027..fbb2ed8 100644 --- a/AndroidTool/ShellTasker.swift +++ b/AndroidTool/ShellTasker.swift @@ -37,17 +37,10 @@ class ShellTasker: NSObject { isUserScript:Bool = false, isIOS:Bool = false, complete:@escaping (_ output:NSString)-> Void) { - var output = NSString() - var data = Data() - - var scriptPath:String - - if isUserScript { - scriptPath = scriptFile - } else { - scriptPath = Bundle.main.path(forResource: scriptFile, ofType: "sh")! - } + let scriptPath = isUserScript + ? scriptFile + : Bundle.main.path(forResource: scriptFile, ofType: "sh")! let resourcesPath = Bundle.main.resourcePath! task = Process() @@ -96,8 +89,8 @@ class ShellTasker: NSObject { queue: nil) { (notification) -> Void in DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: { () -> Void in - data = pipe.fileHandleForReading.readDataToEndOfFile() // use .availabledata instead to stream from the console, pretty cool - output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! + let data = pipe.fileHandleForReading.availableData + let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! DispatchQueue.main.async(execute: { () -> Void in self.postNotification(output, channel: self.notificationChannel()) complete(output)