diff --git a/.blueprints/component/__camelCase_name__/__loweCase_name__.rs b/.blueprints/component/__camelCase_name__/__loweCase_name__.rs new file mode 100644 index 0000000..e69de29 diff --git a/.blueprints/component/__camelCase_name__/__snakeCase_name__/__kebabCase_name__ b/.blueprints/component/__camelCase_name__/__snakeCase_name__/__kebabCase_name__ new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index 0535e9e..f5ea2a5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # code-templates # code-templates + +## Known issues + +- Only admits 1 level of encapsulation. This means that within the `templates`/`blueprints` folder, only the direct childrens will be treated as templates. diff --git a/src/main.rs b/src/main.rs index bf87768..ffdfc56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,12 @@ mod files; mod parsers; +use std::fs; + use files::*; use inquire::{Select, Text}; +use parsers::apply_name_template; +use regex::Regex; #[cfg(test)] #[path = "./parsers/test/parsers_tests.rs"] @@ -19,20 +23,66 @@ fn main() { let template_type_result = Select::new("Select a template variant:", template_type_options).prompt(); - if let Ok(template_type) = template_type_result { - let template_file_options = get_template_options(vec![template_type]); - let template_file_result = - Select::new("Select a template:", template_file_options).prompt(); + if let Ok(template_path) = template_type_result { + let target_name_result = Text::new("Insert the desired name:").prompt(); - if let Ok(template_file) = template_file_result { - let target_name_result = Text::new("Insert the desired name:").prompt(); + if let Ok(target_name) = target_name_result { + // TODO: decide if the path should be inserted automatically of with a loop of selections -> Maybe better the loop + let target_path_result = Text::new("Insert the target path:").prompt(); - if let Ok(target_name) = target_name_result { - // TODO: decide if the path should be inserted automatically of with a loop of selections -> Maybe better the loop - let target_path_result = Text::new("Insert the target path:").prompt(); - - if let Ok(target_path) = target_path_result {} + if let Ok(target_path) = target_path_result { + create_template(template_path, target_name, target_path).unwrap(); } } } } + +fn create_template( + template_path: String, + target_name: String, + target_path: String, +) -> Result<(), ()> { + let paths = fs::read_dir(&template_path).unwrap(); + for _path in paths { + let path = _path.unwrap().path(); + let mut to_create_path = path.display().to_string().clone(); + + to_create_path.replace_range(0..template_path.len(), &target_path); + + let get_path_names_regex = Regex::new(r"\/(__.*__)").unwrap(); + to_create_path = get_path_names_regex + .replace_all(&to_create_path, |captured: ®ex::Captures| { + format!("/{}", apply_name_template(&captured[1], &target_name),) + }) + .into_owned(); + + if path.is_dir() { + // Create path + create_template( + path.display().to_string(), + target_name.clone(), + to_create_path, + ) + .unwrap(); + } else { + // Create file + } + } + + Ok(()) +} + +#[cfg(test)] +pub mod tests { + use crate::create_template; + + #[test] + fn test_create_template() { + create_template( + "./.blueprints".to_string(), + "this_is a-TeSt".to_string(), + "./source/test".to_string(), + ) + .unwrap(); + } +} diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index 318ef80..4c8b9c1 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -2,7 +2,7 @@ use regex::Regex; pub mod expressions; -pub fn apply_filename_template(template: &str, filename: &str) -> String { +pub fn apply_name_template(template: &str, filename: &str) -> String { match template { "__name__" => filename.to_string(), "__upperCase_name__" => filename.to_uppercase().to_string(), diff --git a/src/parsers/test/parsers_tests.rs b/src/parsers/test/parsers_tests.rs index 4c0f0da..1e0b8a1 100644 --- a/src/parsers/test/parsers_tests.rs +++ b/src/parsers/test/parsers_tests.rs @@ -1,6 +1,6 @@ #[cfg(test)] pub mod tests { - use crate::parsers::{apply_filename_template, expressions::FILENAME_EXPRESSIONS}; + use crate::parsers::{apply_name_template, expressions::FILENAME_EXPRESSIONS}; #[test] fn test_apply_filename_template() { @@ -19,7 +19,7 @@ pub mod tests { ]; for (i, expression) in FILENAME_EXPRESSIONS.into_iter().enumerate() { - let output = apply_filename_template(expression, FILENAME); + let output = apply_name_template(expression, FILENAME); assert_eq!(output, expected_filename_output[i]) } }