Skip to content

Commit 566811b

Browse files
committed
Add a code sample to the readme
1 parent 645c93e commit 566811b

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
# ReactiveTask
2-
32
ReactiveTask is a Swift framework for launching shell tasks (processes), built using [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa).
43

4+
```swift
5+
let strings = [ "foo\n", "bar\n", "buzz\n", "fuzz\n" ]
6+
let input = SignalProducer<NSData, NoError>(values: strings.map { $0.dataUsingEncoding(NSUTF8StringEncoding)! })
7+
let task = Task("/usr/bin/sort")
8+
9+
// Run the task, ignoring the output, and do something with the final result.
10+
let result: Result<String, TaskError>? = launchTask(task, standardInput: input)
11+
.ignoreTaskData()
12+
.map { String(data: $0, encoding: NSUTF8StringEncoding) }
13+
.ignoreNil()
14+
.single()
15+
print("Output of `\(task)`: \(result?.value ?? "")")
16+
17+
// Start the task and print all the events, which includes all the output
18+
// that was received.
19+
launchTask(task, standardInput: input)
20+
.flatMapTaskEvents(.Concat) { data in
21+
return SignalProducer(value: String(data: data, encoding: NSUTF8StringEncoding))
22+
}
23+
.startWithNext { (event: TaskEvent) in
24+
switch event {
25+
case let .Launch(task):
26+
print("launched task: \(task)")
27+
28+
case let .StandardError(data):
29+
print("stderr: \(data)")
30+
31+
case let .StandardOutput(data):
32+
print("stdout: \(data)")
33+
34+
case let .Success(string):
35+
print("value: \(string)")
36+
}
37+
}
38+
```
39+
540
For examples of how to use ReactiveTask, see the Xcode and Git integration code from the [CarthageKit](https://github.com/Carthage/Carthage) framework.
641

742
## License
8-
943
ReactiveTask is released under the [MIT license](LICENSE.md).

0 commit comments

Comments
 (0)