Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

utils: support forwarding Subprocess output (#413)

Kabir Oberaibec5aa106ac3
diff --git a/Package.swift b/Package.swift
index a15419b..1f472f4 100644
--- a/Package.swift
+++ b/Package.swift
@@ -113,6 +113,7 @@ let package = Package(
name: "XUtils",
dependencies: [
"CXKit",
+ .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "SystemPackage", package: "swift-system"),
.product(
diff --git a/Sources/XUtils/CommandOutput.swift b/Sources/XUtils/CommandOutput.swift
index f201f29..39e01eb 100644
--- a/Sources/XUtils/CommandOutput.swift
+++ b/Sources/XUtils/CommandOutput.swift
@@ -56,6 +56,21 @@ public struct CommandOutput: DependencyKey, Sendable {
public static var testValue: Self { Self(write: unimplemented("CommandOutput.write")) }
}
+extension CommandOutput {
+ /// Create a `FileDescriptor` for writing to the given `CommandOutput` stream.
+ public func makeDescriptor(for stream: Stream) throws -> FileDescriptor {
+ let (readFD, writeFD) = try FileDescriptor.pipe()
+ let input = try DispatchInput(fileDesciptor: readFD)
+ // no need to keep a handle to the task, it'll terminate when writeFD is closed
+ Task {
+ while let next = try? await input.read() {
+ self.write(next, stream, false)
+ }
+ }
+ return writeFD
+ }
+}
+
extension DependencyValues {
public var out: CommandOutput {
get { self[CommandOutput.self] }
diff --git a/Sources/XCLIUtils/DataChannel.swift b/Sources/XUtils/DataChannel.swift
similarity index 100%
rename from Sources/XCLIUtils/DataChannel.swift
rename to Sources/XUtils/DataChannel.swift
diff --git a/Sources/XCLIUtils/DispatchInput.swift b/Sources/XUtils/DispatchInput.swift
similarity index 90%
rename from Sources/XCLIUtils/DispatchInput.swift
rename to Sources/XUtils/DispatchInput.swift
index 27bf281..fc94334 100644
--- a/Sources/XCLIUtils/DispatchInput.swift
+++ b/Sources/XUtils/DispatchInput.swift
@@ -1,14 +1,13 @@
import Foundation
-import XUtils
import ConcurrencyExtras
-final class DispatchInput: Sendable {
+public final class DispatchInput: Sendable {
private struct State {
var hasClosed = false
var isReading = false
}
- static let defaultChunkSize: Int = 64 * 1024
+ public static let defaultChunkSize: Int = 64 * 1024
let queue = DispatchQueue(label: "xtool.dispatch-reader")
let channel: DispatchIO
@@ -17,7 +16,7 @@ final class DispatchInput: Sendable {
private let dataChannel = DataChannel()
private let state = LockIsolated(State())
- init(fileDesciptor: FileDescriptor, chunkSize: Int = DispatchInput.defaultChunkSize) throws {
+ public init(fileDesciptor: FileDescriptor, chunkSize: Int = DispatchInput.defaultChunkSize) throws {
let closeSignal = Task { _ = try? await Task.never() }
self.closeSignal = closeSignal
self.chunkSize = chunkSize
@@ -65,7 +64,7 @@ final class DispatchInput: Sendable {
}
}
- nonisolated(nonsending) func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? {
+ nonisolated(nonsending) public func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? {
try await dataChannel.read(upTo: limit)
}
@@ -110,7 +109,7 @@ final class DispatchInput: Sendable {
}
}
- nonisolated(nonsending) func close() async {
+ nonisolated(nonsending) public func close() async {
requestClose(error: nil)
await closeSignal.value
}
diff --git a/Sources/XUtils/Subprocess+Utils.swift b/Sources/XUtils/Subprocess+Utils.swift
index c8c48e9..25ed68c 100644
--- a/Sources/XUtils/Subprocess+Utils.swift
+++ b/Sources/XUtils/Subprocess+Utils.swift
@@ -54,4 +54,21 @@ extension Environment {
)
}
}
+
+extension OutputProtocol where Self == FileDescriptorOutput {
+ /// Writes subprocess output to `d.out`'s stdout
+ public static func xtoolOutput() throws -> Self {
+ try makeXToolOutput(for: .stdout)
+ }
+
+ /// Writes subprocess output to `d.out`'s stderr
+ public static func xtoolError() throws -> Self {
+ try makeXToolOutput(for: .stderr)
+ }
+
+ private static func makeXToolOutput(for stream: CommandOutput.Stream) throws -> Self {
+ let fd = try d.out.makeDescriptor(for: stream)
+ return .fileDescriptor(fd, closeAfterSpawningProcess: true)
+ }
+}
#endif
diff --git a/Tests/XToolTests/CommandInputTests.swift b/Tests/XToolTests/CommandInputTests.swift
index d7bba91..f437098 100644
--- a/Tests/XToolTests/CommandInputTests.swift
+++ b/Tests/XToolTests/CommandInputTests.swift
@@ -1,4 +1,5 @@
@testable import XCLIUtils
+import XUtils
import ConcurrencyExtras
import Foundation
import Testing
diff --git a/Tests/XToolTests/DataChannelTests.swift b/Tests/XToolTests/DataChannelTests.swift
index b0593d8..8c676ff 100644
--- a/Tests/XToolTests/DataChannelTests.swift
+++ b/Tests/XToolTests/DataChannelTests.swift
@@ -1,4 +1,4 @@
-@testable import XCLIUtils
+@testable import XUtils
import ConcurrencyExtras
import CustomDump
import Foundation
diff --git a/Tests/XToolTests/DispatchInputTests.swift b/Tests/XToolTests/DispatchInputTests.swift
index 34ce9fb..f5adc57 100644
--- a/Tests/XToolTests/DispatchInputTests.swift
+++ b/Tests/XToolTests/DispatchInputTests.swift
@@ -1,8 +1,8 @@
-@testable import XCLIUtils
+@testable import XUtils
+import XCLIUtils
import CustomDump
import Foundation
import Testing
-import XUtils
@Suite(.timeLimit(.minutes(1)))
struct DispatchInputTests {