Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.
Create ToolRegistry
Kabir Oberai4e77c5be336a
diff --git a/Sources/SupersignCLISupport/DevNewCommand.swift b/Sources/SupersignCLISupport/DevNewCommand.swiftindex 41cfcfc..da8189e 100644--- a/Sources/SupersignCLISupport/DevNewCommand.swift+++ b/Sources/SupersignCLISupport/DevNewCommand.swift@@ -161,8 +161,8 @@ struct DevNewCommand: AsyncParsableCommand {}let gitInit = Process()- gitInit.executableURL = URL(fileURLWithPath: "/usr/bin/env")- gitInit.arguments = ["git", "init"]+ gitInit.executableURL = try await ToolRegistry.locate("git")+ gitInit.arguments = ["init"]gitInit.currentDirectoryURL = baseURLtry gitInit.run()await gitInit.waitForExit()diff --git a/Sources/SupersignCLISupport/DevSDKCommand.swift b/Sources/SupersignCLISupport/DevSDKCommand.swiftindex 6148805..38c4c99 100644--- a/Sources/SupersignCLISupport/DevSDKCommand.swift+++ b/Sources/SupersignCLISupport/DevSDKCommand.swift@@ -149,8 +149,8 @@ extension SwiftVersion {let outPipe = Pipe()let errPipe = Pipe()let swift = Process()- swift.executableURL = URL(fileURLWithPath: "/usr/bin/env")- swift.arguments = ["swift", "--version"]+ swift.executableURL = try await ToolRegistry.locate("swift")+ swift.arguments = ["--version"]swift.standardOutput = outPipeswift.standardError = errPipetry swift.run()@@ -217,9 +217,9 @@ struct InstallSDKOperation {}let install = Process()- install.executableURL = URL(fileURLWithPath: "/usr/bin/env")+ install.executableURL = try await ToolRegistry.locate("swift")install.arguments = [- "swift", "sdk", "install",+ "sdk", "install","""https://github.com/kabiroberai/swift-sdk-darwin/releases/download/\(list.current)/\darwin-linux-\(arch.releaseName).artifactbundle.zipdiff --git a/Sources/SupersignCLISupport/ProcessZIPCompressor.swift b/Sources/SupersignCLISupport/ProcessZIPCompressor.swiftindex 8c82b04..e63f984 100644--- a/Sources/SupersignCLISupport/ProcessZIPCompressor.swift+++ b/Sources/SupersignCLISupport/ProcessZIPCompressor.swift@@ -19,9 +19,9 @@ extension ZIPCompressor: DependencyKey {let dest = dir.deletingLastPathComponent().appendingPathComponent("app.ipa")let zip = Process()- zip.executableURL = URL(fileURLWithPath: "/usr/bin/env")+ zip.executableURL = try await ToolRegistry.locate("zip")zip.currentDirectoryURL = dir.deletingLastPathComponent()- zip.arguments = ["zip", "-yqru0", dest.path, "Payload"]+ zip.arguments = ["-yqru0", dest.path, "Payload"]try zip.run()await zip.waitForExit()guard zip.terminationStatus == 0 else {@@ -33,8 +33,8 @@ extension ZIPCompressor: DependencyKey {decompress: { ipa, directory, progress inprogress(nil)let unzip = Process()- unzip.executableURL = URL(fileURLWithPath: "/usr/bin/env")- unzip.arguments = ["unzip", "-q", ipa.path, "-d", directory.path]+ unzip.executableURL = try await ToolRegistry.locate("unzip")+ unzip.arguments = ["-q", ipa.path, "-d", directory.path]try unzip.run()await unzip.waitForExit()guard unzip.terminationStatus == 0 else {diff --git a/Sources/SupersignCLISupport/ToolRegistry.swift b/Sources/SupersignCLISupport/ToolRegistry.swiftnew file mode 100644index 0000000..8fd87c1--- /dev/null+++ b/Sources/SupersignCLISupport/ToolRegistry.swift@@ -0,0 +1,55 @@+import Foundation++enum ToolRegistry {+ enum Errors: Error, CustomStringConvertible {+ case toolNotFound(String)++ var description: String {+ switch self {+ case .toolNotFound(let tool):+ "Could not find executable '\(tool)' in PATH"+ }+ }+ }++ private static let cache = Cache()++ static func locate(_ tool: StaticString) async throws -> URL {+ try await cache.locate(tool: tool)+ }++ private actor Cache {+ 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()+ try proc.run()+ await proc.waitForExit()+ guard proc.terminationStatus == 0 else {+ throw Errors.toolNotFound(tool)+ }+ let path = String(decoding: try await bytes ?? Data(), as: UTF8.self)+ .trimmingCharacters(in: .whitespacesAndNewlines)+ return URL(fileURLWithPath: path)+ }++ func locate(tool: StaticString) async throws -> URL {+ let tool = "\(tool)"++ let task: Task<URL, Error>+ if let cached = cache[tool] {+ task = cached+ } else {+ task = Task { try await _locate(tool: tool) }+ cache[tool] = task+ }++ return try await task.value+ }+ }+}diff --git a/Sources/SupersignCLISupport/Utilities.swift b/Sources/SupersignCLISupport/Utilities.swiftdeleted file mode 100644index efb0210..0000000--- a/Sources/SupersignCLISupport/Utilities.swift+++ /dev/null@@ -1,21 +0,0 @@-import Foundation-import Supersign--func withSyncContinuation<T>(- _ action: (_ cont: @escaping (T) -> Void) -> Void-) -> T {- var value: T!- let sem = DispatchSemaphore(value: 0)- action {- value = $0- sem.signal()- }- sem.wait()- return value-}--func withSyncContinuation<T>(- _ action: (_ cont: @escaping (Result<T, Error>) -> Void) -> Void-) throws -> T {- try withSyncContinuation(action).get()-}