@@ -16,7 +16,7 @@ import CoreFoundation
1616 import Glibc
1717#endif
1818
19- extension Task {
19+ extension Process {
2020 public enum TerminationReason : Int {
2121 case exit
2222 case uncaughtSignal
@@ -72,27 +72,27 @@ private func runloopIsEqual(_ a : UnsafeRawPointer?, _ b : UnsafeRawPointer?) ->
7272}
7373
7474
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 {
7777
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 {
8080 return false
8181 }
8282
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 {
8585 return false
8686 }
8787
88- guard taskA == taskB else {
88+ guard processA == processB else {
8989 return false
9090 }
9191
9292 return true
9393}
9494
95- open class Task : NSObject {
95+ open class Process : NSObject {
9696 private static func setup( ) {
9797 struct Once {
9898 static var done = false
@@ -124,7 +124,7 @@ open class Task: NSObject {
124124 }
125125
126126 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" )
128128 }
129129 thread. start ( )
130130 managerThreadRunLoopIsRunningCondition. lock ( )
@@ -137,11 +137,11 @@ open class Task: NSObject {
137137 }
138138 }
139139
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 ; }
145145 //
146146
147147 public override init ( ) {
@@ -156,19 +156,19 @@ open class Task: NSObject {
156156 open var currentDirectoryPath : String = FileManager . default. currentDirectoryPath
157157
158158 // standard I/O channels; could be either an NSFileHandle or an NSPipe
159- open var standardInput : AnyObject ? {
159+ open var standardInput : Any ? {
160160 willSet {
161161 precondition ( newValue is Pipe || newValue is FileHandle ,
162162 " standardInput must be either NSPipe or NSFileHandle " )
163163 }
164164 }
165- open var standardOutput : AnyObject ? {
165+ open var standardOutput : Any ? {
166166 willSet {
167167 precondition ( newValue is Pipe || newValue is FileHandle ,
168168 " standardOutput must be either NSPipe or NSFileHandle " )
169169 }
170170 }
171- open var standardError : AnyObject ? {
171+ open var standardError : Any ? {
172172 willSet {
173173 precondition ( newValue is Pipe || newValue is FileHandle ,
174174 " standardError must be either NSPipe or NSFileHandle " )
@@ -189,7 +189,7 @@ open class Task: NSObject {
189189
190190 // Dispatch the manager thread if it isn't already running
191191
192- Task . setup ( )
192+ Process . setup ( )
193193
194194 // Ensure that the launch path is set
195195
@@ -251,14 +251,14 @@ open class Task: NSObject {
251251 let socket = CFSocketCreateWithNative ( nil , taskSocketPair [ 0 ] , CFOptionFlags ( kCFSocketDataCallBack) , {
252252 ( socket, type, address, data, info ) in
253253
254- let task : Task = NSObject . unretainedReference ( info!)
254+ let process : Process = NSObject . unretainedReference ( info!)
255255
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 ( )
259259 }
260260
261- task . processLaunchedCondition. unlock ( )
261+ process . processLaunchedCondition. unlock ( )
262262
263263 var exitCode : Int32 = 0
264264#if CYGWIN
@@ -271,31 +271,31 @@ open class Task: NSObject {
271271
272272 repeat {
273273#if CYGWIN
274- waitResult = waitpid ( task . processIdentifier, exitCodePtrWrapper, 0 )
274+ waitResult = waitpid ( process . processIdentifier, exitCodePtrWrapper, 0 )
275275#else
276- waitResult = waitpid ( task . processIdentifier, & exitCode, 0 )
276+ waitResult = waitpid ( process . processIdentifier, & exitCode, 0 )
277277#endif
278278 } while ( ( waitResult == - 1 ) && ( errno == EINTR) )
279279
280- task . terminationStatus = WEXITSTATUS ( exitCode )
280+ process . terminationStatus = WEXITSTATUS ( exitCode )
281281
282282 // If a termination handler has been set, invoke it on a background thread
283283
284- if task . terminationHandler != nil {
284+ if process . terminationHandler != nil {
285285 let thread = Thread {
286- task . terminationHandler!( task )
286+ process . terminationHandler!( process )
287287 }
288288 thread. start ( )
289289 }
290290
291291 // Set the running flag to false
292292
293- task . running = false
293+ process . isRunning = false
294294
295295 // Invalidate the source and wake up the run loop, if it's available
296296
297- CFRunLoopSourceInvalidate ( task . runLoopSource)
298- if let runLoop = task . runLoop {
297+ CFRunLoopSourceInvalidate ( process . runLoopSource)
298+ if let runLoop = process . runLoop {
299299 CFRunLoopWakeUp ( runLoop. _cfRunLoop)
300300 }
301301
@@ -384,7 +384,7 @@ open class Task: NSObject {
384384 retain: { return runLoopSourceRetain ( $0) } ,
385385 release: { runLoopSourceRelease ( $0) } ,
386386 copyDescription: nil ,
387- equal: { return nstaskIsEqual ( $0, $1) } ,
387+ equal: { return processIsEqual ( $0, $1) } ,
388388 hash: nil ,
389389 schedule: nil ,
390390 cancel: nil ,
@@ -395,7 +395,7 @@ open class Task: NSObject {
395395 runLoopContext. version = 0
396396 runLoopContext. retain = runLoopSourceRetain
397397 runLoopContext. release = runLoopSourceRelease
398- runLoopContext. equal = nstaskIsEqual
398+ runLoopContext. equal = processIsEqual
399399 runLoopContext. perform = emptyRunLoopCallback
400400 self . withUnretainedReference {
401401 ( refPtr: UnsafeMutablePointer < UInt8 > ) in
@@ -406,7 +406,7 @@ open class Task: NSObject {
406406 self . runLoopSource = CFRunLoopSourceCreate ( kCFAllocatorDefault, 0 , & runLoopSourceContext!)
407407 CFRunLoopAddSource ( CFRunLoopGetCurrent ( ) , runLoopSource, kCFRunLoopDefaultMode)
408408
409- running = true
409+ isRunning = true
410410
411411 self . processIdentifier = pid
412412
@@ -422,43 +422,46 @@ open class Task: NSObject {
422422
423423 // status
424424 open private( set) var processIdentifier : Int32 = - 1
425- open private( set) var running : Bool = false
425+ open private( set) var isRunning : Bool = false
426426
427427 open private( set) var terminationStatus : Int32 = 0
428428 open var terminationReason : TerminationReason { NSUnimplemented ( ) }
429429
430430 /*
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.
432432 */
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
435435}
436436
437- extension Task {
437+ extension Process {
438438
439439 // 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 ( )
445445
446- return task
446+ return process
447447 }
448448
449- // poll the runLoop in defaultMode until task completes
449+ // poll the runLoop in defaultMode until process completes
450450 open func waitUntilExit( ) {
451451
452452 repeat {
453453
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 ) ) )
455455
456456 self . runLoop = nil
457457 }
458458}
459459
460- public let NSTaskDidTerminateNotification : String = " NSTaskDidTerminateNotification "
461-
460+ extension Process {
461+
462+ public static let didTerminateNotification = NSNotification . Name ( rawValue: " NSTaskDidTerminateNotification " )
463+ }
464+
462465private func posix( _ code: Int32 ) {
463466 switch code {
464467 case 0 : return
0 commit comments