Skip to content

Commit 62da383

Browse files
committed
RunOutput(launch:): rename parameter to ‘command’, add documentation.
Removed ```swift group.enter() defer { group.leave() } ``` because no matter what happens, "group.leave()" will be called when exiting this scope. As such it serves no purpose. We still need to use ‘group’ for the asynchronous reading of standard error though.
1 parent b69b6ff commit 62da383

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

Sources/SwiftShell/Command.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,27 @@ public final class RunOutput {
137137
/// The error from running the command, if any.
138138
public let error: CommandError?
139139

140-
init(launch output: AsyncCommand) {
140+
/// Launches command, reads all output from both standard output and standard error simultaneously,
141+
/// and waits until the command is finished.
142+
init(launch command: AsyncCommand) {
141143
var error: CommandError?
142144
var stdout = Data()
143145
var stderror = Data()
144-
let queue = DispatchQueue.global()
145146
let group = DispatchGroup()
146147

147148
do {
148149
// launch and read stdout and stderror.
149150
// see https://github.com/kareman/SwiftShell/issues/52
150-
group.enter()
151-
defer {
152-
group.leave()
153-
}
154-
try output.process.launchThrowably()
151+
try command.process.launchThrowably()
155152

156-
if output.stdout.filehandle.fileDescriptor != output.stderror.filehandle.fileDescriptor {
157-
group.enter()
158-
queue.async {
159-
stderror = output.stderror.readData()
160-
group.leave()
153+
if command.stdout.filehandle.fileDescriptor != command.stderror.filehandle.fileDescriptor {
154+
DispatchQueue.global().async(group: group) {
155+
stderror = command.stderror.readData()
161156
}
162157
}
163158

164-
stdout = output.stdout.readData()
165-
try output.finish()
159+
stdout = command.stdout.readData()
160+
try command.finish()
166161
} catch let commandError as CommandError {
167162
error = commandError
168163
} catch let error {
@@ -173,7 +168,7 @@ public final class RunOutput {
173168

174169
self.rawStdout = stdout
175170
self.rawStderror = stderror
176-
self.output = output
171+
self.output = command
177172
self.error = error
178173
}
179174

@@ -341,7 +336,7 @@ public class PrintedAsyncCommand {
341336
}
342337
}
343338

344-
/** Output from the 'runAsync' methods. */
339+
/** Output from the 'runAsync' commands. */
345340
public final class AsyncCommand: PrintedAsyncCommand {
346341
public let stdout: ReadableStream
347342
public let stderror: ReadableStream

0 commit comments

Comments
 (0)