Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XToolSupport / XTool.swift

XTool.swift91 lines · 2.8 KiBRaw
1import XCLIUtils
2import Foundation
3import XKit
4import ArgumentParser
5import XUtils
6import Dependencies
7
8public enum XTool {
9 public static func run(arguments: [String]? = nil) async throws {
10 TemporaryDirectory.prepare()
11 await XToolCommand.cancellableMain(arguments)
12 }
13}
14
15private struct XToolCommand: AsyncParsableCommand {
16 static let configuration = CommandConfiguration(
17 commandName: "xtool",
18 abstract: "Cross-platform Xcode replacement",
19 version: "xtool \(XTool.version)",
20 groupedSubcommands: [
21 CommandGroup(
22 name: "Configuration",
23 subcommands: [
24 SetupCommand.self,
25 AuthCommand.self,
26 SDKCommand.self,
27 ]
28 ),
29 CommandGroup(
30 name: "Development",
31 subcommands: [
32 NewCommand.self,
33 DevCommand.self,
34 DSCommand.self,
35 DistCommand.self,
36 ]
37 ),
38 CommandGroup(
39 name: "Device",
40 subcommands: [
41 DevicesCommand.self,
42 InstallCommand.self,
43 UninstallCommand.self,
44 LaunchCommand.self,
45 ]
46 )
47 ]
48 )
49}
50
51#if compiler(<6.2)
52private typealias SendableMetatype = Any
53#endif
54
55extension ParsableCommand where Self: SendableMetatype {
56 fileprivate static func cancellableMain(_ arguments: [String]? = nil) async {
57 let (canStart, cont) = AsyncStream.makeStream(of: Never.self)
58 let task = Task {
59 for await _ in canStart {}
60 guard !Task.isCancelled else { return }
61 do {
62 @Dependency(\.shutdownRegistry) var shutdownRegistry
63 defer { await shutdownRegistry.shutdown() }
64 var command = try self.parseAsRoot(arguments)
65 if var asyncCommand = command as? AsyncParsableCommand {
66 try await asyncCommand.run()
67 } else {
68 try command.run()
69 }
70 } catch where error is CancellationError || Task.isCancelled {
71 d.out.print("Cancelled.", stream: .stderr)
72 self.exit()
73 } catch {
74 if ProcessInfo.processInfo.environment["XTL_DEBUG_ERRORS"] != nil {
75 d.out.print("ERROR DETAILS:")
76 d.out.print(String(reflecting: error))
77 }
78 self.exit(withError: error)
79 }
80 }
81
82 signal(SIGINT, SIG_IGN)
83 let source = DispatchSource.makeSignalSource(signal: SIGINT)
84 source.setEventHandler { task.cancel() }
85 source.resume()
86
87 cont.finish()
88
89 await task.value
90 }
91}