@@ -16,7 +16,7 @@ import CoreFoundation
16
16
import Glibc
17
17
#endif
18
18
19
- extension Task {
19
+ extension Process {
20
20
public enum TerminationReason : Int {
21
21
case exit
22
22
case uncaughtSignal
@@ -72,27 +72,27 @@ private func runloopIsEqual(_ a : UnsafeRawPointer?, _ b : UnsafeRawPointer?) ->
72
72
}
73
73
74
74
75
- // Equal method for task in run loop source
76
- private func nstaskIsEqual ( _ a : UnsafeRawPointer ? , _ b : UnsafeRawPointer ? ) -> _DarwinCompatibleBoolean {
75
+ // Equal method for process in run loop source
76
+ private func processIsEqual ( _ a : UnsafeRawPointer ? , _ b : UnsafeRawPointer ? ) -> _DarwinCompatibleBoolean {
77
77
78
- let unmanagedTaskA = Unmanaged< AnyObject> . fromOpaque( a!)
79
- guard let taskA = unmanagedTaskA . takeUnretainedValue ( ) as? Task else {
78
+ let unmanagedProcessA = Unmanaged< AnyObject> . fromOpaque( a!)
79
+ guard let processA = unmanagedProcessA . takeUnretainedValue ( ) as? Process else {
80
80
return false
81
81
}
82
82
83
- let unmanagedTaskB = Unmanaged< AnyObject> . fromOpaque( a!)
84
- guard let taskB = unmanagedTaskB . takeUnretainedValue ( ) as? Task else {
83
+ let unmanagedProcessB = Unmanaged< AnyObject> . fromOpaque( a!)
84
+ guard let processB = unmanagedProcessB . takeUnretainedValue ( ) as? Process else {
85
85
return false
86
86
}
87
87
88
- guard taskA == taskB else {
88
+ guard processA == processB else {
89
89
return false
90
90
}
91
91
92
92
return true
93
93
}
94
94
95
- open class Task : NSObject {
95
+ open class Process : NSObject {
96
96
private static func setup( ) {
97
97
struct Once {
98
98
static var done = false
@@ -124,7 +124,7 @@ open class Task: NSObject {
124
124
}
125
125
126
126
managerThreadRunLoop? . run ( )
127
- fatalError ( " NSTask manager run loop exited unexpectedly; it should run forever once initialized" )
127
+ fatalError ( " Process manager run loop exited unexpectedly; it should run forever once initialized" )
128
128
}
129
129
thread. start ( )
130
130
managerThreadRunLoopIsRunningCondition. lock ( )
@@ -137,11 +137,11 @@ open class Task: NSObject {
137
137
}
138
138
}
139
139
140
- // Create an NSTask which can be run at a later time
141
- // An NSTask can only be run once. Subsequent attempts to
142
- // run an NSTask will raise.
143
- // Upon task death a notification will be sent
144
- // { Name = NSTaskDidTerminateNotification ; object = task ; }
140
+ // Create an Process which can be run at a later time
141
+ // An Process can only be run once. Subsequent attempts to
142
+ // run an Process will raise.
143
+ // Upon process death a notification will be sent
144
+ // { Name = ProcessDidTerminateNotification ; object = process ; }
145
145
//
146
146
147
147
public override init ( ) {
@@ -156,19 +156,19 @@ open class Task: NSObject {
156
156
open var currentDirectoryPath : String = FileManager . default. currentDirectoryPath
157
157
158
158
// standard I/O channels; could be either an NSFileHandle or an NSPipe
159
- open var standardInput : AnyObject ? {
159
+ open var standardInput : Any ? {
160
160
willSet {
161
161
precondition ( newValue is Pipe || newValue is FileHandle ,
162
162
" standardInput must be either NSPipe or NSFileHandle " )
163
163
}
164
164
}
165
- open var standardOutput : AnyObject ? {
165
+ open var standardOutput : Any ? {
166
166
willSet {
167
167
precondition ( newValue is Pipe || newValue is FileHandle ,
168
168
" standardOutput must be either NSPipe or NSFileHandle " )
169
169
}
170
170
}
171
- open var standardError : AnyObject ? {
171
+ open var standardError : Any ? {
172
172
willSet {
173
173
precondition ( newValue is Pipe || newValue is FileHandle ,
174
174
" standardError must be either NSPipe or NSFileHandle " )
@@ -189,7 +189,7 @@ open class Task: NSObject {
189
189
190
190
// Dispatch the manager thread if it isn't already running
191
191
192
- Task . setup ( )
192
+ Process . setup ( )
193
193
194
194
// Ensure that the launch path is set
195
195
@@ -251,14 +251,14 @@ open class Task: NSObject {
251
251
let socket = CFSocketCreateWithNative ( nil , taskSocketPair [ 0 ] , CFOptionFlags ( kCFSocketDataCallBack) , {
252
252
( socket, type, address, data, info ) in
253
253
254
- let task : Task = NSObject . unretainedReference ( info!)
254
+ let process : Process = NSObject . unretainedReference ( info!)
255
255
256
- task . processLaunchedCondition. lock ( )
257
- while task . running == false {
258
- task . processLaunchedCondition. wait ( )
256
+ process . processLaunchedCondition. lock ( )
257
+ while process . isRunning == false {
258
+ process . processLaunchedCondition. wait ( )
259
259
}
260
260
261
- task . processLaunchedCondition. unlock ( )
261
+ process . processLaunchedCondition. unlock ( )
262
262
263
263
var exitCode : Int32 = 0
264
264
#if CYGWIN
@@ -271,31 +271,31 @@ open class Task: NSObject {
271
271
272
272
repeat {
273
273
#if CYGWIN
274
- waitResult = waitpid ( task . processIdentifier, exitCodePtrWrapper, 0 )
274
+ waitResult = waitpid ( process . processIdentifier, exitCodePtrWrapper, 0 )
275
275
#else
276
- waitResult = waitpid ( task . processIdentifier, & exitCode, 0 )
276
+ waitResult = waitpid ( process . processIdentifier, & exitCode, 0 )
277
277
#endif
278
278
} while ( ( waitResult == - 1 ) && ( errno == EINTR) )
279
279
280
- task . terminationStatus = WEXITSTATUS ( exitCode )
280
+ process . terminationStatus = WEXITSTATUS ( exitCode )
281
281
282
282
// If a termination handler has been set, invoke it on a background thread
283
283
284
- if task . terminationHandler != nil {
284
+ if process . terminationHandler != nil {
285
285
let thread = Thread {
286
- task . terminationHandler!( task )
286
+ process . terminationHandler!( process )
287
287
}
288
288
thread. start ( )
289
289
}
290
290
291
291
// Set the running flag to false
292
292
293
- task . running = false
293
+ process . isRunning = false
294
294
295
295
// Invalidate the source and wake up the run loop, if it's available
296
296
297
- CFRunLoopSourceInvalidate ( task . runLoopSource)
298
- if let runLoop = task . runLoop {
297
+ CFRunLoopSourceInvalidate ( process . runLoopSource)
298
+ if let runLoop = process . runLoop {
299
299
CFRunLoopWakeUp ( runLoop. _cfRunLoop)
300
300
}
301
301
@@ -384,7 +384,7 @@ open class Task: NSObject {
384
384
retain: { return runLoopSourceRetain ( $0) } ,
385
385
release: { runLoopSourceRelease ( $0) } ,
386
386
copyDescription: nil ,
387
- equal: { return nstaskIsEqual ( $0, $1) } ,
387
+ equal: { return processIsEqual ( $0, $1) } ,
388
388
hash: nil ,
389
389
schedule: nil ,
390
390
cancel: nil ,
@@ -395,7 +395,7 @@ open class Task: NSObject {
395
395
runLoopContext. version = 0
396
396
runLoopContext. retain = runLoopSourceRetain
397
397
runLoopContext. release = runLoopSourceRelease
398
- runLoopContext. equal = nstaskIsEqual
398
+ runLoopContext. equal = processIsEqual
399
399
runLoopContext. perform = emptyRunLoopCallback
400
400
self . withUnretainedReference {
401
401
( refPtr: UnsafeMutablePointer < UInt8 > ) in
@@ -406,7 +406,7 @@ open class Task: NSObject {
406
406
self . runLoopSource = CFRunLoopSourceCreate ( kCFAllocatorDefault, 0 , & runLoopSourceContext!)
407
407
CFRunLoopAddSource ( CFRunLoopGetCurrent ( ) , runLoopSource, kCFRunLoopDefaultMode)
408
408
409
- running = true
409
+ isRunning = true
410
410
411
411
self . processIdentifier = pid
412
412
@@ -422,43 +422,46 @@ open class Task: NSObject {
422
422
423
423
// status
424
424
open private( set) var processIdentifier : Int32 = - 1
425
- open private( set) var running : Bool = false
425
+ open private( set) var isRunning : Bool = false
426
426
427
427
open private( set) var terminationStatus : Int32 = 0
428
428
open var terminationReason : TerminationReason { NSUnimplemented ( ) }
429
429
430
430
/*
431
- A block to be invoked when the process underlying the NSTask terminates. Setting the block to nil is valid, and stops the previous block from being invoked, as long as it hasn't started in any way. The NSTask is passed as the argument to the block so the block does not have to capture, and thus retain, it. The block is copied when set. Only one termination handler block can be set at any time. The execution context in which the block is invoked is undefined. If the NSTask has already finished, the block is executed immediately/soon (not necessarily on the current thread). If a terminationHandler is set on an NSTask , the NSTaskDidTerminateNotification notification is not posted for that task . Also note that -waitUntilExit won't wait until the terminationHandler has been fully executed. You cannot use this property in a concrete subclass of NSTask which hasn't been updated to include an implementation of the storage and use of it.
431
+ A block to be invoked when the process underlying the Process terminates. Setting the block to nil is valid, and stops the previous block from being invoked, as long as it hasn't started in any way. The Process is passed as the argument to the block so the block does not have to capture, and thus retain, it. The block is copied when set. Only one termination handler block can be set at any time. The execution context in which the block is invoked is undefined. If the Process has already finished, the block is executed immediately/soon (not necessarily on the current thread). If a terminationHandler is set on an Process , the ProcessDidTerminateNotification notification is not posted for that process . Also note that -waitUntilExit won't wait until the terminationHandler has been fully executed. You cannot use this property in a concrete subclass of Process which hasn't been updated to include an implementation of the storage and use of it.
432
432
*/
433
- open var terminationHandler : ( ( Task ) -> Void ) ?
434
- open var qualityOfService : NSQualityOfService = . default // read-only after the task is launched
433
+ open var terminationHandler : ( ( Process ) -> Void ) ?
434
+ open var qualityOfService : NSQualityOfService = . default // read-only after the process is launched
435
435
}
436
436
437
- extension Task {
437
+ extension Process {
438
438
439
439
// convenience; create and launch
440
- open class func launchedTaskWithLaunchPath ( _ path: String , arguments: [ String ] ) -> Task {
441
- let task = Task ( )
442
- task . launchPath = path
443
- task . arguments = arguments
444
- task . launch ( )
440
+ open class func launchedProcess ( launchPath path: String , arguments: [ String ] ) -> Process {
441
+ let process = Process ( )
442
+ process . launchPath = path
443
+ process . arguments = arguments
444
+ process . launch ( )
445
445
446
- return task
446
+ return process
447
447
}
448
448
449
- // poll the runLoop in defaultMode until task completes
449
+ // poll the runLoop in defaultMode until process completes
450
450
open func waitUntilExit( ) {
451
451
452
452
repeat {
453
453
454
- } while ( self . running == true && RunLoop . current. run ( mode: . defaultRunLoopMode, before: Date ( timeIntervalSinceNow: 0.05 ) ) )
454
+ } while ( self . isRunning == true && RunLoop . current. run ( mode: . defaultRunLoopMode, before: Date ( timeIntervalSinceNow: 0.05 ) ) )
455
455
456
456
self . runLoop = nil
457
457
}
458
458
}
459
459
460
- public let NSTaskDidTerminateNotification : String = " NSTaskDidTerminateNotification "
461
-
460
+ extension Process {
461
+
462
+ public static let didTerminateNotification = NSNotification . Name ( rawValue: " NSTaskDidTerminateNotification " )
463
+ }
464
+
462
465
private func posix( _ code: Int32 ) {
463
466
switch code {
464
467
case 0 : return
0 commit comments