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.

Improve cancellation handling

Kabir Oberaif16c2c869dad
diff --git a/Sources/SupersignCLISupport/SupersignCommand.swift b/Sources/SupersignCLISupport/SupersignCommand.swift
index eb4d7d0..e37fdd8 100644
--- a/Sources/SupersignCLISupport/SupersignCommand.swift
+++ b/Sources/SupersignCLISupport/SupersignCommand.swift
@@ -21,16 +21,7 @@ public enum SupersignCLI {
public static func run(configuration: Configuration, arguments: [String]? = nil) async throws {
_config = configuration
- do {
- var command = try SupersignCommand.parseAsRoot(arguments)
- if var asyncCommand = command as? AsyncParsableCommand {
- try await asyncCommand.run()
- } else {
- try command.run()
- }
- } catch {
- SupersignCommand.exit(withError: error)
- }
+ await SupersignCommand.cancellableMain(arguments)
}
}
@@ -51,3 +42,34 @@ struct SupersignCommand: AsyncParsableCommand {
]
)
}
+
+extension ParsableCommand {
+ 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()
+ if var asyncCommand = command as? AsyncParsableCommand {
+ try await asyncCommand.run()
+ } else {
+ try command.run()
+ }
+ } catch is CancellationError {
+ self.exit()
+ } catch {
+ 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
+ }
+}