Skip to content

Commit

Permalink
Embed dependancies (#86)
Browse files Browse the repository at this point in the history
* dependencies are now embedded
* added extra functions for rendering embedded files
* render_multiple_embedded_templates now takes Iterable
* changed PathBuf to Path where possible
* added a few comments
  • Loading branch information
bird-dancer committed Jul 8, 2023
1 parent 6aa2c7d commit 37648d4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
42 changes: 24 additions & 18 deletions src/generator/common.rs
Original file line number Diff line number Diff line change
@@ -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"); `
Expand All @@ -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);
Expand All @@ -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<gtmpl::Value>,
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<String>,
context_ref: impl Into<gtmpl::Value>,
output_path: &Path,
) {
let mut render = match render_template(template, context_ref, TEMPLATE_FUNCTIONS) {
Ok(render) => render,
Err(e) => {
eprintln!("❌ Error rendering template: {}", e);
Expand Down Expand Up @@ -92,21 +97,22 @@ fn render_template<T: Into<String>, C: Into<gtmpl::Value>, F: Into<String> + 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<Item = &'a str>,
) {
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),
Expand Down
5 changes: 4 additions & 1 deletion src/generator/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/generator/model.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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!(
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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",
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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())
Expand Down

0 comments on commit 37648d4

Please sign in to comment.