From b7b1ea041272748bd14f3c1e3ab6527f2730f06e Mon Sep 17 00:00:00 2001 From: NanoSector Date: Sat, 13 Mar 2021 19:46:42 +0100 Subject: [PATCH] Force shell commands onto a background thread; fixes #2 --- Brewed/Helpers/Shell.swift | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Brewed/Helpers/Shell.swift b/Brewed/Helpers/Shell.swift index e6eea1e..550a4a4 100644 --- a/Brewed/Helpers/Shell.swift +++ b/Brewed/Helpers/Shell.swift @@ -10,32 +10,36 @@ import PromiseKit struct Shell { typealias processResult = (exitCode: Int32, stdout: String, stderr: String) - + static func exec(_ command: String) -> Promise { Promise { seal in let task = Process() let stdout = Pipe() let stderr = Pipe() - + task.standardOutput = stdout task.standardError = stderr task.arguments = ["-c", command] task.launchPath = "/bin/zsh" - task.launch() - task.waitUntilExit() - - guard task.terminationStatus == 0 else { - throw ProcessError.NonZeroExitCode(stderr: stderr.toString()) + + DispatchQueue.global(qos: .background).async { + task.launch() + task.waitUntilExit() + + guard task.terminationStatus == 0 else { + seal.reject(ProcessError.NonZeroExitCode(stderr: stderr.toString())) + return + } + + seal.fulfill((task.terminationStatus, stdout.toString(), stderr.toString())) } - - seal.fulfill((task.terminationStatus, stdout.toString(), stderr.toString())) } } } protocol ShellCommandWrapper { associatedtype resultType - + func exec() -> Promise }