Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 9, 2024
1 parent 453a891 commit e2cecb5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .phil-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
logo:assets/logo.png
logo_small:assets/logo.png
description_translate:de
version:0.3.0
version:0.3.1
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down
5 changes: 5 additions & 0 deletions src/command_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ pub fn get_all_commands() -> Vec<Command> {
"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;
Expand Down
6 changes: 5 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 9 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -125,19 +125,19 @@ 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;
}
}
}
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);

Expand All @@ -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();

Expand Down

0 comments on commit e2cecb5

Please sign in to comment.