ReactiveTask
ReactiveTask 是一个 Swift 框架,用于启动 shell 任务(进程),使用 ReactiveSwift 构建。
let strings = [ "foo\n", "bar\n", "buzz\n", "fuzz\n" ]
let input = SignalProducer<Data, NoError>(values: strings.map { $0.data(using: .utf8)! })
let task = Task("/usr/bin/sort")
// Run the task, ignoring the output, and do something with the final result.
let result: Result<String, TaskError>? = task.launch(standardInput: input)
.ignoreTaskData()
.map { String(data: $0, encoding: .utf8) }
.ignoreNil()
.single()
print("Output of `\(task)`: \(result?.value ?? "")")
// Start the task and print all the events, which includes all the output
// that was received.
task.launch(standardInput: input)
.flatMapTaskEvents(.concat) { data in
return SignalProducer(value: String(data: data, encoding: .utf8))
}
.startWithNext { (event: TaskEvent) in
switch event {
case let .launch(task):
print("launched task: \(task)")
case let .standardError(data):
print("stderr: \(data)")
case let .standardOutput(data):
print("stdout: \(data)")
case let .success(string):
print("value: \(string ?? "")")
}
}
有关如何使用 ReactiveTask 的示例,请参阅 CarthageKit 框架中的 Xcode 和 Git 集成代码。
许可证
ReactiveTask 在 MIT 许可证 下发布。