feat: basic path parsing done + testing
This commit is contained in:
@@ -1,2 +1,6 @@
|
|||||||
# code-templates
|
# code-templates
|
||||||
# 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.
|
||||||
|
|||||||
64
src/main.rs
64
src/main.rs
@@ -1,8 +1,12 @@
|
|||||||
mod files;
|
mod files;
|
||||||
mod parsers;
|
mod parsers;
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
use files::*;
|
use files::*;
|
||||||
use inquire::{Select, Text};
|
use inquire::{Select, Text};
|
||||||
|
use parsers::apply_name_template;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[path = "./parsers/test/parsers_tests.rs"]
|
#[path = "./parsers/test/parsers_tests.rs"]
|
||||||
@@ -19,20 +23,66 @@ fn main() {
|
|||||||
let template_type_result =
|
let template_type_result =
|
||||||
Select::new("Select a template variant:", template_type_options).prompt();
|
Select::new("Select a template variant:", template_type_options).prompt();
|
||||||
|
|
||||||
if let Ok(template_type) = template_type_result {
|
if let Ok(template_path) = 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_file) = template_file_result {
|
|
||||||
let target_name_result = Text::new("Insert the desired name:").prompt();
|
let target_name_result = Text::new("Insert the desired name:").prompt();
|
||||||
|
|
||||||
if let Ok(target_name) = target_name_result {
|
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
|
// 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();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use regex::Regex;
|
|||||||
|
|
||||||
pub mod expressions;
|
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 {
|
match template {
|
||||||
"__name__" => filename.to_string(),
|
"__name__" => filename.to_string(),
|
||||||
"__upperCase_name__" => filename.to_uppercase().to_string(),
|
"__upperCase_name__" => filename.to_uppercase().to_string(),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use crate::parsers::{apply_filename_template, expressions::FILENAME_EXPRESSIONS};
|
use crate::parsers::{apply_name_template, expressions::FILENAME_EXPRESSIONS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_apply_filename_template() {
|
fn test_apply_filename_template() {
|
||||||
@@ -19,7 +19,7 @@ pub mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (i, expression) in FILENAME_EXPRESSIONS.into_iter().enumerate() {
|
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])
|
assert_eq!(output, expected_filename_output[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user