Files
xtool-mirror/Sources/XToolSupport/XTool.swift
Kabir Oberai 0710653cd0 Handle static xcframeworks not being unpacked (#156)
SwiftPM may not unpack static xcframeworks. Handle this gracefully, if
SwiftPM doesn't need the framework we don't either.

Fixes #155.
2025-08-10 18:05:28 -04:00

87 lines
2.6 KiB
Swift

import Foundation
import XKit
import ArgumentParser
import XUtils
public enum XTool {
public static func run(arguments: [String]? = nil) async throws {
TemporaryDirectory.prepare()
await XToolCommand.cancellableMain(arguments)
}
}
private struct XToolCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "xtool",
abstract: "Cross-platform Xcode replacement",
version: "xtool \(XTool.version)",
groupedSubcommands: [
CommandGroup(
name: "Configuration",
subcommands: [
SetupCommand.self,
AuthCommand.self,
SDKCommand.self,
]
),
CommandGroup(
name: "Development",
subcommands: [
NewCommand.self,
DevCommand.self,
DSCommand.self,
]
),
CommandGroup(
name: "Device",
subcommands: [
DevicesCommand.self,
InstallCommand.self,
UninstallCommand.self,
LaunchCommand.self,
]
)
]
)
}
#if compiler(<6.2)
private typealias SendableMetatype = Any
#endif
extension ParsableCommand where Self: SendableMetatype {
fileprivate static func cancellableMain(_ arguments: [String]? = nil) async {
let (canStart, cont) = AsyncStream.makeStream(of: Never.self)
let task = Task {
for await _ in canStart {}
guard !Task.isCancelled else { return }
do {
var command = try self.parseAsRoot(arguments)
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
} else {
try command.run()
}
} catch where error is CancellationError || Task.isCancelled {
print("Cancelled.")
self.exit()
} catch {
if ProcessInfo.processInfo.environment["XTL_DEBUG_ERRORS"] != nil {
print("ERROR DETAILS:")
print(String(reflecting: error))
}
self.exit(withError: error)
}
}
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT)
source.setEventHandler { task.cancel() }
source.resume()
cont.finish()
await task.value
}
}