Rust task runner and build tool.
The cargo-make task runner enables to define and configure sets of tasks and run them as a flow.
A task is a command or a script to execute.
Tasks can have dependencies which are also tasks that will be executed before the task itself.
With a simple toml based configuration file, you can define a multi platform build script that can run build, test, documentation generation, bench tests execution, security validations and more and executed by running a single command.
In order to install, just run the following command
sh
cargo install cargo-make
This will install cargo-make in your ~/.cargo/bin.
Make sure to add ~/.cargo/bin directory to your PATH variable.
When using cargo-make, all tasks are defined and configured via toml files.
Below are simple instructions to get you started off quickly.
In order to run a set of tasks, you first must define them in a toml file.
For example, if we would like to have a script which:
We will create a toml file as follows:
````toml [tasks.format] install_crate = "rustfmt" command = "cargo" args = ["fmt", "--", "--write-mode=overwrite"]
[tasks.clean] command = "cargo" args = ["clean"]
[tasks.build] command = "cargo" args = ["build"] dependencies = ["clean"]
[tasks.test] command = "cargo" args = ["test"] dependencies = ["clean"]
[tasks.my-flow] dependencies = [ "format", "build", "test" ] ````
We would execute the flow with the following command:
sh
cargo make --makefile simple-example.toml my-flow
The output would look something like this:
````console [cargo-make] info - Using Build File: simple-example.toml [cargo-make] info - Task: my-flow [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: format [cargo-make] info - Execute Command: "cargo" "fmt" "--" "--write-mode=overwrite" [cargo-make] info - Running Task: clean [cargo-make] info - Execute Command: "cargo" "clean" [cargo-make] info - Running Task: build [cargo-make] info - Execute Command: "cargo" "build" Compiling bitflags v0.9.1 Compiling unicode-width v0.1.4 Compiling quote v0.3.15 Compiling unicode-segmentation v1.1.0 Compiling strsim v0.6.0 Compiling libc v0.2.24 Compiling serde v1.0.8 Compiling vecmap v0.8.0 Compiling ansiterm v0.9.0 Compiling unicode-xid v0.0.4 Compiling synom v0.11.3 Compiling rand v0.3.15 Compiling termsize v0.3.0 Compiling atty v0.2.2 Compiling syn v0.11.11 Compiling textwrap v0.6.0 Compiling clap v2.25.0 Compiling serdederiveinternals v0.15.1 Compiling toml v0.4.2 Compiling serdederive v1.0.8 Compiling cargo-make v0.1.2 (file:///home/ubuntu/workspace) Finished dev [unoptimized + debuginfo] target(s) in 79.75 secs [cargo-make] info - Running Task: test [cargo-make] info - Execute Command: "cargo" "test" Compiling cargo-make v0.1.2 (file:///home/ubuntu/workspace) Finished dev [unoptimized + debuginfo] target(s) in 5.1 secs Running target/debug/deps/cargo_make-d5f8d30d73043ede
running 10 tests test log::tests::createinfo ... ok test log::tests::getlevelerror ... ok test log::tests::createverbose ... ok test log::tests::getlevelinfo ... ok test log::tests::getlevelother ... ok test log::tests::getlevelverbose ... ok test installer::tests::iscrateinstalledfalse ... ok test installer::tests::iscrateinstalledtrue ... ok test command::tests::validateexitcodeerror ... ok test log::tests::createerror ... ok
test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
[cargo-make] info - Running Task: my-flow [cargo-make] info - Build done in 72 seconds. ````
We now created a build script that can run on any platform.
In many cases, certain tasks depend on other tasks.
For example you would like to format the code before running build and run the build before running tests.
Such flow can be defined as follows:
````toml [tasks.format] install_crate = "rustfmt" command = "cargo" args = ["fmt", "--", "--write-mode=overwrite"]
[tasks.build] command = "cargo" args = ["build"] dependencies = ["format"]
[tasks.test] command = "cargo" args = ["test"] dependencies = ["build"] ````
When you run:
sh
cargo make --makefile ./my_build.toml test
It will try to run test, see that it has dependencies and those have other dependencies.
Therefore it will create an execution plan for the tasks based on the tasks and their dependencies.
In our case it will invoke format -> build -> test.
The same task will never be executed twice so if we have for example:
````toml [tasks.A] dependencies = ["B", "C"]
[tasks.B] dependencies = ["D"]
[tasks.C] dependencies = ["D"]
[tasks.D] script = [ "echo hello" ] ````
In this example, A depends on B and C, and both B and C are dependended on D.
Task D however will not be invoked twice.
The output of the execution will look something like this:
console
[cargo-make] info - Task: A
[cargo-make] info - Setting Up Env.
[cargo-make] info - Running Task: D
[cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/CNuU47tIix.sh"
hello
[cargo-make] info - Running Task: B
[cargo-make] info - Running Task: C
[cargo-make] info - Running Task: A
As you can see, 'hello' was printed once by task D as it was only invoked once.
But what if we want to run D twice?
Simple answer would be to duplicate task D and have B depend on D and C depend on D2 which is a copy of D.
But duplicating can lead to bugs and to huge makefiles, so we have alias for that.
An alias task has its own name and points to another task.
All of the definitions of the alias task are ignored.
So now, if we want to have D execute twice we can do the following:
````toml [tasks.A] dependencies = ["B", "C"]
[tasks.B] dependencies = ["D"]
[tasks.C] dependencies = ["D2"]
[tasks.D] script = [ "echo hello" ]
[tasks.D2] alias="D" ````
Now C depends on D2 and D2 is an alias for D.
Execution output of such make file would like as follows:
console
[cargo-make] info - Task: A
[cargo-make] info - Setting Up Env.
[cargo-make] info - Running Task: D
[cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/HP0UD7pgoX.sh"
hello
[cargo-make] info - Running Task: B
[cargo-make] info - Running Task: D2
[cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/TuuZJkqCE2.sh"
hello
[cargo-make] info - Running Task: C
[cargo-make] info - Running Task: A
Now you can see that 'hello' was printed twice.
It is also possible to define platform specific aliases, for example:
````toml [tasks.mytask] linuxalias = "linuxmytask" windowsalias = "windowsmytask" macalias = "macmytask"
[tasks.linuxmytask]
[tasks.macmytask]
[tasks.windowsmytask] ````
If platform specific alias is found and matches current platform it will take precedence over the non platform alias definition.
For example:
````toml [tasks.mytask] linuxalias = "run" alias = "do_nothing"
[tasks.run] script = [ "echo hello" ]
[tasks.do_nothing] ````
If you run task mytask on windows or mac, it will invoke the donothing task.
However, if executed on a linux platform, it will invoke the run task.
There is no real need to define the tasks that were shown in the previous example.
cargo-make comes with a built in toml file that will serve as a base for every execution.
The optional external toml file that is provided while running cargo-make will only extend and add or overwrite
tasks that are defined in the default toml.
Lets take the build task definition which comes alrady in the default toml:
toml
[tasks.build]
command = "cargo"
args = ["build"]
If for example, you would like to add verbose output to it, you would just need to change the args and add the --verbose as follows:
toml
[tasks.build]
args = ["build", "--verbose"]
If you want to disable some existing task (will not disable its dependencies), you can do it as follows:
toml
[tasks.build]
disabled = true
There is no need to redefine existing properties of the task, only what needs to be added or overwritten.
The default toml file comes with many steps and flows already built in, so it is worth to check it first.
You can also extend other external files from your external file by using the extend attribute, for example:
toml
extend = "my_common_makefile.toml"
The file path in the extend attribute is always relative to the current toml file you are in and not to the process working directory.
The extend attribute can be very usefull when you have a workspace with a Makefile.toml that contains all of the common custom tasks and in each project you can have a simple Makefile.toml which just has the extend attribute pointing to the workspace makefile.
In some cases you want to run optional tasks as part of a bigger flow, but do not want to break your entire build in case of any error in those optional tasks.
For those tasks, you can add the force=true attribute.
toml
[tasks.unstable_task]
force = true
In case you want to override a task or specific attributes in a task for specific platforms, you can define an override task with the platform name (currently linux, windows and mac) under the specific task.
For example:
````toml [tasks.hello-world] script = [ "echo \"Hello World From Unknown\"" ]
[tasks.hello-world.linux] script = [ "echo \"Hello World From Linux\"" ] ````
If you run cargo make with task 'hello-world' on linux, it would redirect to hello-world.linux while on other platforms it will execute the original hello-world.
In linux the output would be:
console
[cargo-make] info - Task: hello-world
[cargo-make] info - Setting Up Env.
[cargo-make] info - Running Task: hello-world
[cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/kOUJfw8Vfc.sh"
Hello World From Linux
[cargo-make] info - Build done in 0 seconds.
While on other platforms
console
[cargo-make] info - Task: hello-world
[cargo-make] info - Setting Up Env.
[cargo-make] info - Running Task: hello-world
[cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/2gYnulOJLP.sh"
Hello World From Unknown
[cargo-make] info - Build done in 0 seconds.
In the override task you can define any attribute that will override the attribute of the parent task, while undefined attributes will use the value from the parent task and will not be modified.
In case you need to delete attributes from the parent (for example script is only invoked if command is not defined and you have command defined in the parent task and script in the override task), then you will
have to clear the parent task in the override task using the clear attribute as follows:
toml
[tasks.hello-world.linux]
clear = true
script = [
"echo \"Hello World From Linux\""
]
This means, however, that you will have to redefine all attributes in the override task that you want to carry with you from the parent task.
Important - alias comes before checking override task so if parent task has an alias it will be redirected to that task instead of the override.
To override per platform, use the linuxalias, windowsalias, mac_alias attributes.
In addition, aliases can not be defined in platform override tasks, only in parent tasks.
You can also define env vars to be set as part of the execution of the flow in the env block, for examle:
yaml
[env]
RUST_BACKTRACE = "1"
All env vars defined in the env block and in the default toml will be defined before running the tasks.
In addition, cargo-make will also add few environment variables that can be helpful when running task scripts/commands:
The following environment variables will be set by cargo-make if Cargo.toml file exists and the relevant value is defined:
The following environment variables will be set by cargo-make if the project is part of a git repo:
cargo-make comes with a predefined flow for continuous integration build executed by internal or online services such as travis-ci and appveyor.
For travis-ci, simple change the script to invoke the cargo-make installation and invocation as follows:
yaml
script:
- cargo install --debug cargo-make
- cargo make ci-flow
For appveyor:
````yaml build: false
test_script: - cargo install --debug cargo-make - cargo make ci-flow ````
For online CI services, it is better to install with the debug flag to enable a much faster installation.
The default toml file comes with many predefined tasks and flows.
The following are some of the main flows that can be used without any need of an external Makefile.toml definition.
cargo make
).These are the following options available while running cargo-make:
````console USAGE: cargo make [FLAGS] [OPTIONS] [TASK]
FLAGS: -h, --help Prints help information --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) -V, --version Prints version information
OPTIONS:
--cwd
ARGS:
````rust
/// Holds the entire externally read configuration such as task definitions and env vars where all values are optional
pub struct ExternalConfig {
/// Path to another toml file to extend
pub extend: Option
/// Holds a single task configuration such as command and dependencies list
pub struct Task {
/// if true, the command/script of this task will not be invoked, dependencies however will be
pub disabled: Option
/// Holds a single task configuration for a specific platform as an override of another task
pub struct PlatformOverrideTask {
/// if true, it should ignore all data in base task
pub clear: Option
This section explains the logic behind the default task names.
While the default names logic can be used as a convention for any new task defined in some project Makefile.toml, it is not required.
The default toml file comes with three types of tasks:
cargo build
)Single command tasks are named based on their commmand (in most cases), for example the task that runs cargo build is named build.
toml
[tasks.build]
command = "cargo"
args = ["build"]
This allows to easily understand what this task does.
Tasks that are invoked before/after those tasks are named the same way as the original task but with the pre/post prefix.
For example for task build the default toml also defines pre-build and post-build tasks.
````toml [tasks.pre-build]
[tasks.post-build] ````
In the default toml, all pre/post tasks are empty and are there as placeholders for external Makefile.toml to override so custom functionality can be defined easily before/after running a specfific task.
Flows are named with the flow suffix, for example: ci-flow
````toml [tasks.ci-flow]
dependencies = [ "pre-build", "build-verbose", "post-build", "pre-test", "test-verbose", "post-test" ] ````
This prevents flow task names to conflict with single command task names and quickly allow users to understand that this task is a flow definition.
If you are using cargo-make in your project and want to display it in your project README or website, you can embed the "Built with cargo-make" badge.
Here are few snapshots:
md
[](https://sagiegurari.github.io/cargo-make)
html
<a href="https://sagiegurari.github.io/cargo-make">
<img src="https://img.shields.io/badge/built%20with-cargo--make-e49d41.svg" alt="Built with cargo-make">
</a>
The cargo-make task runner is still under heavy development.
You can view the future development items list in the project board
| Date | Version | Description | | ----------- | ------- | ----------- | | 2017-07-10 | v0.3.12 | cargo-make now defines rust version env vars | | 2017-07-09 | v0.3.11 | cargo-make now defines env vars based on project git repo information | | 2017-07-06 | v0.3.10 | cargo-make now defines env vars based on project Cargo.toml | | 2017-07-05 | v0.3.6 | Added --cwd cli arg to enable setting working directory | | 2017-07-04 | v0.3.5 | Added clippy task | | 2017-07-03 | v0.3.4 | Added --print-steps cli arg | | 2017-07-02 | v0.3.1 | Added CARGOMAKETASK env var holding the main task name | | 2017-07-02 | v0.3.0 | Renamed few cli options | | 2017-07-02 | v0.2.20 | Added -v and --verbose cli arg | | 2017-07-01 | v0.2.19 | Added extend config level attribute | | 2017-06-30 | v0.2.17 | Added force task attribute | | 2017-06-28 | v0.2.12 | Published website | | 2017-06-28 | v0.2.8 | Platform specific task override | | 2017-06-26 | v0.2.7 | Platform specific alias | | 2017-06-26 | v0.2.6 | Enable task attributes override | | 2017-06-25 | v0.2.3 | Added disabled task attribute support | | 2017-06-24 | v0.2.0 | Internal fixes (renamed dependencies attribute) | | 2017-06-24 | v0.1.2 | Print build time, added internal docs, unit tests and coverage | | 2017-06-24 | v0.1.1 | Added support for env vars, task alias and crate installation | | 2017-06-23 | v0.1.0 | Initial release. |
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.