From 114c2c2ed8924fef8c40579bb58735299597d51d Mon Sep 17 00:00:00 2001 From: luciano_bestia Date: Fri, 23 Feb 2024 11:43:01 -0500 Subject: [PATCH] 2024-02-23 --- automation_tasks_rs/src/main.rs | 115 +++++++++++++++++--------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/automation_tasks_rs/src/main.rs b/automation_tasks_rs/src/main.rs index ed50325..df6bbce 100644 --- a/automation_tasks_rs/src/main.rs +++ b/automation_tasks_rs/src/main.rs @@ -1,22 +1,19 @@ -//! automation_tasks_rs for rust_plantuml_client +// automation_tasks_rs for rust_plantuml_client -use cargo_auto_lib::*; -use cargo_auto_github_lib::*; +// region: library with basic automation tasks +use cargo_auto_lib as cl; +// traits must be in scope (Rust strangeness) +use cl::CargoTomlPublicApiMethods; -// ANSI colors for Linux terminal -// https://github.com/shiena/ansicolor/blob/master/README.md -#[allow(dead_code)] -pub const RED: &str = "\x1b[31m"; -#[allow(dead_code)] -pub const YELLOW: &str = "\x1b[33m"; -#[allow(dead_code)] -pub const GREEN: &str = "\x1b[32m"; -#[allow(dead_code)] -pub const RESET: &str = "\x1b[0m"; +use cargo_auto_lib::GREEN; +use cargo_auto_lib::RED; +use cargo_auto_lib::RESET; +use cargo_auto_lib::YELLOW; +// region: library with basic automation tasks fn main() { - exit_if_not_run_in_rust_project_root_directory(); + cl::exit_if_not_run_in_rust_project_root_directory(); // get CLI arguments let mut args = std::env::args(); @@ -78,7 +75,7 @@ fn print_help() { This task needs PAT (personal access token from github) in the env variable:{RESET} {GREEN} export GITHUB_TOKEN=paste_token_here{RESET} - {YELLOW}© 2023 bestia.dev MIT License github.com/bestia-dev/cargo-auto{RESET} + {YELLOW}© 2024 bestia.dev MIT License github.com/bestia-dev/cargo-auto{RESET} "# ); print_examples_cmd(); @@ -120,9 +117,9 @@ fn completion() { /// cargo build fn task_build() { let cargo_toml = CargoToml::read(); - auto_version_increment_semver_or_date(); - run_shell_command("cargo fmt"); - run_shell_command("cargo build"); + cl::auto_version_increment_semver_or_date(); + cl::run_shell_command("cargo fmt"); + cl::run_shell_command("cargo build"); println!( r#" {YELLOW}After `cargo auto build`, run the compiled binary, examples and/or tests{RESET} @@ -138,13 +135,13 @@ package_name = cargo_toml.package_name(), /// cargo build --release fn task_release() { let cargo_toml = CargoToml::read(); - auto_version_increment_semver_or_date(); - auto_cargo_toml_to_md(); - auto_lines_of_code(""); + cl::auto_version_increment_semver_or_date(); + cl::auto_cargo_toml_to_md(); + cl::auto_lines_of_code(""); - run_shell_command("cargo fmt"); - run_shell_command("cargo build --release"); - run_shell_command(&format!( + cl::run_shell_command("cargo fmt"); + cl::run_shell_command("cargo build --release"); + cl::run_shell_command(&format!( "strip target/release/{package_name}", package_name = cargo_toml.package_name() )); @@ -162,56 +159,68 @@ package_name = cargo_toml.package_name(), /// cargo doc, then copies to /docs/ folder, because this is a github standard folder fn task_doc() { - let cargo_toml = CargoToml::read(); - auto_cargo_toml_to_md(); - auto_lines_of_code(""); - // auto_plantuml(&cargo_toml.package_repository().unwrap()); - auto_md_to_doc_comments(); + let cargo_toml = cl::CargoToml::read(); + cl::auto_cargo_toml_to_md(); + cl::auto_lines_of_code(""); + cl::auto_plantuml(&cargo_toml.package_repository().unwrap()); + cl::auto_md_to_doc_comments(); - run_shell_command("cargo doc --no-deps --document-private-items"); + cl::run_shell_command("cargo doc --no-deps --document-private-items"); // copy target/doc into docs/ because it is github standard - run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/"); + cl::run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/"); // Create simple index.html file in docs directory - run_shell_command(&format!( - "echo \"\" > docs/index.html", - cargo_toml.package_name().replace("-","_") + cl::run_shell_command(&format!( + r#"echo "" > docs/index.html"#, + cargo_toml.package_name().replace("-", "_") )); - run_shell_command("cargo fmt"); + // pretty html + cl::auto_doc_tidy_html().unwrap(); + cl::run_shell_command("cargo fmt"); // message to help user with next move println!( r#" {YELLOW}After `cargo auto doc`, check `docs/index.html`. If ok then test the documentation code examples{RESET} {GREEN}cargo auto test{RESET} - "# +"# ); } /// cargo test fn task_test() { - run_shell_command("cargo test"); + cl::run_shell_command("cargo test"); println!( - r#" +r#" {YELLOW}After `cargo auto test`. If ok then {RESET} {GREEN}cargo auto commit_and_push "message"{RESET} {YELLOW}with mandatory commit message{RESET} -{GREEN}{RESET}"# +"# ); } /// commit and push fn task_commit_and_push(arg_2: Option) { - match arg_2 { - None => println!("{RED}Error: Message for commit is mandatory.{RESET}"), - Some(message) => { - run_shell_command(&format!(r#"git add -A && git commit --allow-empty -m "{}""#, message)); - run_shell_command("git push"); - println!( - r#" - {YELLOW}After `cargo auto commit_and_push "message"`{RESET} -{GREEN}cargo auto github_new_release{RESET} - "# - ); + let Some(message) = arg_2 else { + eprintln!("{RED}Error: Message for commit is mandatory. Exiting.{RESET}"); + // early exit + return; + }; + + // init repository if needed. If it is not init then normal commit and push. + if !cl::init_repository_if_needed(&message) { + // separate commit for docs if they changed, to not make a lot of noise in the real commit + if std::path::Path::new("docs").exists() { + cl::run_shell_command(r#"git add docs && git diff --staged --quiet || git commit -m "update docs" "#); } + cl::add_message_to_unreleased(&message); + // the real commit of code + cl::run_shell_command(&format!( r#"git add -A && git diff --staged --quiet || git commit -m "{message}" "#)); + cl::run_shell_command("git push"); + println!( +r#" + {YELLOW}After `cargo auto commit_and_push "message"`{RESET} +{GREEN}cargo auto publish_to_crates_io{RESET} +"# + ); } } @@ -225,7 +234,7 @@ fn task_github_new_release() { "git tag -f -a v{version} -m version_{version}", version = cargo_toml.package_version() ); - run_shell_command(&shell_command); + cl::run_shell_command(&shell_command); // async block inside sync code with tokio use tokio::runtime::Runtime; @@ -249,11 +258,11 @@ r#"## Changed // compress files tar.gz let tar_name = format!("{repo_name}-{tag_name_version}-x86_64-unknown-linux-gnu.tar.gz"); - run_shell_command(&format!("tar -zcvf {tar_name} target/release/{repo_name}")); + cl::run_shell_command(&format!("tar -zcvf {tar_name} target/release/{repo_name}")); // upload asset auto_github_upload_asset_to_release(&owner, &repo_name, &release_id, &tar_name).await; - run_shell_command(&format!("rm {tar_name}")); + cl::run_shell_command(&format!("rm {tar_name}")); println!(" {YELLOW}Asset uploaded. Open and edit the description on Github-Releases in the browser.{RESET}"); println!("{GREEN}https://github.com/{owner}/{repo_name}/releases{RESET}");