Skip to content

Commit

Permalink
Merge pull request #29 from q231950/launch-improvements
Browse files Browse the repository at this point in the history
Support macOS versions where the launch path will not work any more
  • Loading branch information
q231950 authored Jun 3, 2020
2 parents 61eb135 + cb0c062 commit aa8c5c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Sources/CommandsCore/CommandExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation

public class CommandExecutor {

public init() {}

public
// the outputHandler is called with the output of a command
var outputHandler: ((String) -> ())?
Expand All @@ -21,7 +23,14 @@ public class CommandExecutor {
let process = Process()

public func execute(_ command: Command) {
process.launchPath = command.launchPath

if #available(OSX 10.13, *) {
let url = URL(fileURLWithPath: command.launchPath)
process.executableURL = url
} else {
process.launchPath = command.launchPath
}

process.arguments = command.arguments

let pipe = outputStreamWritingPipe()
Expand All @@ -33,7 +42,16 @@ public class CommandExecutor {
self.terminationHandler?(process.terminationStatus)
}

process.launch()
do {
if #available(OSX 10.13, *) {
try process.run()
} else {
process.launch()
}
} catch let error {
print("\(error.localizedDescription)")
}

process.waitUntilExit()
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/CommandsCore/Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public final class Commands {
}

DispatchQueue.global(qos: .userInitiated).async {
while let x = readLine() {
commandExecutor.write(input: x)
while let input = readLine() {
commandExecutor.write(input: input)
}
}

Expand Down

0 comments on commit aa8c5c7

Please sign in to comment.