Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build errors on Linux #7

Merged
merged 7 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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