Skip to content

Commit

Permalink
feat(libmake): ✨ added loggers functions, refactored templates to ens…
Browse files Browse the repository at this point in the history
…ure local folder takes priority
  • Loading branch information
sebastienrousseau committed Nov 7, 2023
1 parent 04f7c0f commit 5a70c84
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 28 deletions.
71 changes: 46 additions & 25 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use crate::interface::replace_placeholders;
use reqwest::blocking::get;

Check warning on line 5 in src/generator.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `reqwest::blocking::get`

Check warning on line 5 in src/generator.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `reqwest::blocking::get`

Check warning on line 5 in src/generator.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `reqwest::blocking::get`

Check warning on line 5 in src/generator.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `reqwest::blocking::get`

Check failure on line 5 in src/generator.rs

View workflow job for this annotation

GitHub Actions / Lint

unused import: `reqwest::blocking::get`
// use assert_cmd::Command;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_yaml;
Expand All @@ -16,13 +15,11 @@ use std::{
///
/// # Description
///
/// * The parameters are optional, but the output directory is required.
/// * The output directory is the directory where the project files will be created.
/// * The `output` directory is the directory where the project files will be created.
/// * The other parameters are optional and will be used to replace the placeholders in the template files.
/// * The template files are located in the template directory of the project.
/// * The template files are copied to the output directory and the placeholders are replaced with the values of the parameters.
///
///
#[derive(
Clone,
Debug,
Expand Down Expand Up @@ -58,7 +55,7 @@ pub struct FileGenerationParams {
pub license: Option<String>,
/// The name of the project (optional).
pub name: Option<String>,
/// The output directory where the project files will be created (required).
/// The output directory where the project files will be created (optional).
pub output: Option<String>,
/// The name of the readme file (optional).
pub readme: Option<String>,
Expand All @@ -73,7 +70,8 @@ pub struct FileGenerationParams {
}

impl FileGenerationParams {
/// Creates a default instance with default values.
/// Creates a default instance with default values for all fields.
/// Fields that are truly optional without a default are initialized as `None`.
pub fn default_params() -> Self {
Self {
author: Some("John Smith".to_string()),
Expand Down Expand Up @@ -211,34 +209,42 @@ pub fn create_template_folder() -> io::Result<()> {
"example.tpl",
"gitignore.tpl",
"lib.tpl",
"loggers.tpl",
"macros.tpl",
"main.tpl",
"README.tpl",
"rustfmt.tpl",
"TEMPLATE.tpl",
"test.tpl",
"test_loggers.tpl"
];
for file in &files {
let file_url = format!("{}{}", url, file);
let file_path = template_dir_path.join(file);
let response = get(&file_url).map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("Failed to download template file: {}", e),
)
})?;
let file_contents = response.text().map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("Failed to read response body: {}", e),
)
})?;
std::fs::write(
&file_path,
file_contents
.trim_start_matches('\n')
.trim_end_matches('\n'),
)?;
// Check if the file already exists
if !file_path.exists() {
let file_url = format!("{}{}", url, file);
let response = reqwest::blocking::get(&file_url).map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("Failed to download template file: {}", e),
)
})?;

let file_contents = response.text().map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("Failed to read response body: {}", e),
)
})?;

// Write the file contents, trimming any leading or trailing newline characters
std::fs::write(
&file_path,
file_contents
.trim_start_matches('\n')
.trim_end_matches('\n'),
)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -426,6 +432,13 @@ pub fn generate_files(params: FileGenerationParams) -> io::Result<()> {
&project_directory,
&params,
)?;
// Copying the `lib.tpl` file to the new library directory
copy_and_replace_template(
"loggers.tpl",
"src/loggers.rs",
&project_directory,
&params,
)?;
// Copying the `macros.tpl` file to the new library directory
copy_and_replace_template(
"macros.tpl",
Expand Down Expand Up @@ -469,6 +482,14 @@ pub fn generate_files(params: FileGenerationParams) -> io::Result<()> {
&params,
)?;

// Copying the `test.tpl` file to the new library directory
copy_and_replace_template(
"test_loggers.tpl",
"tests/test_loggers.rs",
&project_directory,
&params,
)?;

// Displaying the argument and value pairs
println!("{:<15}Value", "Argument");
println!("{:<15}{}", "author", params.author.unwrap_or_default());
Expand Down
9 changes: 7 additions & 2 deletions template/Cargo.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ path = "benches/criterion.rs"
debug = true

[dependencies]
serde = { version = "1.0.162", features = ["derive"] }
serde_json = "1.0.96"
anyhow = "1.0.75"
dtt = "0.0.4"
serde = { version = "1.0.190", features = ["derive"] }
serde_json = "1.0.108"
serde_yaml = "0.9.27"
toml = "0.8.6"
vrd = "0.0.4"

[dev-dependencies]
criterion = "0.4.0"
Expand Down
5 changes: 4 additions & 1 deletion template/lib.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
#![crate_name = "{name}"]
#![crate_type = "lib"]

/// The `macros` module.
/// The `loggers` module contains the loggers for the library.
pub mod loggers;

/// The `macros` module contains functions for generating macros.
pub mod macros;

use serde::{Deserialize, Serialize};
Expand Down
Loading

0 comments on commit 5a70c84

Please sign in to comment.