Skip to content

Commit 5f02e6f

Browse files
committed
Drain stdout and stderr concurrently
1 parent e035543 commit 5f02e6f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Sources/DangerShellExecutor/ShellExecutor.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extension ShellExecuting {
5454
}
5555

5656
public struct ShellExecutor: ShellExecuting {
57+
/// Queue used to concurrently listen to both stdout and stderr
58+
private let outputQueue = DispatchQueue(label: "ShellExecutor.outputQueue", attributes: .concurrent)
59+
5760
public init() {}
5861

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

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

105-
// Read from STDERR to ensure the `Pipe` does not fill up
106-
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
106+
var stdoutString: String!
107+
var stderrData: Data!
108+
109+
outputQueue.async(group: group, qos: .userInitiated) {
110+
// Pull out the STDOUT as a string because we'll need that regardless
111+
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
112+
stdoutString = String(data: stdoutData, encoding: .utf8)!
113+
}
114+
115+
outputQueue.async(group: group, qos: .userInitiated) {
116+
// Read from STDERR to ensure the `Pipe` does not fill up
117+
stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
118+
}
119+
120+
group.wait()
107121

108122
task.waitUntilExit()
109123

0 commit comments

Comments
 (0)