cargo_auto/main.rs
1// cargo-auto/src/main.rs
2
3// logo for docs.rs in png
4#![doc(html_logo_url = "https://github.com/automation-tasks-rs/cargo-auto/raw/main/images/logo/logo_cargo_auto.svg")]
5// region: auto_md_to_doc_comments include README.md A //!
6//! # cargo-auto
7//!
8//! **Automation tasks coded in Rust language for the workflow of Rust projects**
9//! ***version: 2025.814.1154 date: 2025-08-14 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)***
10//!
11//! 
12//! 
13//! 
14//! 
15//! 
16//!
17//! 
18//! cargo-auto is part of the [automation_tasks_rs](https://github.com/automation-tasks-rs) project
19//!
20//! [](https://crates.io/crates/cargo-auto)
21//! [](https://docs.rs/cargo-auto/)
22//! [](https://web.crev.dev/rust-reviews/crate/cargo-auto/)
23//! [](https://lib.rs/crates/cargo-auto/)
24//! [](https://github.com/automation-tasks-rs/cargo-auto/blob/master/LICENSE)
25//! [](https://github.com/automation-tasks-rs/cargo-auto/)
26//! [](https://automation-tasks-rs.github.io/cargo-auto/cargo_auto/index.html)
27//! 
28//!
29//! [](https://github.com/automation-tasks-rs/cargo-auto/)
30//! [](https://github.com/automation-tasks-rs/cargo-auto/)
31//! [](https://github.com/automation-tasks-rs/cargo-auto/)
32//! [](https://github.com/automation-tasks-rs/cargo-auto/)
33//! [](https://github.com/automation-tasks-rs/cargo-auto/)
34//!
35//! Hashtags: #maintained #ready-for-use #rustlang #automation #workflow
36//! My projects on GitHub are more like a tutorial than a finished product: [bestia-dev tutorials](https://github.com/bestia-dev/tutorials_rust_wasm).
37//! I recommend using the [CRUSTDE - Containerized Rust Development Environment](https://github.com/CRUSTDE-ContainerizedRustDevEnvrustde_cnt_img_pod) to write Rust projects on Linux, isolated from your system.
38//!
39//! ## Try it
40//!
41//! 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.
42//!
43//! ```bash
44//! cargo install cargo-auto
45//! cargo auto new_cli my_hello_project
46//! cd my_hello_project
47//! cargo auto
48//! # it lists all the prepared automation tasks
49//! # try a few
50//! cargo auto build
51//! cargo auto release
52//! cargo auto doc
53//! cargo auto test
54//! ```
55//!
56//! We can also add `automation tasks` to an existing Rust project.
57//! Inside your Rust project directory (the one with `Cargo.toml`) run:
58//!
59//! ```bash
60//! cargo auto new_auto_for_cli
61//! cargo auto
62//! # it lists all the prepared automation tasks
63//! # try to build
64//! cargo auto build
65//! ```
66//!
67//! Congratulations! You are already using `cargo-auto`. Simple as that.
68//! Now you can modify the tasks to your needs. It is all Rust language.
69//!
70//! ## Motivation
71//!
72//! Cargo is a great tool for building Rust projects. It has all the basics: `cargo build`, `cargo build --release`, `cargo fmt`, `cargo test`, `cargo doc`,...
73//! But sometimes we need to do more things like copying some files, publishing to FTP, or entering long commands. These repetitive tasks must be automated.
74//! Task automation makes work easier and faster, and simplifies the workflow while improving the consistency and accuracy of workflows.
75//! This is also sometimes referred to as "workflow automation."
76//! There are many different build systems and task runners there: `make`, `cmake`, `shell scripts`, `cargo-xtask`, `cargo-make`, `cargo-task`, `cargo-script`, `cargo-run-script`, `runner`, `python scripts`, `powershell scripts`, `cmd prompt scripts`, ...
77//! Sadly there is no standard in the Rust community for now.
78//! I want something similar to [build.rs](https://doc.rust-lang.org/cargo/reference/build-scripts.html), so I can write my "tasks" in pure Rust I don't want to learn another meta language with weird syntax and difficulty to debug. So I will make something really simple, easy, rusty, and extensible.
79//!
80//! ## cargo auto subcommand
81//!
82//! The command `cargo install cargo-auto` will add a new subcommand to cargo:
83//!
84//! ```bash
85//! cargo auto
86//! ```
87//!
88//! This binary is super simple. It has only 1 trivial dependency: `lazy_static`.
89//! The binary only reads the CLI arguments and runs the `automation_tasks_rs` binary with them. If needed it will compile `automation_tasks_rs` first.
90//! The code-flow of the source code of `cargo-auto` is simple, fully commented, and straightforward to audit.
91//! The source code is on [GitHub](https://github.com/automation-tasks-rs/cargo-auto) with MIT open-source licensing.
92//!
93//! ## bash auto-completion
94//!
95//! With the help of the crate [dev_bestia_cargo_completion](https://crates.io/crates/dev_bestia_cargo_completion), the commands `cargo` and `cargo auto` get bash auto-completion. Try it!
96//!
97//! ## cargo auto new_cli
98//!
99//! I like very much that Rust has the command `cargo new project_name`. It creates a super simple Rust Hello project that can be built and run immediately. But this example is too simple. It lacks the basic file structures of a serious CLI program.
100//! I composed an opinionated template for a Rust CLI project. It is easy to run:
101//!
102//! ```bash
103//! cargo auto new_cli project_name
104//! # then
105//! cd project_name
106//! cargo auto build
107//! # then follow detailed instructions
108//! ```
109//!
110//! ## cargo auto new_wasm
111//!
112//! I composed an opinionated template for a simple Rust WASM project for a browser. It is very similar to the new_cli template but for WASM.
113//! It is easy to run:
114//!
115//! ```bash
116//! cargo auto new_wasm project_name github_owner_or_organization web_server_domain server_username
117//! # then
118//! cd project_name
119//! cargo auto build
120//! # then follow detailed instructions
121//! ```
122//!
123//! ## cargo auto new_pwa_wasm
124//!
125//! I composed an opinionated template for a simple Rust PWA-WASM project for a 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.
126//! The template needs the `icon512x512.png` file for the icons. You can use the default icon if you don't have your own. 
127//! It is easy to run:
128//!
129//! ```bash
130//! curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png
131//! cargo auto new_pwa_wasm project_name github_owner_or_organization web_server_domain server_username
132//! # then
133//! cd project_name
134//! cargo auto build
135//! # then follow detailed instructions
136//! ```
137//!
138//! ## scripting with rust
139//!
140//! 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 so the speed will be even faster.
141//! This tool `cargo-auto` is meant for Rust projects, so it means that all the Rust infrastructure is already in place.
142//!
143//! ## automation_tasks_rs Rust sub-project
144//!
145//! The command `cargo auto new_auto_for_cli` will create a new Rust sub-project`automation_tasks_rs` inside your `Rust project`. It should not interfere with the main Rust project. This directory will be added to git commits and pushed to remote repositories as part of the main project. It has its own `.gitignore` to avoid committing to its target directory.
146//! The `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.
147//! You can edit it and add your dependencies and Rust code. No limits. Freedom of expression.
148//! This is now your code, your tasks, and your helper Rust project!
149//! Because only you know what you want to automate and how to do it.
150//! Never write secrets, passwords, passphrases, or tokens inside your Rust code. Because then it is pushed to GitHub and the whole world can read it in the next second!
151//! Basic example (most of the useful functions are already there):
152//!
153//! ```rust ignore
154//! /// match arguments and call tasks functions
155//! fn match_arguments_and_call_tasks(mut args: std::env::Args){
156//! // the first argument is the user defined task: (no argument for help), build, release,...
157//! let arg_1 = args.next();
158//! match arg_1 {
159//! None => print_help(),
160//! Some(task) => {
161//! println!("Running auto task: {}", &task);
162//! if &task == "build"{
163//! task_build();
164//! } else if &task == "release" {
165//! task_release();
166//! } else if &task == "doc" {
167//! task_doc();
168//! } else {
169//! println!("Task {} is unknown.", &task);
170//! print_help();
171//! }
172//! }
173//! }
174//! }
175//!
176//! /// write a comprehensible help for user defined tasks
177//! fn print_help() {
178//! println!(r#"
179//! User defined tasks in automation_tasks_rs:
180//! cargo auto build - builds the crate in debug mode
181//! cargo auto release - builds the crate in release mode
182//! cargo auto docs - builds the docs
183//! "#);
184//! }
185//!
186//! // region: tasks
187//!
188//! /// cargo build
189//! fn task_build() {
190//! run_shell_command("cargo fmt");
191//! run_shell_command("cargo build");
192//! }
193//!
194//! /// cargo build --release
195//! fn task_release() {
196//! run_shell_command("cargo fmt");
197//! run_shell_command("cargo build --release");
198//! }
199//!
200//! /// cargo doc, then copies to /docs/ folder, because this is a github standard folder
201//! fn task_doc() {
202//! run_shell_command("cargo doc --no-deps --document-private-items");
203//! // copy target/doc into docs/ because it is github standard
204//! run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/");
205//! // Create simple index.html file in docs directory
206//! run_shell_command(&format!(
207//! "printf \"<meta http-equiv=\\\"refresh\\\" content=\\\"0; url={}/index.html\\\" />\\n\" > docs/index.html",
208//! cargo_toml.package_name().replace("-","_")
209//! ));
210//! run_shell_command("cargo fmt");
211//! }
212//!
213//! // endregion: tasks
214//!
215//! ```
216//!
217//! ## more complex tasks
218//!
219//! You can write more complex tasks in Rust language.
220//! For example in this project I use automation to create GitHub Releases: <https://github.com/automation-tasks-rs/dropbox_backup_to_external_disk>
221//! Here is a pretty complex workspace with more sub-projects:
222//! <https://github.com/automation-tasks-rs/cargo_crev_reviews_workspace>
223//! There is no end to your imagination. If you write something that looks like it can help other developers, please share it with me and I will add it here.
224//!
225//! ## Development details
226//!
227//! Read the development details in a separate md file:
228//! [DEVELOPMENT.md](https://github.com/automation-tasks-rs/cargo-auto/blob/main/DEVELOPMENT.md)
229//!
230//! ## Releases changelog
231//!
232//! Read the changelog in a separate md file:
233//! [RELEASES.md](https://github.com/automation-tasks-rs/cargo-auto/blob/main/RELEASES.md)
234//!
235//! ## TODO
236//!
237//! Nothing big in the near future.
238//!
239//! ## Open-source and free as a beer
240//!
241//! My open-source projects are free as a beer (MIT license).
242//! I just love programming.
243//! But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my [PayPal](https://paypal.me/LucianoBestia).
244//! You know the price of a beer in your local bar ;-)
245//! So I can drink a free beer for your health :-)
246//! [Na zdravje!](https://translate.google.com/?hl=en&sl=sl&tl=en&text=Na%20zdravje&op=translate) [Alla salute!](https://dictionary.cambridge.org/dictionary/italian-english/alla-salute) [Prost!](https://dictionary.cambridge.org/dictionary/german-english/prost) [Nazdravlje!](https://matadornetwork.com/nights/how-to-say-cheers-in-50-languages/) 🍻
247//!
248//! [//bestia.dev](https://bestia.dev)
249//! [//github.com/automation-tasks-rs](https://github.com/automation-tasks-rs)
250//! [//bestiadev.substack.com](https://bestiadev.substack.com)
251//! [//youtube.com/@bestia-dev-tutorials](https://youtube.com/@bestia-dev-tutorials)
252//!
253// endregion: auto_md_to_doc_comments include README.md A //!
254
255mod bin_cli_functions_mod;
256use bin_cli_functions_mod as cli;
257
258use cargo_auto_main_lib as lib;
259// use cli::{BLUE, GREEN, RED, RESET, YELLOW};
260
261fn main() {
262 std::panic::set_hook(Box::new(cli::panic_set_hook));
263 cli::tracing_init();
264 // get CLI arguments
265 let mut args = std::env::args();
266 // the zero argument is the name of the program
267 let _arg_0 = args.next();
268
269 if lib::is_not_run_in_rust_project_root_directory() {
270 lib::outside_of_rust_project_mod::parse_args(&mut args);
271 } else {
272 lib::inside_of_rust_project_mod::parse_args(&mut args);
273 }
274}