Skip to content

Commit

Permalink
Merge pull request #7 from JohnSundell/linux-fix
Browse files Browse the repository at this point in the history
Fix build errors on Linux
  • Loading branch information
JohnSundell authored Aug 28, 2017
2 parents f5fba68 + 9f17ef0 commit c21f7a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
os: linux
language: generic
sudo: required
dist: trusty
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
script:
- swift test
30 changes: 21 additions & 9 deletions Sources/ShellOut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import Foundation
* - parameter arguments: The arguments to pass to the command
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
* (at the moment this is only supported on macOS)
*
* - returns: The output of running the command
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
Expand All @@ -39,7 +41,9 @@ import Foundation
* - parameter commands: The commands to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
* (at the moment this is only supported on macOS)
*
* - returns: The output of running the command
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
Expand Down Expand Up @@ -315,34 +319,42 @@ private extension Process {
var outputData = Data()
var errorData = Data()

let stdoutHandler: (FileHandle) -> Void = { handler in
let outputPipe = Pipe()
standardOutput = outputPipe

let errorPipe = Pipe()
standardError = errorPipe

#if !os(Linux)
outputPipe.fileHandleForReading.readabilityHandler = { handler in
let data = handler.availableData
outputData.append(data)
outputHandle?.write(data)
}

let stderrHandler: (FileHandle) -> Void = { handler in
errorPipe.fileHandleForReading.readabilityHandler = { handler in
let data = handler.availableData
errorData.append(data)
errorHandle?.write(data)
}
#endif

let outputPipe = Pipe()
standardOutput = outputPipe
outputPipe.fileHandleForReading.readabilityHandler = stdoutHandler
launch()

let errorPipe = Pipe()
standardError = errorPipe
errorPipe.fileHandleForReading.readabilityHandler = stderrHandler
#if os(Linux)
outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
#endif

launch()
waitUntilExit()

outputHandle?.closeFile()
errorHandle?.closeFile()

#if !os(Linux)
outputPipe.fileHandleForReading.readabilityHandler = nil
errorPipe.fileHandleForReading.readabilityHandler = nil
#endif

if terminationStatus != 0 {
throw ShellOutError(
Expand Down
6 changes: 5 additions & 1 deletion Tests/ShellOutTests/ShellOutTests+Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ extension ShellOutTests {
("testWithoutArguments", testWithoutArguments),
("testWithArguments", testWithArguments),
("testWithInlineArguments", testWithInlineArguments),
("testSingleCommandAtPath", testSingleCommandAtPath),
("testSeriesOfCommands", testSeriesOfCommands),
("testSeriesOfCommandsAtPath", testSeriesOfCommandsAtPath),
("testThrowingError", testThrowingError),
("testRedirection", testRedirection)
("testGitCommands", testGitCommands),
("testSwiftPackageManagerCommands", testSwiftPackageManagerCommands)
]
}
#endif

0 comments on commit c21f7a0

Please sign in to comment.