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.

Create ToolRegistry

Kabir Oberai4e77c5be336a
diff --git a/Sources/SupersignCLISupport/DevNewCommand.swift b/Sources/SupersignCLISupport/DevNewCommand.swift
index 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 = baseURL
try gitInit.run()
await gitInit.waitForExit()
diff --git a/Sources/SupersignCLISupport/DevSDKCommand.swift b/Sources/SupersignCLISupport/DevSDKCommand.swift
index 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 = outPipe
swift.standardError = errPipe
try 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.zip
diff --git a/Sources/SupersignCLISupport/ProcessZIPCompressor.swift b/Sources/SupersignCLISupport/ProcessZIPCompressor.swift
index 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 in
progress(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.swift
new file mode 100644
index 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.swift
deleted file mode 100644
index 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()
-}