Skip to content

Commit c21f7a0

Browse files
authored
Merge pull request #7 from JohnSundell/linux-fix
Fix build errors on Linux
2 parents f5fba68 + 9f17ef0 commit c21f7a0

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
os: linux
2+
language: generic
3+
sudo: required
4+
dist: trusty
5+
install:
6+
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
7+
script:
8+
- swift test

Sources/ShellOut.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import Foundation
1515
* - parameter arguments: The arguments to pass to the command
1616
* - parameter path: The path to execute the commands at (defaults to current folder)
1717
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
18+
* (at the moment this is only supported on macOS)
1819
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
20+
* (at the moment this is only supported on macOS)
1921
*
2022
* - returns: The output of running the command
2123
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -39,7 +41,9 @@ import Foundation
3941
* - parameter commands: The commands to run
4042
* - parameter path: The path to execute the commands at (defaults to current folder)
4143
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
44+
* (at the moment this is only supported on macOS)
4245
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
46+
* (at the moment this is only supported on macOS)
4347
*
4448
* - returns: The output of running the command
4549
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -315,34 +319,42 @@ private extension Process {
315319
var outputData = Data()
316320
var errorData = Data()
317321

318-
let stdoutHandler: (FileHandle) -> Void = { handler in
322+
let outputPipe = Pipe()
323+
standardOutput = outputPipe
324+
325+
let errorPipe = Pipe()
326+
standardError = errorPipe
327+
328+
#if !os(Linux)
329+
outputPipe.fileHandleForReading.readabilityHandler = { handler in
319330
let data = handler.availableData
320331
outputData.append(data)
321332
outputHandle?.write(data)
322333
}
323334

324-
let stderrHandler: (FileHandle) -> Void = { handler in
335+
errorPipe.fileHandleForReading.readabilityHandler = { handler in
325336
let data = handler.availableData
326337
errorData.append(data)
327338
errorHandle?.write(data)
328339
}
340+
#endif
329341

330-
let outputPipe = Pipe()
331-
standardOutput = outputPipe
332-
outputPipe.fileHandleForReading.readabilityHandler = stdoutHandler
342+
launch()
333343

334-
let errorPipe = Pipe()
335-
standardError = errorPipe
336-
errorPipe.fileHandleForReading.readabilityHandler = stderrHandler
344+
#if os(Linux)
345+
outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
346+
errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
347+
#endif
337348

338-
launch()
339349
waitUntilExit()
340350

341351
outputHandle?.closeFile()
342352
errorHandle?.closeFile()
343353

354+
#if !os(Linux)
344355
outputPipe.fileHandleForReading.readabilityHandler = nil
345356
errorPipe.fileHandleForReading.readabilityHandler = nil
357+
#endif
346358

347359
if terminationStatus != 0 {
348360
throw ShellOutError(

Tests/ShellOutTests/ShellOutTests+Linux.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ extension ShellOutTests {
1313
("testWithoutArguments", testWithoutArguments),
1414
("testWithArguments", testWithArguments),
1515
("testWithInlineArguments", testWithInlineArguments),
16+
("testSingleCommandAtPath", testSingleCommandAtPath),
17+
("testSeriesOfCommands", testSeriesOfCommands),
18+
("testSeriesOfCommandsAtPath", testSeriesOfCommandsAtPath),
1619
("testThrowingError", testThrowingError),
17-
("testRedirection", testRedirection)
20+
("testGitCommands", testGitCommands),
21+
("testSwiftPackageManagerCommands", testSwiftPackageManagerCommands)
1822
]
1923
}
2024
#endif

0 commit comments

Comments
 (0)