Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.
Use swift-subprocess (#225)
Kabir Oberai5c4f3bcb0500
diff --git a/Package.resolved b/Package.resolvedindex e1d69b9..0bcfc14 100644--- a/Package.resolved+++ b/Package.resolved@@ -1,5 +1,5 @@{- "originHash" : "eb2a57fb4e4c2c83ff1f1fa55631db81988fc6e2e576a05a3cc5c8ed69432c3a",+ "originHash" : "80db0cddb0e78cf6a4c79f94537cdfebd8004679161532fbae8a57ee9d1d8cd1","pins" : [{"identity" : "aexml",@@ -352,6 +352,15 @@"version" : "2.11.0"}},+ {+ "identity" : "swift-subprocess",+ "kind" : "remoteSourceControl",+ "location" : "https://github.com/swiftlang/swift-subprocess",+ "state" : {+ "revision" : "11633673a41f509f8945f23c96c7acd4adafd679",+ "version" : "0.5.0"+ }+ },{"identity" : "swift-syntax","kind" : "remoteSourceControl",diff --git a/Package.swift b/Package.swiftindex 9446d21..8ea98c7 100644--- a/Package.swift+++ b/Package.swift@@ -55,6 +55,7 @@ let package = Package(.package(url: "https://github.com/apple/swift-nio", from: "2.77.0"),.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),+ .package(url: "https://github.com/swiftlang/swift-subprocess", .upToNextMinor(from: "0.5.0")),.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.23.0"),.package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"),@@ -96,6 +97,11 @@ let package = Package(name: "XUtils",dependencies: [.product(name: "SystemPackage", package: "swift-system"),+ .product(+ name: "Subprocess",+ package: "swift-subprocess",+ condition: .when(platforms: [.linux, .macOS])+ ),]),.target(diff --git a/Sources/PackLib/BuildSettings.swift b/Sources/PackLib/BuildSettings.swiftindex 8a3e09d..4ba0efc 100644--- a/Sources/PackLib/BuildSettings.swift+++ b/Sources/PackLib/BuildSettings.swift@@ -1,9 +1,11 @@import Foundation+import Subprocess+import XUtilspublic struct BuildSettings: Sendable {private static let customBinDir =// this is the same option used by SwiftPM itself for dev builds- ProcessInfo.processInfo.environment["SWIFTPM_CUSTOM_BIN_DIR"].map { URL(fileURLWithPath: $0) }+ ProcessInfo.processInfo.environment["SWIFTPM_CUSTOM_BIN_DIR"].map { FilePath($0) }private static let envURL = URL(fileURLWithPath: "/usr/bin/env")@@ -34,18 +36,16 @@ public struct BuildSettings: Sendable {#if os(macOS)private static func xcrun(_ arguments: [String]) async throws -> String {- let xcrun = Process()- let pipe = Pipe()- xcrun.executableURL = URL(fileURLWithPath: "/usr/bin/xcrun")- xcrun.arguments = arguments- xcrun.standardOutput = pipe- try await xcrun.runUntilExit()- return String(decoding: pipe.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)- .trimmingCharacters(in: .whitespacesAndNewlines)+ let result = try await Subprocess.run(+ .path("/usr/bin/xcrun"),+ arguments: .init(arguments),+ output: .string(limit: .max)+ ).checkSuccess()+ return result.standardOutput?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""}private static let swiftURL = Task {- try await URL(fileURLWithPath: xcrun(["-f", "swift"]))+ try await FilePath(xcrun(["-f", "swift"]))}#endif@@ -53,12 +53,11 @@ public struct BuildSettings: Sendable {forTool tool: String,arguments: [String],packagePathOverride: String? = nil- ) async throws -> Process {- let process = Process()-+ ) async throws -> Subprocess.Configuration {+ let executable: Executablelet baseArguments: [String]if let customBinDir = Self.customBinDir {- process.executableURL = customBinDir.appendingPathComponent("swift-\(tool)")+ executable = .path(customBinDir.appending("swift-\(tool)"))baseArguments = []} else {#if os(macOS)@@ -67,9 +66,9 @@ public struct BuildSettings: Sendable {// 1) invoking the real swift executable (located with `xcrun -f`) and// 2) explicitly removing SDKROOT from the env, as it may be inherited// through the `swift run pack` invocation.- process.executableURL = try await Self.swiftURL.value+ executable = .path(try await Self.swiftURL.value)#else- process.executableURL = try await ToolRegistry.locate("swift")+ executable = .name("swift")#endifbaseArguments = [tool]}@@ -79,11 +78,18 @@ public struct BuildSettings: Sendable {"--configuration", configuration.rawValue,]- var env = ProcessInfo.processInfo.environment- env.removeValue(forKey: "SDKROOT")- process.environment = env- process.arguments = baseArguments + extraArguments + sdkOptions + options + arguments- return process+ var rawEnv = ProcessInfo.processInfo.environment+ rawEnv.removeValue(forKey: "SDKROOT")+ let env = Dictionary(uniqueKeysWithValues: rawEnv.map {+ (Environment.Key(rawValue: $0)!, $1)+ })++ return Configuration(+ executable,+ arguments: .init(baseArguments + extraArguments + sdkOptions + options + arguments),+ environment: .custom(env),+ platformOptions: .withGracefulShutDown,+ )}}diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swiftindex 9712ead..054f65c 100644--- a/Sources/PackLib/Packer.swift+++ b/Sources/PackLib/Packer.swift@@ -1,5 +1,6 @@import Foundationimport XUtils+import Subprocesspublic struct Packer: Sendable {public let buildSettings: BuildSettings@@ -54,7 +55,7 @@ public struct Packer: Sendable {try Data().write(to: sources.appendingPathComponent("stub.c", isDirectory: false))}- let builder = try await buildSettings.swiftPMInvocation(+ let buildConfig = try await buildSettings.swiftPMInvocation(forTool: "build",arguments: ["--package-path", packageDir.path,@@ -68,8 +69,12 @@ public struct Packer: Sendable {"--disable-automatic-resolution",])- builder.standardOutput = FileHandle.standardError- try await builder.runUntilExit()+ try await Subprocess.run(+ buildConfig,+ output: .currentStandardError,+ error: .currentStandardError,+ )+ .checkSuccess()}public func pack() async throws -> URL {diff --git a/Sources/PackLib/Planner.swift b/Sources/PackLib/Planner.swiftindex b3f3e93..11501af 100644--- a/Sources/PackLib/Planner.swift+++ b/Sources/PackLib/Planner.swift@@ -1,5 +1,6 @@import Foundationimport XUtils+import Subprocesspublic struct Planner: Sendable {public var buildSettings: BuildSettings@@ -243,16 +244,18 @@ public struct Planner: Sendable {}private func _dumpAction(arguments: [String], path: String) async throws -> Data {- let dump = try await buildSettings.swiftPMInvocation(+ let dumpConfig = try await buildSettings.swiftPMInvocation(forTool: "package",arguments: arguments,packagePathOverride: path)- let pipe = Pipe()- dump.standardOutput = pipe- async let task = Data(reading: pipe.fileHandleForReading)- try await dump.runUntilExit()- return try await task+ return try await Subprocess.run(+ dumpConfig,+ output: .data(limit: .max),+ error: .currentStandardError,+ )+ .checkSuccess()+ .standardOutput}private func selectLibrary(diff --git a/Sources/PackLib/Process+Helpers.swift b/Sources/PackLib/Process+Helpers.swiftdeleted file mode 100644index 3b47bcb..0000000--- a/Sources/PackLib/Process+Helpers.swift+++ /dev/null@@ -1,63 +0,0 @@-import Foundation--extension Process {- /// Launches the process and suspends until the receiver is finished.- ///- /// - Parameter onCancel: The action to take if the current- /// task is cancelled.- public func runUntilExit(onCancel: TaskCancelAction = .interrupt) async throws {- try Task.checkCancellation()-- let (terminationStream, terminationContinuation) = AsyncStream<Never>.makeStream()- terminationHandler = { _ in- terminationContinuation.finish()- }-- do {- try run()- } catch {- terminationContinuation.finish()- throw error- }-- await withTaskCancellationHandler {- for await _ in terminationStream {}- } onCancel: {- switch onCancel {- case .interrupt:- interrupt()- case .terminate:- terminate()- case .ignore:- break- }- }-- try Task.checkCancellation()-- switch terminationReason {- case .exit where terminationStatus == 0:- break- case .exit:- throw Failure.exit(terminationStatus)- case .uncaughtSignal:- throw Failure.uncaughtSignal(terminationStatus)- @unknown default:- break- }- }-- public enum Failure: Error {- case exit(CInt)- case uncaughtSignal(CInt)- }-- public enum TaskCancelAction: Sendable {- /// Sends `SIGINT` to the process.- case interrupt- /// Sends `SIGTERM` to the process.- case terminate- /// Don't participate in cooperative cancellation.- case ignore- }-}diff --git a/Sources/PackLib/ToolRegistry.swift b/Sources/PackLib/ToolRegistry.swiftindex 0d82e28..502c966 100644--- a/Sources/PackLib/ToolRegistry.swift+++ b/Sources/PackLib/ToolRegistry.swift@@ -1,4 +1,5 @@import Foundation+import Subprocesspackage enum ToolRegistry {package enum Errors: Error, CustomStringConvertible {@@ -16,11 +17,6 @@ package enum ToolRegistry {/// Obtain the full path to a tool in the user's `PATH`.///- /// This effectively invokes `/bin/sh -c "command -v '$tool'"`.- ///- /// - Warning: Make sure you trust/sanitize the `tool` parameter. If it- /// contains a single quote, it can be used in a shell escape.- ////// - Throws: `Errors.toolNotFound` if the tool could not be located.package static func locate(_ tool: String) async throws -> URL {try await cache.locate(tool: tool)@@ -30,20 +26,10 @@ package enum ToolRegistry {private var cache: [String: Task<URL, Error>] = [:]private func _locate(tool: String) async throws -> URL {- let pipe = Pipe()- let proc = Process()- proc.executableURL = URL(fileURLWithPath: "/bin/sh")- proc.arguments = ["-c", "command -v '\(tool)'"]- proc.standardOutput = pipe- async let bytes = pipe.fileHandleForReading.readToEnd()- do {- try await proc.runUntilExit()- } catch is Process.Failure {- throw Errors.toolNotFound(tool)- }- let path = String(decoding: try await bytes ?? Data(), as: UTF8.self)- .trimmingCharacters(in: .whitespacesAndNewlines)- return URL(fileURLWithPath: path)+ guard let path = try? await Executable.name(tool).resolveExecutablePath(in: .inherit),+ let url = URL(filePath: path)+ else { throw Errors.toolNotFound(tool) }+ return url}func locate(tool: String) async throws -> URL {diff --git a/Sources/XKit/GrandSlam/Anisette/XADIProvider.swift b/Sources/XKit/GrandSlam/Anisette/XADIProvider.swiftindex cf5f9d6..3183101 100644--- a/Sources/XKit/GrandSlam/Anisette/XADIProvider.swift+++ b/Sources/XKit/GrandSlam/Anisette/XADIProvider.swift@@ -3,6 +3,7 @@import Foundationimport XADIimport Dependencies+import Subprocesspublic actor XADIProvider: RawADIProvider {@MainActor private static var loadTask: Task<Void, Error>?@@ -45,17 +46,17 @@ public actor XADIProvider: RawADIProvider {let archDir = "lib/\(arch)"// TODO: Use ZIPFoundation- let proc = Process()- proc.executableURL = URL(filePath: "/usr/bin/env")- proc.arguments = [- "unzip", "-q",- applemusic.path(),- "\(archDir)/libCoreADI.so",- "\(archDir)/libstoreservicescore.so",- "-d", tmp.path()- ]- try proc.run()- proc.waitUntilExit()+ try await Subprocess.run(+ .name("unzip"),+ arguments: [+ "-q",+ applemusic.path,+ "\(archDir)/libCoreADI.so",+ "\(archDir)/libstoreservicescore.so",+ "-d", tmp.path+ ],+ output: .discarded+ ).checkSuccess()try FileManager.default.moveItem(at: tmp.appending(path: archDir),to: libDirdiff --git a/Sources/XToolSupport/Console.swift b/Sources/XToolSupport/Console.swiftindex 07cc77e..8dc420c 100644--- a/Sources/XToolSupport/Console.swift+++ b/Sources/XToolSupport/Console.swift@@ -1,6 +1,6 @@import Foundationimport XKit-import SystemPackage+import XUtilsimport NIOPosiximport NIOCorediff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swiftindex 64d3327..a827354 100644--- a/Sources/XToolSupport/DevCommand.swift+++ b/Sources/XToolSupport/DevCommand.swift@@ -282,6 +282,8 @@ struct DevCommand: AsyncParsableCommand {extension BuildConfiguration: ExpressibleByArgument {}#if os(macOS)+import Subprocess+struct SimInstallOperation {var path: URL@@ -289,10 +291,12 @@ struct SimInstallOperation {var simulator = "booted"func run() async throws {- let process = Process()- process.executableURL = URL(fileURLWithPath: "/usr/bin/xcrun")- process.arguments = ["simctl", "install", simulator, path.path]- try await process.runUntilExit()+ try await Subprocess.run(+ .path("/usr/bin/xcrun"),+ arguments: ["simctl", "install", simulator, path.path],+ output: .discarded+ )+ .checkSuccess()print("Installed to simulator")}}diff --git a/Sources/XToolSupport/ProcessZIPCompressor.swift b/Sources/XToolSupport/ProcessZIPCompressor.swiftindex 0a68503..cc04f05 100644--- a/Sources/XToolSupport/ProcessZIPCompressor.swift+++ b/Sources/XToolSupport/ProcessZIPCompressor.swift@@ -2,6 +2,8 @@ import Foundationimport XKitimport Dependenciesimport PackLib+import Subprocess+import XUtilsextension ZIPCompressor: DependencyKey {// TODO: Use `powershell Compress-Archive` and `powershell Expand-Archive` on Windows@@ -12,20 +14,23 @@ extension ZIPCompressor: DependencyKey {let dest = dir.deletingLastPathComponent().appendingPathComponent("app.ipa")- let zip = Process()- zip.executableURL = try await ToolRegistry.locate("zip")- zip.currentDirectoryURL = dir.deletingLastPathComponent()- zip.arguments = ["-yqru0", dest.path, dir.lastPathComponent]- try await zip.runUntilExit()+ try await Subprocess.run(+ .name("zip"),+ arguments: ["-yqru0", dest.path, dir.lastPathComponent],+ workingDirectory: FilePath(dir.deletingLastPathComponent()),+ output: .discarded,+ ).checkSuccess()return dest},decompress: { ipa, directory, progress inprogress(nil)- let unzip = Process()- unzip.executableURL = try await ToolRegistry.locate("unzip")- unzip.arguments = ["-q", ipa.path, "-d", directory.path]- try await unzip.runUntilExit()++ try await Subprocess.run(+ .name("unzip"),+ arguments: ["-q", ipa.path, "-d", directory.path],+ output: .discarded,+ ).checkSuccess()})}diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swiftindex 051bf0e..96e7f6f 100644--- a/Sources/XToolSupport/SDKBuilder.swift+++ b/Sources/XToolSupport/SDKBuilder.swift@@ -1,9 +1,9 @@import Foundationimport Dependencies-import SystemPackageimport libunxip+import Subprocessimport XKit // HTTPClient, stdoutSafe-import PackLib // ToolRegistry+import XUtils // System.File{Path,Descriptor}struct SDKBuilder {enum Arch: String {@@ -180,14 +180,6 @@ struct SDKBuilder {withIntermediateDirectories: false)- let pipe = Pipe()- let untar = Process()- untar.currentDirectoryURL = toolsetDir- untar.executableURL = try await ToolRegistry.locate("tar")- untar.arguments = ["xzf", "-"]- untar.standardInput = pipe.fileHandleForReading- async let tarExit: Void = untar.runUntilExit()-@Dependency(\.httpClient) var httpClientlet url = URL(string: """https://github.com/xtool-org/darwin-tools-linux-llvm/releases/download/\@@ -199,22 +191,33 @@ struct SDKBuilder {case .known(let known): knowncase .unknown: nil}- let writer = pipe.fileHandleForWriting- var written: Int64 = 0- do {- defer { try? writer.close() }++ defer { print() }+ try await Subprocess.run(+ .name("tar"),+ arguments: ["xzf", "-"],+ workingDirectory: FilePath(toolsetDir),+ input: .inputWriter,+ output: .discarded,+ error: .discarded,+ ) { execution in+ var totalWritten: Int64 = 0for try await chunk in body {- try writer.write(contentsOf: chunk)- written += Int64(chunk.count)- if let length {- let progress = Int(Double(written) / Double(length) * 100)+ var remaining = chunk+ while !remaining.isEmpty {+ let written = try await execution.standardInputWriter.write(Array(remaining))+ remaining = remaining.dropFirst(written)+ totalWritten += Int64(written)++ guard let length else { continue }+ let progress = Int(Double(totalWritten) / Double(length) * 100)print("\r[Downloading toolset] \(progress)%", terminator: "")fflush(stdoutSafe)}}+ try await execution.standardInputWriter.finish()}- print()- try await tarExit+ .checkSuccess()}// swiftlint:disable:next cyclomatic_complexity function_body_lengthdiff --git a/Sources/XToolSupport/SDKCommand.swift b/Sources/XToolSupport/SDKCommand.swiftindex 696d26a..cdb0bfc 100644--- a/Sources/XToolSupport/SDKCommand.swift+++ b/Sources/XToolSupport/SDKCommand.swift@@ -5,6 +5,7 @@ import ArgumentParserimport Dependenciesimport PackLibimport XUtils+import Subprocessstruct SDKCommand: AsyncParsableCommand {static let configuration = CommandConfiguration(@@ -151,50 +152,40 @@ struct DarwinSDK {try await addHostClangResourceDir(to: url)- let process = Process()- process.executableURL = try await ToolRegistry.locate("swift")- process.arguments = ["sdk", "install", url.path]- try await process.runUntilExit()+ try await Subprocess.run(+ .name("swift"),+ arguments: ["sdk", "install", url.path],+ output: .discarded+ )+ .checkSuccess()}private static func addHostClangResourceDir(to sdk: URL) async throws {- let outPipe = Pipe()- let clang = Process()- clang.executableURL = try await ToolRegistry.locate("clang")- clang.arguments = ["-print-resource-dir"]- clang.standardOutput = outPipe- clang.standardError = FileHandle.standardError- async let outputData = Data(reading: outPipe.fileHandleForReading)- do {- try await clang.runUntilExit()- } catch is Process.Failure {- throw Console.Error("Failed to query host clang -print-resource-dir")- }- let path = String(decoding: try await outputData, as: UTF8.self)- .trimmingCharacters(in: .whitespacesAndNewlines)- guard !path.isEmpty else {- throw Console.Error("Host clang returned an empty resource directory")- }- let hostClangResources = URL(fileURLWithPath: path, isDirectory: true)+ let clangURL = try await ToolRegistry.locate("clang")+ let process = try await Subprocess.run(+ .path(FilePath(clangURL.path)),+ arguments: ["-print-resource-dir"],+ output: .string(limit: .max)+ ).checkSuccess()+ let output = process.standardOutput ?? ""+ let hostClangResources = URL(filePath: output.trimmingCharacters(in: .whitespacesAndNewlines))let hostInclude = hostClangResources.appending(path: "include")let sdkInclude = sdk.appending(path: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include")try FileManager.default.copyItem(at: hostInclude, to: sdkInclude)}static func current() async throws -> DarwinSDK? {- let output = Pipe()-- let process = Process()- process.executableURL = try await ToolRegistry.locate("swift")- process.arguments = ["sdk", "configure", "darwin", "arm64-apple-ios", "--show-configuration"]- process.standardOutput = output- process.standardError = FileHandle.nullDevice-- async let outputData = Data(reading: output.fileHandleForReading)-+ let outputString: Stringdo {- try await process.runUntilExit()- } catch Process.Failure.exit {+ outputString = try await Subprocess.run(+ .name("swift"),+ arguments: ["sdk", "configure", "darwin", "arm64-apple-ios", "--show-configuration"],+ output: .string(limit: .max)+ )+ .checkSuccess()+ .standardOutput+ ?? ""+ } catch SubprocessFailure.exited {return nil}@@ -202,7 +193,6 @@ struct DarwinSDK {// swiftResourcesPath: /home/user/.swiftpm/swift-sdks/darwin.artifactbundle/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift// swiftlint:disable:previous line_lengthlet resourcesPathPrefix = "swiftResourcesPath: "- let outputString = String(decoding: try await outputData, as: UTF8.self)guard let resourcesPath = outputString.split(separator: "\n")@@ -230,21 +220,19 @@ struct DarwinSDK {private enum SwiftVersion {}extension SwiftVersion {static func current() async throws -> Version {- let outPipe = Pipe()- let errPipe = Pipe()- let swift = Process()- swift.executableURL = try await ToolRegistry.locate("swift")- swift.arguments = ["--version"]- swift.standardOutput = outPipe- swift.standardError = errPipe- async let outputTask = outPipe.fileHandleForReading.readToEnd()+ let outputString: String?do {- try await swift.runUntilExit()- } catch is Process.Failure {+ outputString = try await Subprocess.run(+ .name("swift"),+ arguments: ["--version"],+ output: .string(limit: .max)+ )+ .checkSuccess()+ .standardOutput+ } catch {throw Console.Error("Failed to obtain Swift version")}- let outputData = try await outputTask- var output = String(decoding: outputData ?? Data(), as: UTF8.self)[...]+ var output = outputString?[...] ?? ""if output.hasPrefix("Apple ") {output = output.dropFirst("Apple ".count)}diff --git a/Sources/XUtils/Subprocess+Utils.swift b/Sources/XUtils/Subprocess+Utils.swiftnew file mode 100644index 0000000..c8c48e9--- /dev/null+++ b/Sources/XUtils/Subprocess+Utils.swift@@ -0,0 +1,57 @@+#if canImport(Subprocess)+import Subprocess+import Foundation++extension ExecutionResult {+ @discardableResult+ public func checkSuccess() throws(SubprocessFailure) -> Self {+ try terminationStatus.checkSuccess()+ return self+ }+}++extension TerminationStatus {+ fileprivate func checkSuccess() throws(SubprocessFailure) {+ if let failure { throw failure }+ }++ private var failure: SubprocessFailure? {+ if isSuccess { return nil }+ switch self {+ case .exited(let code): return .exited(code)+ case .signaled(let code): return .signaled(code)+ }+ }+}++public enum SubprocessFailure: Error {+ case exited(TerminationStatus.Code)+ case signaled(TerminationStatus.Code)+}++extension PlatformOptions {+ public static var withGracefulShutDown: Self {+ .init().withGracefulShutDown+ }++ public var withGracefulShutDown: Self {+ var copy = self+ copy.teardownSequence = [+ .gracefulShutDown(+ allowedDurationToNextStep: .milliseconds(500)+ )+ ]+ return copy+ }+}++extension Environment {+ public static func currentMap() -> [Environment.Key: String] {+ Dictionary(+ uniqueKeysWithValues: ProcessInfo.processInfo.environment.map {+ (Environment.Key(rawValue: $0)!, $1)+ }+ )+ }+}+#endif