diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 0000000..60fd8e4 --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,4 @@ +{ + "line-length": false, + "MD041": false +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 8eeaa07..02e4514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-auto" -version = "2024.501.223" +version = "2024.501.1827" authors = ["bestia.dev"] homepage = "https://bestia.dev" edition = "2021" diff --git a/README.md b/README.md index 7301d8a..e453507 100644 --- a/README.md +++ b/README.md @@ -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.223 date: 2024-05-01 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)*** +***version: 2024.501.1827 date: 2024-05-01 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) @@ -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-1949-green.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -[![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-971-blue.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -[![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-433-purple.svg)](https://github.com/automation-tasks-rs/cargo-auto/) +[![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 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-8881-orange.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/) [//]: # (auto_lines_of_code end) @@ -58,7 +58,7 @@ cargo auto test ``` We can also add `automation tasks` to an existing Rust project. -Inside your Rust project directory (the one with Cargo.toml) run: +Inside your Rust project directory (the one with `Cargo.toml`) run: ```bash cargo auto new_auto @@ -117,7 +117,7 @@ I composed an opinionated template for a simple Rust WASM project for a browser. It is easy to run: ```bash -cargo auto new_wasm project_name +cargo auto new_wasm project_name github_owner_or_organization web_server_domain server_username # then cd project_name cargo auto build @@ -127,14 +127,12 @@ cargo auto build ## cargo auto new_pwa_wasm 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. -The template needs the title, name, long name, and description inside a `pwa.json5` file and the `icon512x512.png` file for the icons. +The template needs the `icon512x512.png` file for the icons. You can use the default icon if you don't have your own. ![icon512x512.png](https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png) It is easy to run: ```bash -cargo auto new_pwa_wasm -# on first run it will just create the `pwa.json5` and `icon512x512.png` files -# modify these files with data for your new app and then repeat -cargo auto new_pwa_wasm +curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png +cargo auto new_pwa_wasm project_name github_owner_or_organization web_server_domain server_username # then cd project_name cargo auto build diff --git a/RELEASES.md b/RELEASES.md index 10245c1..7f9235d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -12,6 +12,8 @@ The TODO section is part of the [README.md](https://github.com/automation-tasks- - new_cli +- templates from GitHub + ## Version 2024.422.214 (2024-04-22) - allow dead code diff --git a/automation_tasks_rs/src/main.rs b/automation_tasks_rs/src/main.rs index 1f3ef18..825cc84 100644 --- a/automation_tasks_rs/src/main.rs +++ b/automation_tasks_rs/src/main.rs @@ -255,31 +255,35 @@ 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 new terminal in VSCode and run:{RESET} + {YELLOW}1. Check if the template `new_cli` is working. Open a second terminal in VSCode and run:{RESET} {GREEN}cd ~/rustprojects{RESET} {GREEN}./cargo-auto/target/release/{package_name} new_cli hello_world bestia-dev;{RESET} {GREEN}code hello_world{RESET} - {YELLOW}In the new VSCODE window terminal, first change in Cargo.toml/repository from "github_owner" to your github username.{RESET} - {YELLOW} Then try the workflow: cargo auto build, cargo auto release, cargo auto doc,... all to the end.{RESET} - {YELLOW}If ok, close the VSCode window. Back in the first terminal check the next template:{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} {GREEN}rm -rf hello_world{RESET} + {YELLOW}Kill the second bash terminal from the first VSCode window.{RESET} - {YELLOW}2. Check if the template `new_wasm` is working. Open a new terminal in VSCode and run:{RESET} + {YELLOW}2. Check if the template `new_wasm` is working. Open a second terminal in VSCode and run:{RESET} {GREEN}cd ~/rustprojects{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}In the new VSCODE window terminal, first change in Cargo.toml/repository from "github_owner" to your github username.{RESET} - {YELLOW} Then try the workflow: cargo auto build, cargo auto release, cargo auto doc,... all to the end.{RESET} - {YELLOW}If ok, close the VSCode window.{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} {GREEN}rm -rf hello_world{RESET} + {YELLOW}Kill the second bash terminal from the first VSCode window.{RESET} - {YELLOW}3. Check if the template `new_pwa_wasm` is working. Open a new terminal in VSCode and run:{RESET} + {YELLOW}3. Check if the template `new_pwa_wasm` is working. Open a second terminal in VSCode and run:{RESET} + {YELLOW}Copy your 'icon512x512.png' into this folder or download and use this default icon.{RESET} {GREEN}cd ~/rustprojects{RESET} -{GREEN}./cargo-auto/target/release/{package_name} new_pwa_wasm{RESET} - {YELLOW}Follow the instructions{RESET} - - {YELLOW}If ok, close the VSCode window.{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} +{GREEN}rm icon512x512.png{RESET} {GREEN}rm -rf hello_world{RESET} + {YELLOW}Kill the second bash terminal from the first VSCode window.{RESET} {YELLOW}if ok then{RESET} {GREEN}cargo auto doc{RESET} diff --git a/src/bin/cargo-auto/main.rs b/src/bin/cargo-auto/main.rs index a33b163..6f19d9c 100644 --- a/src/bin/cargo-auto/main.rs +++ b/src/bin/cargo-auto/main.rs @@ -6,7 +6,7 @@ //! # cargo-auto //! //! **Automation tasks coded in Rust language for the workflow of Rust projects** -//! ***version: 2024.501.55 date: 2024-05-01 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)*** +//! ***version: 2024.501.1827 date: 2024-05-01 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) @@ -26,11 +26,11 @@ //! [![Newest docs](https://img.shields.io/badge/newest_docs-blue.svg)](https://automation-tasks-rs.github.io/cargo-auto/cargo_auto/index.html) //! ![cargo-auto](https://bestia.dev/webpage_hit_counter/get_svg_image/959103982.svg) //! -//! [![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-2565-green.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -//! [![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-1159-blue.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -//! [![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-615-purple.svg)](https://github.com/automation-tasks-rs/cargo-auto/) +//! [![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 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-10243-orange.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/) //! //! Hashtags: #maintained #ready-for-use #rustlang #automation #workflow //! My projects on GitHub are more like a tutorial than a finished product: [bestia-dev tutorials](https://github.com/bestia-dev/tutorials_rust_wasm). @@ -54,7 +54,7 @@ //! ``` //! //! We can also add `automation tasks` to an existing Rust project. -//! Inside your Rust project directory (the one with Cargo.toml) run: +//! Inside your Rust project directory (the one with `Cargo.toml`) run: //! //! ```bash //! cargo auto new_auto @@ -113,7 +113,7 @@ //! It is easy to run: //! //! ```bash -//! cargo auto new_wasm project_name +//! cargo auto new_wasm project_name github_owner_or_organization web_server_domain server_username //! # then //! cd project_name //! cargo auto build @@ -123,14 +123,12 @@ //! ## cargo auto new_pwa_wasm //! //! 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. -//! The template needs the title, name, long name, and description inside a `pwa.json5` file and the `icon512x512.png` file for the icons. +//! The template needs the `icon512x512.png` file for the icons. You can use the default icon if you don't have your own. ![icon512x512.png](https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png) //! It is easy to run: //! //! ```bash -//! cargo auto new_pwa_wasm -//! # on first run it will just create the `pwa.json5` and `icon512x512.png` files -//! # modify these files with data for your new app and then repeat -//! cargo auto new_pwa_wasm +//! curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png +//! cargo auto new_pwa_wasm project_name github_owner_or_organization web_server_domain server_username //! # then //! cd project_name //! cargo auto build diff --git a/src/lib.rs b/src/lib.rs index 58c3443..f882a15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! # cargo-auto //! //! **Automation tasks coded in Rust language for the workflow of Rust projects** -//! ***version: 2024.501.55 date: 2024-05-01 author: [bestia.dev](https://bestia.dev) repository: [GitHub](https://github.com/automation-tasks-rs/cargo-auto)*** +//! ***version: 2024.501.1827 date: 2024-05-01 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) @@ -26,11 +26,11 @@ //! [![Newest docs](https://img.shields.io/badge/newest_docs-blue.svg)](https://automation-tasks-rs.github.io/cargo-auto/cargo_auto/index.html) //! ![cargo-auto](https://bestia.dev/webpage_hit_counter/get_svg_image/959103982.svg) //! -//! [![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-2565-green.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -//! [![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-1159-blue.svg)](https://github.com/automation-tasks-rs/cargo-auto/) -//! [![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-615-purple.svg)](https://github.com/automation-tasks-rs/cargo-auto/) +//! [![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 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-10243-orange.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/) //! //! Hashtags: #maintained #ready-for-use #rustlang #automation #workflow //! My projects on GitHub are more like a tutorial than a finished product: [bestia-dev tutorials](https://github.com/bestia-dev/tutorials_rust_wasm). @@ -54,7 +54,7 @@ //! ``` //! //! We can also add `automation tasks` to an existing Rust project. -//! Inside your Rust project directory (the one with Cargo.toml) run: +//! Inside your Rust project directory (the one with `Cargo.toml`) run: //! //! ```bash //! cargo auto new_auto @@ -113,7 +113,7 @@ //! It is easy to run: //! //! ```bash -//! cargo auto new_wasm project_name +//! cargo auto new_wasm project_name github_owner_or_organization web_server_domain server_username //! # then //! cd project_name //! cargo auto build @@ -123,14 +123,12 @@ //! ## cargo auto new_pwa_wasm //! //! 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. -//! The template needs the title, name, long name, and description inside a `pwa.json5` file and the `icon512x512.png` file for the icons. +//! The template needs the `icon512x512.png` file for the icons. You can use the default icon if you don't have your own. ![icon512x512.png](https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png) //! It is easy to run: //! //! ```bash -//! cargo auto new_pwa_wasm -//! # on first run it will just create the `pwa.json5` and `icon512x512.png` files -//! # modify these files with data for your new app and then repeat -//! cargo auto new_pwa_wasm +//! curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png +//! cargo auto new_pwa_wasm project_name github_owner_or_organization web_server_domain server_username //! # then //! cd project_name //! cargo auto build diff --git a/src/outside_of_rust_project_mod.rs b/src/outside_of_rust_project_mod.rs index 4b91b83..74b0025 100644 --- a/src/outside_of_rust_project_mod.rs +++ b/src/outside_of_rust_project_mod.rs @@ -44,7 +44,7 @@ fn print_help_from_cargo_auto() { {YELLOW}Outside of a Rust project, cargo-auto can create a new Rust project:{RESET} {GREEN}cargo auto new_cli project_name github_owner{RESET}{YELLOW} - a simple yet complete CLI application, better then `cargo new`{RESET} {GREEN}cargo auto new_wasm project_name github_owner web_server_domain server_username{RESET}{YELLOW} - a complete wasm application that works inside the browser{RESET} -{GREEN}cargo auto new_pwa_wasm {RESET}{YELLOW} - On first call, it will create the `pwa.json5` and `icon512x512.png` files.{RESET} +{GREEN}cargo auto new_pwa_wasm project_name github_owner web_server_domain server_username{RESET}{YELLOW} - There must already be the `icon512x512.png` file to create the icons.{RESET} {YELLOW}Modify them with the required data for your pwa project and then repeat the same command.{RESET} {YELLOW}© 2024 bestia.dev MIT License github.com/automation-tasks-rs/cargo-auto{RESET} @@ -58,23 +58,21 @@ fn match_first_argument(task: &str, args: &mut std::env::Args) { if task == "completion" { completion(); } else if task == "new_cli" { - // project_name - let arg_2 = args.next(); - // github_owner - let arg_3 = args.next(); - crate::template_new_cli_mod::new_cli(arg_2, arg_3); + let rust_project_name = args.next(); + let github_owner_or_organization = args.next(); + crate::template_new_cli_mod::new_cli(rust_project_name, github_owner_or_organization); } else if task == "new_wasm" { - // project_name - let arg_2 = args.next(); - // github_owner - let arg_3 = args.next(); - // web server URL - let arg_4 = args.next(); - // web server username - let arg_5 = args.next(); - crate::template_new_wasm_mod::new_wasm(arg_2, arg_3, arg_4, arg_5); + let rust_project_name = args.next(); + let github_owner_or_organization = args.next(); + let web_server_domain = args.next(); + let server_username = args.next(); + crate::template_new_wasm_mod::new_wasm(rust_project_name, github_owner_or_organization, web_server_domain, server_username); } else if task == "new_pwa_wasm" { - crate::template_new_pwa_wasm_mod::new_pwa_wasm(); + let rust_project_name = args.next(); + let github_owner_or_organization = args.next(); + let web_server_domain = args.next(); + let server_username = args.next(); + crate::template_new_pwa_wasm_mod::new_pwa_wasm(rust_project_name, github_owner_or_organization, web_server_domain, server_username); } else { print_help_from_cargo_auto(); } diff --git a/src/template_new_auto_mod.rs b/src/template_new_auto_mod.rs index d023cbd..f7b062d 100644 --- a/src/template_new_auto_mod.rs +++ b/src/template_new_auto_mod.rs @@ -51,8 +51,8 @@ pub fn compile_project_automation_tasks_rs() { } } -pub fn copy_to_files(project_name: &str) { - let folder_path = std::path::Path::new(project_name); +pub fn copy_to_files(rust_project_name: &str) { + let folder_path = std::path::Path::new(rust_project_name); std::fs::create_dir_all(folder_path).unwrap(); for file_item in get_vec_file() { // create directory if needed diff --git a/src/template_new_cli_mod.rs b/src/template_new_cli_mod.rs index f78069f..532577c 100644 --- a/src/template_new_cli_mod.rs +++ b/src/template_new_cli_mod.rs @@ -7,33 +7,33 @@ use crate::{GREEN, RED, RESET, YELLOW}; -pub fn new_cli(arg_2: Option, arg_3: Option) { - if arg_2.is_none() { +pub fn new_cli(rust_project_name: Option, github_owner_or_organization: Option) { + if rust_project_name.is_none() { println!("{RED}Error: Project name argument is missing: `cargo auto new_cli project_name github_owner_or_organization`{RESET}"); return; } - if arg_3.is_none() { + if github_owner_or_organization.is_none() { println!("{RED}Error: Github owner argument is missing: `cargo auto new_cli project_name github_owner_or_organization`{RESET}"); return; } - let project_name = arg_2.unwrap(); - let github_owner_or_organization = arg_3.unwrap(); + let rust_project_name = rust_project_name.unwrap(); + let github_owner_or_organization = github_owner_or_organization.unwrap(); - copy_to_files(&project_name, &github_owner_or_organization); + copy_to_files(&rust_project_name, &github_owner_or_organization); println!(""); - println!(" {YELLOW}The command `cargo auto new_cli` generated the directory `{project_name}`.{RESET}"); + println!(" {YELLOW}The command `cargo auto new_cli` generated the directory `{rust_project_name}`.{RESET}"); println!(" {YELLOW}You can open this new Rust project in VSCode:{RESET}",); - println!("{GREEN}code {project_name}{RESET}"); + println!("{GREEN}code {rust_project_name}{RESET}"); println!(" {YELLOW}Then build inside the VSCode terminal with:{RESET}"); println!("{GREEN}cargo auto build{RESET}"); println!(" {YELLOW}and follow the detailed instructions.{RESET}"); } -pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str) { - let folder_path = std::path::Path::new(project_name); +pub fn copy_to_files(rust_project_name: &str, github_owner_or_organization: &str) { + let folder_path = std::path::Path::new(rust_project_name); if folder_path.exists() { - panic!("{RED}Error: Folder {project_name} already exists! {RESET}"); + panic!("{RED}Error: Folder {rust_project_name} already exists! {RESET}"); } std::fs::create_dir_all(folder_path).unwrap(); @@ -65,7 +65,7 @@ pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str) { // template has only valid utf8 files println!("replace: {}", entry.path().to_string_lossy()); let content = std::fs::read_to_string(entry.path()).unwrap(); - let content = content.replace("cargo_auto_template_new_cli", project_name); + let content = content.replace("cargo_auto_template_new_cli", rust_project_name); let content = content.replace("automation-tasks-rs", github_owner_or_organization); let content = content.replace("automation--tasks--rs", "automation-tasks-rs"); std::fs::write(entry.path(), content).unwrap(); @@ -77,7 +77,7 @@ pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str) { for entry in traverse_reverse.iter() { if entry.file_name() == "cargo_auto_template_new_cli" { println!("rename: {}", entry.path().to_string_lossy()); - std::fs::rename(entry.path(), entry.path().parent().unwrap().join(project_name)).unwrap(); + std::fs::rename(entry.path(), entry.path().parent().unwrap().join(rust_project_name)).unwrap(); } } } diff --git a/src/template_new_pwa_wasm_mod.rs b/src/template_new_pwa_wasm_mod.rs index 445c44d..da4f278 100644 --- a/src/template_new_pwa_wasm_mod.rs +++ b/src/template_new_pwa_wasm_mod.rs @@ -5,163 +5,134 @@ //! The template is downloaded from github //! +use crate::{GREEN, RED, RESET, YELLOW}; + +pub fn new_pwa_wasm(rust_project_name: Option, github_owner_or_organization: Option, web_server_domain: Option, server_username: Option) { + // internal function: favicon.ico with 16 and 32 icons + fn encode_to_favicon_ico(img: &image::DynamicImage, rust_project_name: &str) { + /// internal function: favicon + fn favicon_add_entry(img: &image::DynamicImage, size: u32, icon_dir: &mut ico::IconDir) { + // icons need smaller images 48, 32 and 16 + let img_rgba_vec = img.resize(size, size, image::imageops::FilterType::Lanczos3).into_rgba8().into_raw(); + // create an IconImage from raw RGBA pixel data from another image library + let icon_image = ico::IconImage::from_rgba_data(size, size, img_rgba_vec); + icon_dir.add_entry(ico::IconDirEntry::encode(&icon_image).unwrap()); + } + // Create a new, empty icon collection: + let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon); + favicon_add_entry(img, 16, &mut icon_dir); + favicon_add_entry(img, 32, &mut icon_dir); + favicon_add_entry(img, 48, &mut icon_dir); + let file_name = format!("{rust_project_name}/web_server_folder/{rust_project_name}/favicon.ico"); + let buffer = std::fs::File::create(file_name).unwrap(); + icon_dir.write(buffer).unwrap(); + } -use crate::{GREEN, RESET, YELLOW,RED}; -use serde::Deserialize; - -#[derive(Debug, Deserialize)] -struct PwaJson5 { - rust_project_name: String, - github_owner_or_organization: String, - pwa_description: String, - web_server_domain: String, - server_username: String, -} + /// internal function: decode png + fn decode_png(vec: Vec) -> image::DynamicImage { + let img = image::io::Reader::new(std::io::Cursor::new(vec)); + let img = img.with_guessed_format().unwrap(); + let img = img.decode().unwrap(); + // return + img + } -pub fn new_pwa_wasm() { - if std::path::Path::new("pwa.json5").exists() && std::path::Path::new("icon512x512.png").exists() { - // both exist, read the json5 and run the generator - let pwa_json5: PwaJson5 = json5::from_str(&std::fs::read_to_string("pwa.json5").unwrap()).unwrap(); - copy_to_files(&pwa_json5); - - // region: png with various sizes for: favicon png, pwa Android and pwa iOS - // 32, 72, 96, 120, 128, 144, 152, 167, 180, 192, 196, 512 - let img = std::fs::read("icon512x512.png").unwrap(); - let img = decode_png(img); - - resize_image(&img, 32, "icon-032.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 72, "icon-072.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 96, "icon-096.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 120, "icon-120.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 128, "icon-128.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 144, "icon-144.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 152, "icon-152.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 167, "icon-167.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 180, "icon-180.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 192, "icon-192.png", &pwa_json5.rust_project_name, ); - resize_image(&img, 196, "icon-196.png", &pwa_json5.rust_project_name, ); - // overwrite the default with the new - resize_image(&img, 512, "icon-512.png", &pwa_json5.rust_project_name, ); - - // maskable icon 192 - resize_image(&img, 192, "icon-maskable.png", &pwa_json5.rust_project_name, ); - - // favicon.ico with 16, 32 and 48 icons - encode_to_favicon_ico(&img, &pwa_json5.rust_project_name, ); - - // endregion - println!( - r#" - {YELLOW}On second run, the command `crate auto new_pwa_wasm` generated the project directory `{package_name}`{RESET} - {YELLOW}You can open this new Rust project `{package_name}` in VSCode:{RESET} -{GREEN}code {package_name}{RESET} - {YELLOW}Then build with:{RESET} -{GREEN}cargo auto build{RESET} - {YELLOW}and follow the detailed instructions.{RESET} -"#, - package_name = &pwa_json5.rust_project_name - ); - } else { - // On first run - // They don't exist, create the default ones and return instructions to the user. - if !std::path::Path::new("pwa.json5").exists() { - std::fs::write( - "pwa.json5", - r#" -{ - // modify the values in this json5 file accordingly to your new project - - rust_project_name: "hello_world", - github_owner_or_organization: "automation-tasks-rs", - pwa_description: "Template for a minimal pwa wasm project for browser", - web_server_domain: "bestia.dev", - server_username: "luciano_bestia", -} -"#, - ) - .unwrap(); - } - if !std::path::Path::new("icon512x512.png").exists() { - // TODO: download icon 512 from github - // https://raw.githubusercontent.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/main/web_server_folder/cargo_auto_template_new_pwa_wasm/icons/icon-512.png - // decode the icon-512 - for file_item in get_vec_file() { - if file_item.file_name.ends_with("icon-512.png") { - let file_content = file_item.file_content; - let file_content = ::decode_vec(&file_content).unwrap(); - std::fs::write("icon512x512.png", file_content).unwrap(); - break; - } - } + /// internal function: resize img + fn resize_image(img: &image::DynamicImage, img_size: u32, file_name: &str, rust_project_name: &str) { + /// internal function: encode to png + fn encode_to_png(new_img: image::DynamicImage) -> Vec { + //dbg!("encode new_img"); + let vec_u8: Vec = Vec::new(); + let mut cursor_1 = std::io::Cursor::new(vec_u8); + let _x = new_img.write_to(&mut cursor_1, image::ImageOutputFormat::Png).unwrap(); + // return + cursor_1.get_ref().to_owned() } + //dbg!("resize_image {img_size}"); + let new_img = img.resize(img_size, img_size, image::imageops::FilterType::Lanczos3); + let vec_u8 = encode_to_png(new_img); + + let file_name = format!("{rust_project_name}/web_server_folder/{rust_project_name}/icons/{file_name}"); + std::fs::write(file_name, vec_u8).unwrap(); + } + + if !std::path::Path::new("icon512x512.png").exists() { println!( r#" - {YELLOW}On first run, the command `crate auto new_pwa_wasm` generated the files `pwa.json5` and `icon512x512.png`.{RESET} - {YELLOW}Modify these files accordingly. This step is very important:{RESET} -{GREEN}code pwa.json5{RESET} - {YELLOW}Finally, repeat the same command to generate the project directory.{RESET} -{GREEN}crate auto new_pwa_wasm {RESET} + {RED}The mandatory file `icon512x512.png` is not found in this folder.{RESET} + {YELLOW}If you don't have your icon, you can download and use this default:{RESET} +{GREEN}curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png{RESET} "# ); + return; } -} - -// favicon.ico with 16 and 32 icons -pub fn encode_to_favicon_ico(img: &image::DynamicImage, rust_project_name: &str) { - // Create a new, empty icon collection: - let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon); - favicon_add_entry(img, 16, &mut icon_dir); - favicon_add_entry(img, 32, &mut icon_dir); - favicon_add_entry(img, 48, &mut icon_dir); - let file_name = format!("{rust_project_name}/web_server_folder/{rust_project_name}/favicon.ico"); - let buffer = std::fs::File::create(file_name).unwrap(); - icon_dir.write(buffer).unwrap(); -} - -pub fn favicon_add_entry(img: &image::DynamicImage, size: u32, icon_dir: &mut ico::IconDir) { - // icons need smaller images 48, 32 and 16 - let img_rgba_vec = img.resize(size, size, image::imageops::FilterType::Lanczos3).into_rgba8().into_raw(); - // create an IconImage from raw RGBA pixel data from another image library - let icon_image = ico::IconImage::from_rgba_data(size, size, img_rgba_vec); - icon_dir.add_entry(ico::IconDirEntry::encode(&icon_image).unwrap()); -} - -/// decode png -pub fn decode_png(vec: Vec) -> image::DynamicImage { - let img = image::io::Reader::new(std::io::Cursor::new(vec)); - let img = img.with_guessed_format().unwrap(); - let img = img.decode().unwrap(); - // return - img -} - -/// encode to png -pub fn encode_to_png(new_img: image::DynamicImage) -> Vec { - //dbg!("encode new_img"); - let vec_u8: Vec = Vec::new(); - let mut cursor_1 = std::io::Cursor::new(vec_u8); - let _x = new_img.write_to(&mut cursor_1, image::ImageOutputFormat::Png).unwrap(); - // return - cursor_1.get_ref().to_owned() -} - -/// resize img -pub fn resize_image(img: &image::DynamicImage, img_size: u32, file_name: &str, rust_project_name: &str) { - //dbg!("resize_image {img_size}"); - let new_img = img.resize(img_size, img_size, image::imageops::FilterType::Lanczos3); - let vec_u8 = encode_to_png(new_img); - let file_name = format!("{rust_project_name}/web_server_folder/{rust_project_name}/icons/{file_name}"); - std::fs::write(file_name, vec_u8).unwrap(); + if rust_project_name.is_none() { + println!("{RED}Error: Project name argument is missing: `cargo auto new_pwa_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); + return; + } + if github_owner_or_organization.is_none() { + println!("{RED}Error: Github owner or Organization argument is missing: `cargo auto new_pwa_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); + return; + } + if web_server_domain.is_none() { + println!("{RED}Error: Web server argument is missing: `cargo auto new_pwa_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); + return; + } + if server_username.is_none() { + println!("{RED}Error: Server username argument is missing: `cargo auto new_pwa_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); + return; + } + let rust_project_name = rust_project_name.unwrap(); + let github_owner_or_organization = github_owner_or_organization.unwrap(); + let web_server_domain = web_server_domain.unwrap(); + let server_username = server_username.unwrap(); + + // the icon exist, already checked + copy_to_files(&rust_project_name, &github_owner_or_organization, &web_server_domain, &server_username); + + // region: png with various sizes for: favicon png, pwa Android and pwa iOS + // 32, 72, 96, 120, 128, 144, 152, 167, 180, 192, 196, 512 + let img = std::fs::read("icon512x512.png").unwrap(); + let img = decode_png(img); + + resize_image(&img, 32, "icon-032.png", &rust_project_name); + resize_image(&img, 72, "icon-072.png", &rust_project_name); + resize_image(&img, 96, "icon-096.png", &rust_project_name); + resize_image(&img, 120, "icon-120.png", &rust_project_name); + resize_image(&img, 128, "icon-128.png", &rust_project_name); + resize_image(&img, 144, "icon-144.png", &rust_project_name); + resize_image(&img, 152, "icon-152.png", &rust_project_name); + resize_image(&img, 167, "icon-167.png", &rust_project_name); + resize_image(&img, 180, "icon-180.png", &rust_project_name); + resize_image(&img, 192, "icon-192.png", &rust_project_name); + resize_image(&img, 196, "icon-196.png", &rust_project_name); + // overwrite the default with the new + resize_image(&img, 512, "icon-512.png", &rust_project_name); + + // maskable icon 192 + resize_image(&img, 192, "icon-maskable.png", &rust_project_name); + + // favicon.ico with 16, 32 and 48 icons + encode_to_favicon_ico(&img, &rust_project_name); + + // endregion + println!( + r#" + {YELLOW}You can open this new Rust project in VSCode:{RESET} +{GREEN}code {package_name}{RESET} + {YELLOW}Then build it with:{RESET} +{GREEN}cargo auto build{RESET} + {YELLOW}Then follow the detailed instructions.{RESET} +"#, + package_name = rust_project_name + ); } -pub fn copy_to_files(pwa_json5: &PwaJson5) { - let project_name = &pwa_json5.rust_project_name; - - - let folder_path = std::path::Path::new(project_name); +pub fn copy_to_files(rust_project_name: &str, github_owner_or_organization: &str, web_server_domain: &str, server_username: &str) { + let folder_path = std::path::Path::new(rust_project_name); if folder_path.exists() { - panic!("{RED}Error: Folder {project_name} already exists! {RESET}"); + panic!("{RED}Error: Folder {rust_project_name} already exists! {RESET}"); } std::fs::create_dir_all(folder_path).unwrap(); @@ -191,17 +162,16 @@ pub fn copy_to_files(pwa_json5: &PwaJson5) { for entry in walkdir::WalkDir::new(folder_path).into_iter().filter_map(Result::ok) { if entry.file_type().is_file() { if entry.file_name().to_string_lossy().ends_with(".ico") || entry.file_name().to_string_lossy().ends_with(".png") || entry.file_name().to_string_lossy().ends_with(".woff2") { - // cannot replace text in binary files - }else{ + // cannot replace text in binary files + } else { // template has only valid utf8 files println!("replace: {}", entry.path().to_string_lossy()); let content = std::fs::read_to_string(entry.path()).unwrap(); - let content = content.replace("cargo_auto_template_new_pwa_wasm", project_name); - let content = content.replace("automation-tasks-rs", &pwa_json5.github_owner_or_organization); + let content = content.replace("cargo_auto_template_new_pwa_wasm", rust_project_name); + let content = content.replace("automation-tasks-rs", github_owner_or_organization); let content = content.replace("automation--tasks--rs", "automation-tasks-rs"); - let content = content.replace("pwa_description", &pwa_json5.pwa_description); - let content = content.replace("web_server_domain",&pwa_json5.web_server_domain); - let content = content.replace("server_username", &pwa_json5.server_username); + let content = content.replace("web_server_domain", web_server_domain); + let content = content.replace("server_username", server_username); std::fs::write(entry.path(), content).unwrap(); } } @@ -212,7 +182,7 @@ pub fn copy_to_files(pwa_json5: &PwaJson5) { for entry in traverse_reverse.iter() { if entry.file_name() == "cargo_auto_template_new_pwa_wasm" { println!("rename: {}", entry.path().to_string_lossy()); - std::fs::rename(entry.path(), entry.path().parent().unwrap().join(project_name)).unwrap(); + std::fs::rename(entry.path(), entry.path().parent().unwrap().join(rust_project_name)).unwrap(); } } } diff --git a/src/template_new_wasm_mod.rs b/src/template_new_wasm_mod.rs index 2c9d8bd..0fbba8d 100644 --- a/src/template_new_wasm_mod.rs +++ b/src/template_new_wasm_mod.rs @@ -7,44 +7,44 @@ use crate::{GREEN, RED, RESET, YELLOW}; -pub fn new_wasm(arg_2: Option, arg_3: Option, arg_4: Option, arg_5: Option) { - if arg_2.is_none() { +pub fn new_wasm(rust_project_name: Option, github_owner_or_organization: Option, web_server_domain: Option, server_username: Option) { + if rust_project_name.is_none() { println!("{RED}Error: Project name argument is missing: `cargo auto new_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); return; } - if arg_3.is_none() { + if github_owner_or_organization.is_none() { println!("{RED}Error: Github owner or Organization argument is missing: `cargo auto new_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); return; } - if arg_4.is_none() { + if web_server_domain.is_none() { println!("{RED}Error: Web server argument is missing: `cargo auto new_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); return; } - if arg_5.is_none() { + if server_username.is_none() { println!("{RED}Error: Server username argument is missing: `cargo auto new_wasm project_name github_owner_or_organization web_server server_username`{RESET}"); return; } - let project_name = arg_2.unwrap(); - let github_owner_or_organization = arg_3.unwrap(); - let web_server_domain = arg_4.unwrap(); - let server_username = arg_5.unwrap(); + let rust_project_name = rust_project_name.unwrap(); + let github_owner_or_organization = github_owner_or_organization.unwrap(); + let web_server_domain = web_server_domain.unwrap(); + let server_username = server_username.unwrap(); - copy_to_files(&project_name, &github_owner_or_organization, &web_server_domain, &server_username); + copy_to_files(&rust_project_name, &github_owner_or_organization, &web_server_domain, &server_username); println!(""); - println!(" {YELLOW}The command `crate auto new_wasm` generated the directory `{project_name}`{RESET}"); - println!(" {YELLOW}You can open this new Rust project `{project_name}` in a new Rust editor.{RESET}",); + println!(" {YELLOW}The command `crate auto new_wasm` generated the directory `{rust_project_name}`{RESET}"); + println!(" {YELLOW}You can open this new Rust project `{rust_project_name}` in a new Rust editor.{RESET}",); println!(" {YELLOW}For example VSCode:{RESET}"); - println!("{GREEN}code {project_name}{RESET}"); + println!("{GREEN}code {rust_project_name}{RESET}"); println!(" {YELLOW}Then build with:{RESET}"); println!("{GREEN}cargo auto build{RESET}"); println!(" {YELLOW}and follow the detailed instructions.{RESET}"); } -pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str, web_server_domain: &str, server_username: &str) { - let folder_path = std::path::Path::new(project_name); +pub fn copy_to_files(rust_project_name: &str, github_owner_or_organization: &str, web_server_domain: &str, server_username: &str) { + let folder_path = std::path::Path::new(rust_project_name); if folder_path.exists() { - panic!("{RED}Error: Folder {project_name} already exists! {RESET}"); + panic!("{RED}Error: Folder {rust_project_name} already exists! {RESET}"); } std::fs::create_dir_all(folder_path).unwrap(); @@ -76,7 +76,7 @@ pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str, web // template has only valid utf8 files println!("replace: {}", entry.path().to_string_lossy()); let content = std::fs::read_to_string(entry.path()).unwrap(); - let content = content.replace("cargo_auto_template_new_wasm", project_name); + let content = content.replace("cargo_auto_template_new_wasm", rust_project_name); let content = content.replace("automation-tasks-rs", github_owner_or_organization); let content = content.replace("automation--tasks--rs", "automation-tasks-rs"); let content = content.replace("web_server_domain", web_server_domain); @@ -90,7 +90,7 @@ pub fn copy_to_files(project_name: &str, github_owner_or_organization: &str, web for entry in traverse_reverse.iter() { if entry.file_name() == "cargo_auto_template_new_wasm" { println!("rename: {}", entry.path().to_string_lossy()); - std::fs::rename(entry.path(), entry.path().parent().unwrap().join(project_name)).unwrap(); + std::fs::rename(entry.path(), entry.path().parent().unwrap().join(rust_project_name)).unwrap(); } } }