File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -31,4 +31,18 @@ struct SwiftSyntaxDevUtils: ParsableCommand {
3131 VerifySourceCode . self,
3232 ]
3333 )
34+
35+ public static func main( ) {
36+ SigIntListener . registerSigIntSubprocessTerminationHandler ( )
37+ do {
38+ var command = try parseAsRoot ( nil )
39+ try command. run ( )
40+ } catch {
41+ if !SigIntListener. hasReceivedSigInt {
42+ // No point printing an error message if the user requested the termination
43+ // of the script.
44+ exit ( withError: error)
45+ }
46+ }
47+ }
3448}
Original file line number Diff line number Diff line change 1212
1313import Foundation
1414
15+ /// Keeps track of subprocesses spawned by this script and forwards SIGINT
16+ /// signals to them.
17+ class SigIntListener {
18+ /// The subprocesses spawned by this script that are currently running.
19+ static var runningSubprocesses : Set < Process > = [ ]
20+
21+ /// Whether a `SIGINT` signal has been received by this script.
22+ static var hasReceivedSigInt : Bool = false
23+
24+ /// Registers a `SIGINT` signal handler that forwards `SIGINT` to all
25+ /// subprocesses that are registered in `runningSubprocesses`
26+ static func registerSigIntSubprocessTerminationHandler( ) {
27+ #if canImport(Darwin) || canImport(Glibc)
28+ signal ( SIGINT) { _ in
29+ SigIntListener . hasReceivedSigInt = true
30+ for process in SigIntListener . runningSubprocesses {
31+ process. interrupt ( )
32+ }
33+ }
34+ #endif
35+ }
36+ }
37+
1538/// Provides convenience APIs for launching and gathering output from a subprocess
1639public class ProcessRunner {
1740 private static let serialQueue = DispatchQueue ( label: " \( ProcessRunner . self) " )
@@ -56,7 +79,9 @@ public class ProcessRunner {
5679 }
5780
5881 try process. run ( )
82+ SigIntListener . runningSubprocesses. insert ( process)
5983 process. waitUntilExit ( )
84+ SigIntListener . runningSubprocesses. remove ( process)
6085 if captureStdout || captureStderr {
6186 // Make sure we've received all stdout/stderr
6287 group. wait ( )
You can’t perform that action at this time.
0 commit comments