Skip to content

Commit

Permalink
Update Shell interface
Browse files Browse the repository at this point in the history
  • Loading branch information
raptorxcz committed Aug 9, 2024
1 parent a354d01 commit 91e6101
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Sources/SwiftTools/Common/Shell/Platform/ShellService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import XcbeautifyLib

public protocol ShellService {
func execute(arguments: [String]) throws
func executeWithVisibleOutput(arguments: [String]) throws
func executeWithResult(arguments: [String]) throws -> String
func executeWithXCBeautify(arguments: [String]) throws
}
Expand All @@ -24,17 +25,21 @@ final class ShellServiceImpl: ShellService {
}

func execute(arguments: [String]) throws {
try executeTask(arguments: arguments)
try executeTask(arguments: arguments, isOutputVisible: verboseController.isVerbose())
}

func executeWithVisibleOutput(arguments: [String]) throws {
try executeTask(arguments: arguments, isOutputVisible: true)
}

func executeWithResult(arguments: [String]) throws -> String {
return try executeTask(arguments: arguments)
return try executeTask(arguments: arguments, isOutputVisible: verboseController.isVerbose())
}

@discardableResult private func executeTask(arguments: [String]) throws -> String {
@discardableResult private func executeTask(arguments: [String], isOutputVisible: Bool) throws -> String {
let output = CaptureStream()
let error = CaptureStream()
let outputStream = makeOutputStream(captureStream: output)
let outputStream = makeOutputStream(captureStream: output, isOutputVisible: isOutputVisible)

let command = arguments.joined(separator: " ")
let task = Task(executable: "/bin/bash", arguments: ["-c", command], stdout: outputStream, stderr: error)
Expand All @@ -53,8 +58,8 @@ final class ShellServiceImpl: ShellService {
return outputString
}

private func makeOutputStream(captureStream: CaptureStream) -> WritableStream {
if verboseController.isVerbose() {
private func makeOutputStream(captureStream: CaptureStream, isOutputVisible: Bool) -> WritableStream {
if isOutputVisible {
return SplitStream(streams: [captureStream, WriteStream.stdout])
} else {
return captureStream
Expand Down

0 comments on commit 91e6101

Please sign in to comment.