Skip to content

Commit

Permalink
new_auto_for_cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bestia.dev committed May 4, 2024
1 parent 4bd0d18 commit 7c0bfd8
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 2,884 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-auto"
version = "2024.501.1827"
version = "2024.504.1703"
authors = ["bestia.dev"]
homepage = "https://bestia.dev"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ I am using the previous version of `cargo-auto` to develop the next version. I a

Inside the cargo-auto project, there are some Rust sub-projects that are templates. I can open a new editor for these directories and build these crates 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 `cargo-auto`.
Therefore, 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](https://doc.rust-lang.org/rust-by-example/std/str.html). For this repetitive task as always, I prepared an automation task in `automation_tasks_rs`.
Therefore, before publishing I copy the content of these files into the modules `template_new_auto_for_cli_mod.rs` on every build. It is not difficult now that Rust has fantastic [raw strings](https://doc.rust-lang.org/rust-by-example/std/str.html). For this repetitive task as always, I prepared an automation task in `automation_tasks_rs`.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[//]: # (auto_cargo_toml_to_md start)

**Automation tasks coded in Rust language for the workflow of Rust projects**
***version: 2024.501.1827 date: 2024-05-01 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)***
***version: 2024.504.1703 date: 2024-05-04 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)***

![maintained](https://img.shields.io/badge/maintained-green)
![ready-for-use](https://img.shields.io/badge/ready_for_use-green)
Expand All @@ -28,11 +28,11 @@
![cargo-auto](https://bestia.dev/webpage_hit_counter/get_svg_image/959103982.svg)

[//]: # (auto_lines_of_code start)
[![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-1299-green.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-742-blue.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-222-purple.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-743-green.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-574-blue.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-105-purple.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in examples](https://img.shields.io/badge/Lines_in_examples-0-yellow.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in tests](https://img.shields.io/badge/Lines_in_tests-549-orange.svg)](https://github.com/automation-tasks-rs/cargo-auto/)
[![Lines in tests](https://img.shields.io/badge/Lines_in_tests-0-orange.svg)](https://github.com/automation-tasks-rs/cargo-auto/)

[//]: # (auto_lines_of_code end)

Expand Down Expand Up @@ -61,7 +61,7 @@ We can also add `automation tasks` to an existing Rust project.
Inside your Rust project directory (the one with `Cargo.toml`) run:

```bash
cargo auto new_auto
cargo auto new_auto_for_cli
cargo auto
# it lists all the prepared automation tasks
# try to build
Expand Down Expand Up @@ -146,7 +146,7 @@ This tool `cargo-auto` is meant for Rust projects, so it means that all the Rust

## automation_tasks_rs Rust sub-project

The command `cargo auto new_auto` 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.
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.
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.
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!
Expand Down
64 changes: 36 additions & 28 deletions automation_tasks_rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,10 @@ fn completion() {

// endregion: match, help and completion

fn copy_files_into_module(){
let ext_for_binary_files = vec![".ico", ".jpg", ".png", ".woff2"];
let exclude_big_folders = vec!["/.git".to_string(), "/target".to_string(), "/docs".to_string(), "/pkg".to_string()];
cl::copy_folder_files_into_module(
std::path::Path::new("template_new_auto"),
std::path::Path::new("src/template_new_auto_mod.rs"),
&ext_for_binary_files,
&exclude_big_folders,
);

}
// region: tasks

/// cargo build
fn task_build() {
copy_files_into_module();
let cargo_toml = cl::CargoToml::read();
cl::auto_version_increment_semver_or_date();
cl::run_shell_command_static("cargo fmt").unwrap_or_else(|e| panic!("{e}"));
Expand All @@ -238,7 +226,6 @@ fn task_build() {

/// cargo build --release
fn task_release() {
copy_files_into_module();
let cargo_toml = cl::CargoToml::read();
cl::auto_version_increment_semver_or_date();
cl::auto_cargo_toml_to_md();
Expand All @@ -255,35 +242,56 @@ fn task_release() {
r#"
{YELLOW}After `cargo auto release`, run the compiled binary, examples and/or tests{RESET}
{YELLOW}1. Check if the template `new_cli` is working. Open a second terminal in VSCode and run:{RESET}
{GREEN}cd ~/rustprojects{RESET}
{YELLOW}1. Check if the template `new_cli` is working. Open a second VSCode:{RESET}
{GREEN}code ~/rustprojects{RESET}
{YELLOW}and run in its terminal:{RESET}
{GREEN}./cargo-auto/target/release/{package_name} new_cli hello_world bestia-dev;{RESET}
{GREEN}code hello_world{RESET}
{YELLOW} Then in the second VSCode window try the workflow: cargo auto build, cargo auto release, cargo auto doc,... all to the end.{RESET}
{YELLOW}If ok, close the second VSCode window. Back in the first VSCode window remove the temporary project:{RESET}
{YELLOW} In the new VSCode try the workflow: {RESET}
{GREEN}cargo auto build, cargo auto release, cargo auto doc,... {RESET}
{YELLOW}Follow the instructions, try all tasks.{RESET}
{YELLOW}Leave this VSCode open for the next check.{RESET}
{YELLOW}2. Check if the template `new_auto_for_cli` is working. Continue in the third VSCode:{RESET}
{GREEN}../cargo-auto/target/release/{package_name} new_auto_for_cli;{RESET}
{YELLOW}An Error must be produced because automation_tasks_rs already exists.{RESET}
{YELLOW}Remove the directory and try again:{RESET}
{GREEN}rm -r automation_tasks_rs{RESET}
{GREEN}../cargo-auto/target/release/{package_name} new_auto_for_cli;{RESET}
{YELLOW}Try the workflow: {RESET}
{GREEN}cargo auto build, cargo auto release, cargo auto doc,... {RESET}
{YELLOW}Follow the instructions, try all tasks.{RESET}
{YELLOW}If ok, close the third VSCode window. In the second VSCode remove the temporary project:{RESET}
{GREEN}rm -rf hello_world{RESET}
{YELLOW}Kill the second bash terminal from the first VSCode window.{RESET}
{YELLOW}Close the second VSCode.{RESET}
{YELLOW}2. Check if the template `new_wasm` is working. Open a second terminal in VSCode and run:{RESET}
{GREEN}cd ~/rustprojects{RESET}
{YELLOW}3. Check if the template `new_wasm` is working. Open a second VSCode:{RESET}
{GREEN}code ~/rustprojects{RESET}
{YELLOW}and run in its terminal:{RESET}
{GREEN}./cargo-auto/target/release/{package_name} new_wasm hello_world bestia-dev bestia.dev luciano_bestia;{RESET}
{GREEN}code hello_world{RESET}
{YELLOW} Then in the second VSCode window try the workflow: cargo auto build, cargo auto release, cargo auto doc,... all to the end.{RESET}
{YELLOW}If ok, close the second VSCode window. Back in the first VSCode window remove the temporary project:{RESET}
{YELLOW} In the new VSCode try the workflow: {RESET}
{GREEN}cargo auto build, cargo auto release, cargo auto doc,... {RESET}
{YELLOW}Follow the instructions, try all tasks.{RESET}
{YELLOW}If ok, close the third VSCode window. In the second VSCode remove the temporary project:{RESET}
{GREEN}rm -rf hello_world{RESET}
{YELLOW}Kill the second bash terminal from the first VSCode window.{RESET}
{YELLOW}Close the second VSCode.{RESET}
{YELLOW}3. Check if the template `new_pwa_wasm` is working. Open a second terminal in VSCode and run:{RESET}
{YELLOW}4. Check if the template `new_pwa_wasm` is working. Open a second VSCode:{RESET}
{GREEN}code ~/rustprojects{RESET}
{YELLOW}and run in its terminal:{RESET}
{YELLOW}Copy your 'icon512x512.png' into this folder or download and use this default icon.{RESET}
{GREEN}cd ~/rustprojects{RESET}
{GREEN}curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png{RESET}
{GREEN}./cargo-auto/target/release/{package_name} new_pwa_wasm hello_world bestia-dev bestia.dev luciano_bestia;{RESET}
{GREEN}code hello_world{RESET}
{YELLOW} Then in the second VSCode window try the workflow: cargo auto build, cargo auto release, cargo auto doc,... all to the end.{RESET}
{YELLOW}If ok, close the second VSCode window. Back in the first VSCode window remove the temporary project:{RESET}
{YELLOW} In the new VSCode try the workflow: {RESET}
{GREEN}cargo auto build, cargo auto release, cargo auto doc,... {RESET}
{YELLOW}Follow the instructions, try all tasks.{RESET}
{GREEN}rm icon512x512.png{RESET}
{GREEN}rm -rf hello_world{RESET}
{YELLOW}Kill the second bash terminal from the first VSCode window.{RESET}
{YELLOW}Close the second VSCode.{RESET}
{YELLOW}if ok then{RESET}
{GREEN}cargo auto doc{RESET}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cargo-auto/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! Inside your Rust project directory (the one with `Cargo.toml`) run:
//!
//! ```bash
//! cargo auto new_auto
//! cargo auto new_auto_for_cli
//! cargo auto
//! # it lists all the prepared automation tasks
//! # try to build
Expand Down Expand Up @@ -142,7 +142,7 @@
//!
//! ## automation_tasks_rs Rust sub-project
//!
//! The command `cargo auto new_auto` 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.
//! 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.
//! 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.
//! 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!
Expand Down
48 changes: 32 additions & 16 deletions src/inside_of_rust_project_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
use crate::{GREEN, RED, RESET, YELLOW};

pub fn parse_args(args: &mut std::env::Args) {
// the first argument is the task: (no argument for help), new_auto, build, release,...
// the first argument is the task: (no argument for help), new_auto_for_cli, ...
// wooow! There is a difference if I call the standalone binary or as a cargo subcommand:
// cargo-auto build - build is the arg_1
// cargo auto build - build is the arg_2
let arg_1 = args.next();
match arg_1 {
None => print_help_from_cargo_auto(),
Some(task) => {
if task != "auto" {
// when calling as `cargo auto build`
// when calling as `cargo auto new_auto_for_cli`
match_first_argument(&task, args);
} else {
// when calling as `cargo-auto build`
// when calling as `cargo-auto new_auto_for_cli`
let arg_2 = args.next();
match arg_2 {
None => print_help_from_cargo_auto(),
Expand All @@ -41,7 +39,7 @@ fn already_exists_automation_tasks_rs() -> bool {
/// if there is no argument then print help
/// if there exists `automation_tasks_rs/Cargo.toml` and `automation_tasks_rs/src/main.rs`
/// call automation_tasks_rs with no arguments to print the help prepared in user defined automation_tasks_rs
/// else print the help for `cargo auto new_auto`
/// else print the help for `cargo auto new_auto_for_cli`
/// in development use: `cargo run`
/// in runtime use: `cargo auto`
fn print_help_from_cargo_auto() {
Expand All @@ -52,35 +50,33 @@ fn print_help_from_cargo_auto() {
This program automates your custom tasks when developing a Rust project.
To start using `cargo auto` inside your Rust project, you must create a new `automation_tasks_rs` directory with the command:{RESET}
{GREEN}cargo auto new_auto{RESET}
{GREEN}cargo auto new_auto_for_cli{RESET}
{YELLOW}© 2024 bestia.dev MIT License github.com/automation-tasks-rs/cargo-auto{RESET}
"#
);
} else {
// panic! if cannot compile automation_tasks_rs
crate::template_new_auto_mod::compile_automation_tasks_rs_if_needed();
compile_automation_tasks_rs_if_needed();
let _success = crate::utils_mod::run_shell_command_success(&crate::PATH_TARGET_DEBUG_AUTOMATION_TASKS_RS.to_string_lossy());
}
}

/// the first argument is the task: new_auto, build, release,...
/// the first argument is the task: new_auto_for_cli,...
/// the task `new` is processed by `cargo-auto`,
/// all other tasks are processed by the used defined `automation_tasks_rs`
/// in development use: `cargo run -- new_auto`
/// in development use: `cargo run -- build`
/// in development use: `cargo run -- release`
/// in development use: `cargo run -- new_auto_for_cli`
fn match_first_argument(task: &str, args: &mut std::env::Args) {
if task == "completion" {
completion();
} else if task == "new_auto" {
} else if task == "new_auto_for_cli" {
// this task is inside cargo-auto
if already_exists_automation_tasks_rs() {
println!("{RED}Error: Directory automation_tasks_rs already exists. Cannot create new directory automation_tasks_rs.{RESET}");
// early exit
std::process::exit(0);
}
crate::template_new_auto_mod::new_auto();
crate::template_new_auto_for_cli_mod::new_auto_for_cli();
} else {
// these tasks are user defined in automation_tasks_rs
if !already_exists_automation_tasks_rs() {
Expand All @@ -90,7 +86,7 @@ fn match_first_argument(task: &str, args: &mut std::env::Args) {
std::process::exit(0);
}
// panic! if cannot compile automation_tasks_rs
crate::template_new_auto_mod::compile_automation_tasks_rs_if_needed();
compile_automation_tasks_rs_if_needed();
// call automation_tasks_rs/target/debug/automation_tasks_rs with all the arguments
let mut command = std::process::Command::new(crate::PATH_TARGET_DEBUG_AUTOMATION_TASKS_RS.as_os_str());
command.arg(&task);
Expand Down Expand Up @@ -128,7 +124,27 @@ fn completion() {
word_being_completed = args[3].as_str();
}
if last_word == "cargo-auto" || last_word == "auto" {
let sub_commands = vec!["new_auto"];
let sub_commands = vec!["new_auto_for_cli"];
completion_return_one_or_more_sub_commands(sub_commands, word_being_completed);
}
}

/// build if the files are different then the hashes in automation_tasks_rs/file_hashes.json
/// panic! if cannot compile automation_tasks_rs
pub fn compile_automation_tasks_rs_if_needed() {
if !crate::PATH_TARGET_DEBUG_AUTOMATION_TASKS_RS.exists() || crate::file_hashes_mod::is_project_changed() {
compile_project_automation_tasks_rs();
let vec_of_metadata = crate::file_hashes_mod::read_file_metadata();
crate::file_hashes_mod::save_json_file_for_file_meta_data(vec_of_metadata);
}
}

/// build automation_tasks_rs
/// panic! if cannot compile automation_tasks_rs
pub fn compile_project_automation_tasks_rs() {
// build in other directory (not in working current directory)
// cargo build --manifest-path=dir/Cargo.toml
if !crate::utils_mod::run_shell_command_success("cargo build --manifest-path=automation_tasks_rs/Cargo.toml") {
panic!("{RED}Cannot compile automation_tasks_rs. Exiting...{RESET}");
}
}
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! Inside your Rust project directory (the one with `Cargo.toml`) run:
//!
//! ```bash
//! cargo auto new_auto
//! cargo auto new_auto_for_cli
//! cargo auto
//! # it lists all the prepared automation tasks
//! # try to build
Expand Down Expand Up @@ -142,7 +142,7 @@
//!
//! ## automation_tasks_rs Rust sub-project
//!
//! The command `cargo auto new_auto` 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.
//! 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.
//! 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.
//! 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!
Expand Down Expand Up @@ -255,7 +255,7 @@
mod file_hashes_mod;
pub mod inside_of_rust_project_mod;
pub mod outside_of_rust_project_mod;
mod template_new_auto_mod;
mod template_new_auto_for_cli_mod;
mod template_new_cli_mod;
mod template_new_pwa_wasm_mod;
mod template_new_wasm_mod;
Expand Down Expand Up @@ -299,12 +299,6 @@ pub const GREEN: &str = "\x1b[32m";
pub const RESET: &str = "\x1b[0m";
// endregion: Public API constants

/// FileItem to copy text file into String for generators
pub struct FileItem {
file_name: &'static str,
file_content: &'static str,
}

/// Check if is not run in Rust project root directory
pub fn is_not_run_in_rust_project_root_directory() -> bool {
// return negation of exists
Expand Down
Loading

0 comments on commit 7c0bfd8

Please sign in to comment.