diff --git a/src/generator/common.rs b/src/generator/common.rs index bd597ef..1f877b0 100644 --- a/src/generator/common.rs +++ b/src/generator/common.rs @@ -1,7 +1,8 @@ use crate::{generator::template_functions::TEMPLATE_FUNCTIONS, Templates}; use crate::{template_context::TemplateContext, utils}; use gtmpl::Context; -use std::path::{Path, PathBuf}; +use rust_embed::EmbeddedFile; +use std::path::Path; /// runs cargo command with options /// Example: ` cargo_command!("init","--bin","path"); ` @@ -17,9 +18,8 @@ macro_rules! cargo_command { } }; } - +/// checks if project with name already exists, if yes asks for permission to overwrite pub fn check_for_overwrite(output_path: &Path, project_title: &str) { - //check if project with name already exists, if yes ask for permission to overwrite if output_path.exists() { let warn_message = format!("A project with the name {} already exists in the current directory, do you want to overwrite the existing project? \nWARNING: This will delete all files in the directory and all applied. \nType 'y' to continue or anything else to exit.",project_title); println!("{}", warn_message); @@ -40,25 +40,30 @@ pub fn check_for_overwrite(output_path: &Path, project_title: &str) { } } -/// reads template from path renders it with context reference and writes to output file -pub fn template_render_write( +/// takes an embedded `template_path`, renders it with context reference and writes to output file +pub fn embedded_template_render_write( template_path: &str, context_ref: impl Into, - output_path: &PathBuf, + output_path: &Path, ) { - let template = match Templates::get(template_path) { + let template: EmbeddedFile = match Templates::get(template_path) { Some(template) => template, None => { eprintln!("❌ Error reading template"); std::process::exit(1); } }; - let template = template.data.as_ref(); - let mut render = match render_template( - std::str::from_utf8(template).unwrap(), - context_ref, - TEMPLATE_FUNCTIONS, - ) { + let template = std::str::from_utf8(template.data.as_ref()).unwrap(); + template_render_write(template, context_ref, output_path) +} + +/// takes a `template`, renders it with context reference and writes to output file +pub fn template_render_write( + template: impl Into, + context_ref: impl Into, + output_path: &Path, +) { + let mut render = match render_template(template, context_ref, TEMPLATE_FUNCTIONS) { Ok(render) => render, Err(e) => { eprintln!("❌ Error rendering template: {}", e); @@ -92,21 +97,22 @@ fn render_template, C: Into, F: Into + Clo tmpl.parse(template_str)?; tmpl.render(&Context::from(context)).map_err(Into::into) } - -pub fn write_multiple_templates( +/// renders and writes all templates in `template_file_paths` to `output_path` +/// if file has `.go` extension it will be changed to `.rs` +pub fn write_multiple_embedded_templates<'a>( context_ref: &TemplateContext, output_path: &Path, - template_file_paths: &[&str], + template_file_paths: impl Iterator, ) { for template_file_path in template_file_paths { if template_file_path.ends_with(".go") { - template_render_write( + embedded_template_render_write( template_file_path, context_ref, &output_path.join(template_file_path).with_extension("rs"), ); } else { - template_render_write( + embedded_template_render_write( template_file_path, context_ref, &output_path.join(template_file_path), diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 96d8dd4..54537ab 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -1,5 +1,8 @@ mod common; -pub use common::{check_for_overwrite, template_render_write, write_multiple_templates}; +pub use common::{ + check_for_overwrite, embedded_template_render_write, template_render_write, + write_multiple_embedded_templates, +}; mod model; pub use model::generate_models_folder; diff --git a/src/generator/model.rs b/src/generator/model.rs index f35f789..3ded2e0 100644 --- a/src/generator/model.rs +++ b/src/generator/model.rs @@ -1,4 +1,4 @@ -use super::template_render_write; +use super::embedded_template_render_write; use crate::template_context::TemplateContext; use crate::{parser::common::validate_identifier_string, utils::write_to_path_create_dir}; @@ -11,7 +11,7 @@ pub fn generate_models_folder(async_config: &TemplateContext, output_path: &Path .iter() .for_each(|message_model| { if !message_model.model_definition.is_empty() { - template_render_write( + embedded_template_render_write( "src/model.go", message_model.clone(), &output_path.join(format!( diff --git a/src/main.rs b/src/main.rs index 020629f..9ee304b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,12 @@ mod utils; use crate::{ asyncapi_model::AsyncAPI, - generator::{check_for_overwrite, generate_models_folder, write_multiple_templates}, + generator::{check_for_overwrite, generate_models_folder, write_multiple_embedded_templates}, utils::append_file_to_file, }; use clap::Parser; use rust_embed::RustEmbed; -use std::path::Path; -use std::process::Command; +use std::{path::Path, process::Command}; #[derive(RustEmbed)] #[folder = "./templates"] @@ -56,10 +55,10 @@ fn main() { check_for_overwrite(output_path, title); - write_multiple_templates( + write_multiple_embedded_templates( &async_config, output_path, - &[ + [ "src/main.go", "src/handler.go", "src/cli.go", @@ -69,7 +68,8 @@ fn main() { "src/utils/streams.go", "src/utils/common.go", "src/config/mod.go", - ], + ] + .into_iter(), ); generate_models_folder(&async_config, output_path); diff --git a/src/utils.rs b/src/utils.rs index bdb9dd8..49349a3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,10 @@ use std::{ fs::{self, File, OpenOptions}, io::{self, Error, Read, Write}, - path::{Path, PathBuf}, + path::Path, }; /// writes a file and recursivly creates the directory it is in -pub fn write_to_path_create_dir(content: &str, path: &PathBuf) -> Result<(), Error> { +pub fn write_to_path_create_dir(content: &str, path: &Path) -> Result<(), Error> { fs::create_dir_all(path.parent().unwrap())?; let mut out_file = File::create(path)?; out_file.write_all(content.as_bytes())