Skip to content

Commit 7ab9f64

Browse files
committed
Merge pull request #42 from Carthage/send-launch-event
Send `.Launch` event when the task is launched
2 parents 93f4f91 + 05aeafb commit 7ab9f64

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

ReactiveTask/Task.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public protocol TaskEventType {
247247
/// Represents events that can occur during the execution of a task that is
248248
/// expected to terminate with a result of type T (upon success).
249249
public enum TaskEvent<T>: TaskEventType {
250-
/// The task was launched.
250+
/// The task is about to be launched.
251251
case Launch(Task)
252252

253253
/// Some data arrived from the task on `stdout`.
@@ -377,35 +377,35 @@ extension Signal where Value: TaskEventType {
377377
/// Launches a new shell task.
378378
///
379379
/// - Parameters:
380-
/// - taskDescription: The task to launch.
381-
/// - standardInput: Data to stream to standard input of the launched process. If nil, stdin will
382-
/// be inherited from the parent process.
380+
/// - task: The task to launch.
381+
/// - standardInput: Data to stream to standard input of the launched process. If nil, stdin will
382+
/// be inherited from the parent process.
383383
///
384384
/// - Returns: A producer that will launch the task when started, then send
385385
/// `TaskEvent`s as execution proceeds.
386-
public func launchTask(taskDescription: Task, standardInput: SignalProducer<NSData, NoError>? = nil) -> SignalProducer<TaskEvent<NSData>, TaskError> {
386+
public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError>? = nil) -> SignalProducer<TaskEvent<NSData>, TaskError> {
387387
return SignalProducer { observer, disposable in
388-
let queue = dispatch_queue_create(taskDescription.description, DISPATCH_QUEUE_SERIAL)
388+
let queue = dispatch_queue_create(task.description, DISPATCH_QUEUE_SERIAL)
389389
let group = Task.group
390390

391-
let task = NSTask()
392-
task.launchPath = taskDescription.launchPath
393-
task.arguments = taskDescription.arguments
391+
let rawTask = NSTask()
392+
rawTask.launchPath = task.launchPath
393+
rawTask.arguments = task.arguments
394394

395-
if let cwd = taskDescription.workingDirectoryPath {
396-
task.currentDirectoryPath = cwd
395+
if let cwd = task.workingDirectoryPath {
396+
rawTask.currentDirectoryPath = cwd
397397
}
398398

399-
if let env = taskDescription.environment {
400-
task.environment = env
399+
if let env = task.environment {
400+
rawTask.environment = env
401401
}
402402

403403
var stdinProducer: SignalProducer<(), TaskError> = .empty
404404

405405
if let input = standardInput {
406406
switch Pipe.create(queue, group) {
407407
case let .Success(pipe):
408-
task.standardInput = pipe.readHandle
408+
rawTask.standardInput = pipe.readHandle
409409

410410
stdinProducer = pipe.writeDataFromProducer(input).on(started: {
411411
close(pipe.readFD)
@@ -460,11 +460,11 @@ public func launchTask(taskDescription: Task, standardInput: SignalProducer<NSDa
460460
))
461461
}
462462

463-
task.standardOutput = stdoutPipe.writeHandle
464-
task.standardError = stderrPipe.writeHandle
463+
rawTask.standardOutput = stdoutPipe.writeHandle
464+
rawTask.standardError = stderrPipe.writeHandle
465465

466466
dispatch_group_enter(group)
467-
task.terminationHandler = { task in
467+
rawTask.terminationHandler = { task in
468468
let terminationStatus = task.terminationStatus
469469
if terminationStatus == EXIT_SUCCESS {
470470
// Wait for stderr to finish, then pass
@@ -486,8 +486,9 @@ public func launchTask(taskDescription: Task, standardInput: SignalProducer<NSDa
486486
}
487487
dispatch_group_leave(group)
488488
}
489-
490-
task.launch()
489+
490+
observer.sendNext(.Launch(task))
491+
rawTask.launch()
491492
close(stdoutPipe.writeFD)
492493
close(stderrPipe.writeFD)
493494

@@ -496,7 +497,7 @@ public func launchTask(taskDescription: Task, standardInput: SignalProducer<NSDa
496497
}
497498

498499
disposable.addDisposable {
499-
task.terminate()
500+
rawTask.terminate()
500501
}
501502
}
502503
}

ReactiveTaskTests/TaskSpec.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ import Result
1515

1616
class TaskSpec: QuickSpec {
1717
override func spec() {
18+
it("should notify that a task is about to be launched") {
19+
var isLaunched: Bool = false
20+
21+
let task = Task("/usr/bin/true")
22+
let result = launchTask(task)
23+
.on(next: { event in
24+
if case let .Launch(launched) = event {
25+
isLaunched = true
26+
expect(launched) == task
27+
}
28+
})
29+
.wait()
30+
31+
expect(result.error).to(beNil())
32+
expect(isLaunched) == true
33+
}
34+
1835
it("should launch a task that writes to stdout") {
1936
let result = launchTask(Task("/bin/echo", arguments: [ "foobar" ]))
2037
.reduce(NSMutableData()) { aggregated, event in

0 commit comments

Comments
 (0)