cargo-auto - automation tasks written in Rust language for the build process of Rust projects
version: 2023.530.1223 date: 2023-05-30 author: bestia.dev repository: Github
Hashtags: #rustlang #tutorial #buildtool #developmenttool #cli
My projects on Github are more like a tutorial than a finished product: bestia-dev tutorials.
First, we will use
cargo-auto to create a new empty CLI Rust project similar to
cargo new, but with a more complete project structure.
# it lists all the prepared automation tasks # try a few
We can also add
automation tasks to an existing Rust project.
Inside your Rust project directory (the one with Cargo.toml or Cargo-auto.toml) run:
# it lists all the prepared automation tasks # try to build
Congratulations! You are already using
cargo-auto. Simple as that.
Now you can modify the tasks to your needs. It is all Rust language.
Cargo is a great tool for building Rust projects. It has all the basics:
cargo build --release,
But sometimes we need to do more things like copying some files, publish to ftp or enter long commands. These repetitive tasks must be automated.
Task automation makes work easier and faster, simplifies the workflow, while improving the consistency and accuracy of workflows.
This is also sometimes referred to as "workflow automation."
There are many different build systems and task runners there:
cmd prompt scripts, ...
Sadly there is no standard in the Rust community for now.
I want something similar to build.rs, so I can write my "tasks" in pure Rust I don't want to learn another meta language with weird syntax and difficult to debug. So I will make something really simple, easy, rusty and extensible.
cargo auto new_cli
I like very much that Rust has the command
cargo new project_name. It creates a super simple Rust hello project that can be build and run immediately. But this example is too simple. It lacks basic file structures of a serious CLI program.
I composed an opinionated template for a Rust CLI project. It is easy to run:
cargo auto new_wasm
I composed an opinionated template for a simple Rust WASM project for browser. It is very similar to the new_cli template, but for WASM.
It is easy to run:
# then # follow detailed instructions
cargo auto new_pwa_wasm
I composed an opinionated template for a simple Rust PWA-WASM project for browser. It is very similar to the new_cli template, but for WASM. It adds the PWA standard functionality to work as an offline app.
The template needs the title, name, long name and description inside a
pwa.json5 file and the
icon512x512.png file for the icons.
It is easy to run:
# on first run it will just create the `pwa.json5` and `icon512x512.png` files # modify these files for your new app # then # follow detailed instructions
scripting with rust
Rust is a compiled language. It is not really a scripting or interpreted language. But the compilation of small projects is really fast and can be ignored. Subsequent calls will use the already built binary and so the speed will be even faster.
cargo-auto is meant for Rust projects, so it means that all the Rust infrastructure is already in place.
automation_tasks_rs helper project
cargo auto new_auto will create a new directory
automation_tasks_rs with a template for a helper Rust project in the root directory of your
main Rust project . It should not interfere with the main Rust project. This directory will be added into git commits and pushed to remote repositories as part of the main project. It has its own
.gitignore to avoid committing its target directory.
automation_tasks_rs helper project contains user defined tasks in Rust code. Your tasks. This helper project should be opened in a new editor starting from the
automation_tasks_rs directory. It does not share dependencies with the main project. It is completely separate and independent.
You can edit it and add your dependencies and Rust code. No limits. Freedom of expression.
This is now your code, your tasks and your helper Rust project!
Because only you know what you want to automate and how to do it.
Never write secrets, passwords, passcodes or tokens inside your Rust code. Because then it is pushed to Github and the whole world can read it in the next second ! Basic example (most of the useful functions is already there):
/// match arguments and call tasks functions /// write a comprehensible help for user defined tasks // region: tasks /// cargo build /// cargo build --release /// cargo doc, then copies to /docs/ folder, because this is a github standard folder // endregion: tasks
cargo auto subcommand
cargo install cargo-auto will add a new subcommand to cargo:
This binary is super simple. It has only 1 trivial dependency:
The binary only reads the CLI arguments and runs the
automation_tasks_rs binary with them. If needed it will compile
The code-flow of the source code of
cargo-auto is simple, fully commented and straightforward to audit.
The source code is on GitHub with MIT open-source licensing.
With the help of the crate dev_bestia_cargo_completion the commands
cargo auto get bash auto-completion. Try it!
cargo auto new_auto
Inside the cargo-auto project there is a Rust sub-projects that is a template. I can open a new editor for this directories and build this crate independently. So it is easy to debug and develop.
Sadly, I cannot publish these directories and files to
crates.io. I can effectively publish only the source code inside my main Rust project
Therefor, before publishing I copy the content of these files into the modules
template_new_auto_mod.rs on every build. It is not difficult now that Rust has fantastic raw strings.
more complex tasks
You can write more complex tasks in Rust language.
For example in this project I use automation to create github Releases : https://github.com/bestia-dev/dropbox_backup_to_external_disk
Here is pretty complex workspace with more sub-projects:
There is no end to your imagination. If you write something that looks it can help other developers, please share it with me and I will add it here.
Usually I compile and run the code of
cargo-auto with added arguments like this:
new wasm, new wasm_pwa, new wasm_pwa_server, new wasm_pwa_server_pgrsql
cargo crev reviews and advisory
We live in times of danger with supply chain attacks.
It is recommended to always use cargo-crev
to verify the trustworthiness of each of your dependencies.
Please, spread this info.
You can also read reviews quickly on the web:
Open-source and free as a beer
My open-source projects are free as a beer (MIT license).
I just love programming.
But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my PayPal.
You know the price of a beer in your local bar ;-)
So I can drink a free beer for your health :-)
Na zdravje! Alla salute! Prost! Nazdravlje! 🍻