From e2cecb5c7cfd686d9224089a763ac2986d8994cb Mon Sep 17 00:00:00 2001 From: cophilot Date: Fri, 9 Feb 2024 22:51:36 +0100 Subject: [PATCH] v0.3.1 --- .phil-project | 2 +- CHANGELOG.md | 6 ++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 7 ++----- src/command_storage.rs | 5 +++++ src/commands.rs | 6 +++++- src/utils.rs | 13 +++++++++---- 8 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.phil-project b/.phil-project index a77f874..da1396e 100644 --- a/.phil-project +++ b/.phil-project @@ -1,4 +1,4 @@ logo:assets/logo.png logo_small:assets/logo.png description_translate:de -version:0.3.0 +version:0.3.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ad6f08..83a8715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ --- +## [v0.3.1](https://github.com/cophilot/templify/tree/0.3.1) (2024-2-9) + +- Added `-dry-run` flag for the `generate` command + +--- + ## [v0.3.0](https://github.com/cophilot/templify/tree/0.3.0) (2024-2-6) - Bug fixes diff --git a/Cargo.lock b/Cargo.lock index e7ef38e..e4e5286 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "templify" -version = "0.3.0" +version = "0.3.1" dependencies = [ "reqwest", "self-replace", diff --git a/Cargo.toml b/Cargo.toml index 4417172..a6b08e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "templify" -version = "0.3.0" +version = "0.3.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 67e9331..7df6ced 100644 --- a/README.md +++ b/README.md @@ -213,12 +213,9 @@ tpy load https://github.com/cophilot/templify-vault/tree/main/React-ts ## [Release Notes](https://github.com/cophilot/templify/blob/master/CHANGELOG.md) -### [v0.3.0](https://github.com/cophilot/templify/tree/0.3.0) +### [v0.3.1](https://github.com/cophilot/templify/tree/0.3.1) -- Bug fixes -- Added `command` argument for the `help` command to display help for a specific command -- Command `generate` uses pattern matching to determine the type of template to generate -- Added `-strict` flag for `generate` command to disable pattern matching +- Added `-dry-run` flag for the `generate` command --- diff --git a/src/command_storage.rs b/src/command_storage.rs index 5431a75..5349c20 100644 --- a/src/command_storage.rs +++ b/src/command_storage.rs @@ -150,6 +150,11 @@ pub fn get_all_commands() -> Vec { "If enabled the template name must match exactly.".to_string(), )); + generate_com.add_flag(Flag::new_bool_flag( + vec!["dry-run".to_string(), "dr".to_string()], + "If enabled the file will not be created and the output will be printed.".to_string(), + )); + commands.push(generate_com); return commands; diff --git a/src/commands.rs b/src/commands.rs index 4fdd65c..6aa4a9c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -70,6 +70,7 @@ pub fn generate(command: &Command) -> Status { } let strict = command.get_bool_flag("strict"); + let dry_run = command.get_bool_flag("dry-run"); let mut template_name = command.get_argument("template-name").value.clone(); let parsed_template_name = template_name.clone().to_lowercase().to_string(); @@ -120,12 +121,15 @@ pub fn generate(command: &Command) -> Status { .replace("$$name$$", given_name.as_str()); // create dir and all subdirs if they don't exist - std::fs::create_dir_all(&new_path).unwrap(); + if !dry_run { + std::fs::create_dir_all(&new_path).unwrap(); + } if utils::generate_template_dir( &format!(".templates/{}", template_name), &new_path, given_name.as_str(), + dry_run, ) { println!("Files generated successfully."); return Status::ok(); diff --git a/src/utils.rs b/src/utils.rs index eecc027..e080d1b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -104,7 +104,7 @@ pub fn load_remote_template_file(path: &str, url: &str, force: bool) { println!("Created file {}", path); } -pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) -> bool { +pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str, dry_run: bool) -> bool { let paths = std::fs::read_dir(path).unwrap(); for path in paths { let path = path.unwrap().path(); @@ -125,11 +125,11 @@ pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) -> bo if path.is_dir() { std::fs::create_dir(&new_path).unwrap(); - if !generate_template_dir(&path.to_str().unwrap(), &new_path, given_name) { + if !generate_template_dir(&path.to_str().unwrap(), &new_path, given_name, dry_run) { return false; } } else { - if !generate_template_file(&path.to_str().unwrap(), &new_path, given_name) { + if !generate_template_file(&path.to_str().unwrap(), &new_path, given_name, dry_run) { return false; } } @@ -137,7 +137,7 @@ pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) -> bo return true; } -pub fn generate_template_file(path: &str, new_path: &str, given_name: &str) -> bool { +pub fn generate_template_file(path: &str, new_path: &str, given_name: &str, dry_run: bool) -> bool { let file_content = std::fs::read_to_string(path).unwrap(); let file_content = file_content.replace("$$name$$", given_name); @@ -146,6 +146,11 @@ pub fn generate_template_file(path: &str, new_path: &str, given_name: &str) -> b return false; } + if dry_run { + println!("Would create file {}", new_path); + return true; + } + let mut new_file = std::fs::File::create(new_path).unwrap(); new_file.write_all(file_content.as_bytes()).unwrap();