|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import SwiftCLI
|
| 9 | +import XcbeautifyLib |
9 | 10 |
|
10 | 11 | public protocol ShellService {
|
11 | 12 | func execute(arguments: [String]) throws
|
12 | 13 | func executeWithResult(arguments: [String]) throws -> String
|
| 14 | + func executeWithXCBeautify(arguments: [String]) throws |
13 | 15 | }
|
14 | 16 |
|
15 | 17 | final class ShellServiceImpl: ShellService {
|
@@ -58,4 +60,36 @@ final class ShellServiceImpl: ShellService {
|
58 | 60 | return captureStream
|
59 | 61 | }
|
60 | 62 | }
|
| 63 | + |
| 64 | + @discardableResult func executeWithXCBeautify(arguments: [String]) throws { |
| 65 | + let output = Term.stdout |
| 66 | + let error = CaptureStream() |
| 67 | + let printStream = WriteStream.stdout |
| 68 | + let parser = Parser( |
| 69 | + colored: !disableColoredOutput, |
| 70 | + renderer: .terminal, |
| 71 | + preserveUnbeautifiedLines: preserveUnbeautified, |
| 72 | + additionalLines: { nil } |
| 73 | + ) |
| 74 | + let outputStream = makeBeautifyStream(outputStream: printStream, parser: paraser) |
| 75 | + |
| 76 | + let command = arguments.joined(separator: " ") |
| 77 | + let task = Task(executable: "/bin/bash", arguments: ["-c", command], stdout: outputStream) |
| 78 | + printService.printVerbose("shell command: '\(command)'") |
| 79 | + let exitCode = task.runSync() |
| 80 | + |
| 81 | + guard exitCode == 0 else { |
| 82 | + throw ToolsError(description: "shell command: '\(command)' failed with error") |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private func makeBeautifyStream(outputStream: WritableStream, parser: Parser) -> ProcessingStream { |
| 87 | + return LineStream { line in |
| 88 | + if let formatted = parser.parse(line: line) { |
| 89 | + outputStream.write(formatted) |
| 90 | + } else { |
| 91 | + outputStream.write(line) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
61 | 95 | }
|
0 commit comments