An RxSwift implementation of a command line task runner.
Currently, RxTask does not support Linux. RxTask relies on some functionality
in Foundation
that is currently not available in the Linux port. This will be
re-evaluated after the Swift 3.1 release. PRs in this area are quite welcome! 👍
github "RxSwiftCommunity/RxTask"
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.Package(url: "https://github.com/RxSwiftCommunity/RxSwift.git", majorVersion: 0)
]
)
Creating a task is as simple as providing a launchPath
to the executable.
let task = Task(launchPath: "/bin/ls")
Optionally, you can provide arguments
, a workingDirectory
, and an
environment
.
let task = Task(launchPath: "/bin/echo", arguments: ["$MESSAGE"], environment: ["MESSAGE": "Hello World!"])
Task
s can be launched with the launch()
method. This produces a
self-contained process. This means the same task can be launch()
ed multiple
times producing separate processes.
The output of launch()
is a Observable<TaskEvent>
. TaskEvent
is an enum
that is used to report significant events in the task lifetime. The possible
events are:
launch(command: String)
stdOut(Data)
stdErr(Data)
exit(statusCode: Int)
Note: Currently an event is only considered successful if it exits with a
statusCode
of 0. Other exit statuses will be considered a TaskError
.
If you create a task that expects input, you can provide an Observable<Data>
for stdin
when you are launch()
ing the Task
. Data will be written to
stdin
as soon as it is emitted by the Observable
.
If you are only concerned with whether a Task
has completed successfully, you
can use the built-in operator justExitStatus()
.
Task(launchPath: "/bin/ls").launch()
.justExitStatus()
.subscribe(onNext: { exitStatus in /* ... */ })
.disposed(by: disposeBag)
Alternatively, if you are only interested in the output of a Task
, you can use
the operator justOutput()
. This will send the output of both stdout
and
stderr
.
Task(launchPath: "/bin/ls").launch()
.justOutput()
.subscribe(onNext: { output in /* ... */ })
.disposed(by: disposeBag)
TaskError
is an Error
that will be emitted under the following situations:
uncaughtSignal
: TheTask
terminated with an uncaught signal (e.g.SIGINT
).exit(statusCode: Int)
: TheTask
exited with a non-zero exit code.
Full docs can be found here.