-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from zenangst/various/improvements
Various improvements
- Loading branch information
Showing
6 changed files
with
65 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,29 @@ | ||
import Foundation | ||
|
||
class Shell { | ||
let queue = DispatchQueue(label: "shell-execution") | ||
|
||
@discardableResult func execute(command: String, | ||
arguments: [String] = [], | ||
at path: String = ".") throws -> String { | ||
at path: String = ".") -> String { | ||
let process = Process() | ||
let path = path.replacingOccurrences(of: " ", with: "\\ ") | ||
let arguments = arguments.joined(separator: " ") | ||
let command = "cd \(path) && \(command) \(arguments)" | ||
|
||
return try launch(process, command: command) | ||
let command = "cd \(path) && \(command) \(arguments) &" | ||
return process.shell(command: command) | ||
} | ||
} | ||
|
||
private func launch(_ process: Process, | ||
command: String, | ||
withShell: String = "/bin/bash") throws -> String { | ||
let pipe = Pipe() | ||
var output = Data() | ||
|
||
pipe.fileHandleForReading.readabilityHandler = { [weak self] handler in | ||
guard let strongSelf = self else { return } | ||
strongSelf.queue.async { | ||
let data = handler.availableData | ||
output.append(data) | ||
} | ||
} | ||
|
||
process.launchPath = withShell | ||
process.arguments = ["-c", command] | ||
process.standardOutput = pipe | ||
process.launch() | ||
process.waitUntilExit() | ||
extension Process { | ||
public func shell(command: String) -> String { | ||
let pipe = Pipe() | ||
|
||
return queue.sync { | ||
if let output = String(data: output, encoding: .utf8), output.hasSuffix("\n") { | ||
let endIndex = output.index(before: output.endIndex) | ||
return String(output[..<endIndex]) | ||
} | ||
launchPath = "/bin/bash" | ||
arguments = ["-c", command] | ||
standardOutput = pipe | ||
|
||
return "" | ||
} | ||
launch() | ||
waitUntilExit() | ||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
return String(data: data, encoding: String.Encoding.utf8) ?? "" | ||
} | ||
} |