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

Drain stdout and stderr concurrently #636

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## Master

- Add Android support [@marcprux][] - [#635](https://github.com/danger/swift/pull/635)
- Drain stdout and stderr concurrently [@jflan-dd] - [#636](https://github.com/danger/swift/pull/636)

## 3.20.2

Expand Down
24 changes: 19 additions & 5 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ extension ShellExecuting {
}

public struct ShellExecutor: ShellExecuting {
/// Queue used to concurrently listen to both stdout and stderr
private let outputQueue = DispatchQueue(label: "ShellExecutor.outputQueue", attributes: .concurrent)

public init() {}

public func execute(_ command: String,
Expand Down Expand Up @@ -98,12 +101,23 @@ public struct ShellExecutor: ShellExecuting {
task.standardError = stderr
try task.run()

// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
let stdoutString = String(data: stdoutData, encoding: .utf8)!
let group = DispatchGroup()

var stdoutString: String!
var stderrData: Data!

outputQueue.async(group: group, qos: .userInitiated) {
// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
stdoutString = String(data: stdoutData, encoding: .utf8)!
}

outputQueue.async(group: group, qos: .userInitiated) {
// Read from STDERR to ensure the `Pipe` does not fill up
stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
}

// Read from STDERR to ensure the `Pipe` does not fill up
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
group.wait()

task.waitUntilExit()

Expand Down