Skip to content

Commit

Permalink
The process_file function does not need the path argument, perfor…
Browse files Browse the repository at this point in the history
…m check in `main` instead
  • Loading branch information
jessebraham committed Oct 31, 2024
1 parent c8bba0d commit 7e63302
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
env,
fs,
path::{Path, PathBuf},
process,
};
Expand Down Expand Up @@ -249,16 +250,19 @@ fn main() {
}
}

let dir = path.join(&args.name);
std::fs::create_dir(&dir).unwrap();
let project_dir = path.join(&args.name);
fs::create_dir(&project_dir)?;

for &(file_path, contents) in template_files::TEMPLATE_FILES.iter() {
let path = dir.join(file_path);
if file_path == "Cargo.lock" {
continue;
}

if let Some(processed) = process_file(contents, &selected, &variables) {
let file_path = project_dir.join(file_path);

let processed = process_file(file_path, contents, &selected, &variables);
if let Some(processed) = processed {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, processed).unwrap();
fs::create_dir_all(file_path.parent().unwrap())?;
fs::write(file_path, processed)?;
}
}

Expand Down Expand Up @@ -287,19 +291,10 @@ fn main() {
}

fn process_file(
// Path to the file
path: &str,
// Raw content of the file
contents: &str,
// Selected options
options: &[String],
// Variables and its value in a tuple
variables: &[(String, String)],
contents: &str, // Raw content of the file
options: &[String], // Selected options
variables: &[(String, String)], // Variables and their values in tuples
) -> Option<String> {
if path.ends_with("Cargo.lock") {
return None;
}

let mut res = String::new();

let mut replace = None;
Expand Down Expand Up @@ -455,7 +450,6 @@ mod test {
#[test]
fn test_nested_if_else1() {
let res = process_file(
"/foo",
r#"
#IF option("opt1")
opt1
Expand Down Expand Up @@ -486,7 +480,6 @@ mod test {
#[test]
fn test_nested_if_else2() {
let res = process_file(
"/foo",
r#"
#IF option("opt1")
opt1
Expand Down Expand Up @@ -516,7 +509,6 @@ mod test {
#[test]
fn test_nested_if_else3() {
let res = process_file(
"/foo",
r#"
#IF option("opt1")
opt1
Expand Down Expand Up @@ -547,7 +539,6 @@ mod test {
#[test]
fn test_nested_if_else4() {
let res = process_file(
"/foo",
r#"
#IF option("opt1")
#IF option("opt2")
Expand Down Expand Up @@ -576,7 +567,6 @@ mod test {
#[test]
fn test_nested_if_else5() {
let res = process_file(
"/foo",
r#"
#IF option("opt1")
#IF option("opt2")
Expand Down

0 comments on commit 7e63302

Please sign in to comment.