From 5784dfecd50440a2f4b9beaaca10736af05b03aa Mon Sep 17 00:00:00 2001 From: Devin Pastoor Date: Sun, 30 Jun 2024 13:13:49 -0400 Subject: [PATCH] wip: add copy context for file name replacement without j2 extension --- Cargo.lock | 1 + cli/Cargo.toml | 1 + cli/src/main.rs | 16 +++++++++++++++- lefthook.yaml | 8 -------- src/lib.rs | 9 +++++++-- src/util/copy.rs | 16 +++++++++++++--- tests/copy.rs | 34 ++++++++++++++++++++++++++++++++-- 7 files changed, 69 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3e491e..242a2d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1866,6 +1866,7 @@ dependencies = [ "rocket", "rust-embed", "spackle", + "tera", ] [[package]] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index c78e178..4f05bb2 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -13,3 +13,4 @@ clap = { version = "4.5.4", features = ["derive"] } colored = "2.1.0" rocket = { version = "0.5.1", features = ["json"] } rust-embed = "8.4.0" +tera = "1.20.0" diff --git a/cli/src/main.rs b/cli/src/main.rs index 9a6346e..0c40b43 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -7,6 +7,7 @@ use spackle::{ }, util::copy, }; +use tera::Context; use std::{collections::HashMap, error::Error, path::PathBuf, process::exit, time::Instant}; #[derive(Parser)] @@ -162,7 +163,20 @@ fn main() { let start_time = Instant::now(); - match copy::copy(&project_dir, &cli.out, &config.ignore) { + // CR(devin): when looking at the below code, this likely should be pushed + // into the spackle lib itself, there are too many implementation details + // in the CLi that would also need to be replicated in any api/other client + // when by the time you get to actually rendering the template + // the fact this is touching like a util related module shows its + // breaking the ideal implementation boundaries. + + // TODO: refactor the data_entries and context boundaries after considering + // the api surface area + let mut context = Context::new(); + data_entries.iter().for_each(|(key, value)| { + context.insert(key, value); + }); + match copy::copy(&project_dir, &cli.out, &config.ignore, &context) { Ok(r) => { println!( "{} {} {} {}", diff --git a/lefthook.yaml b/lefthook.yaml index 16e7dd0..63ed2ea 100644 --- a/lefthook.yaml +++ b/lefthook.yaml @@ -3,11 +3,3 @@ pre-commit: commands: run-tests: run: cargo test --workspace - - check-frontend-format: - root: frontend - run: bun x biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} && git update-index --again - - check-frontend-types: - root: frontend - run: bun tsc --noEmit \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a37fd75..ec143cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use core::{ use std::{collections::HashMap, path::PathBuf}; use futures::Stream; +use tera::Context; use util::copy; pub mod core; @@ -58,7 +59,9 @@ pub fn generate_stream( let config = config::load(project_dir).map_err(Error::ConfigError)?; // Copy all non-template files to the output directory - copy::copy(project_dir, &out_dir, &config.ignore).map_err(Error::CopyError)?; + // TODO: must actually pass in context + let context = &Context::new(); + copy::copy(project_dir, &out_dir, &config.ignore, context).map_err(Error::CopyError)?; // Render template files to the output directory let results = @@ -90,7 +93,9 @@ pub fn generate( let config = config::load(project_dir).map_err(Error::ConfigError)?; // Copy all non-template files to the output directory - copy::copy(project_dir, &out_dir, &config.ignore).map_err(Error::CopyError)?; + // TODO: must actually pass in context + let context = &Context::new(); + copy::copy(project_dir, &out_dir, &config.ignore, context).map_err(Error::CopyError)?; // Render template files to the output directory let results = diff --git a/src/util/copy.rs b/src/util/copy.rs index 978835f..5624e84 100644 --- a/src/util/copy.rs +++ b/src/util/copy.rs @@ -1,5 +1,6 @@ -use std::{fmt::Display, fs, path::PathBuf}; +use std::{fmt::Display, fs, path::{Path, PathBuf}}; +use tera::{Context, Tera}; use walkdir::WalkDir; use crate::core::{config::CONFIG_FILE, template::TEMPLATE_EXT}; @@ -27,7 +28,7 @@ pub struct CopyResult { pub skipped_count: usize, } -pub fn copy(src: &PathBuf, dest: &PathBuf, skip: &Vec) -> Result { +pub fn copy(src: &PathBuf, dest: &PathBuf, skip: &Vec, context: &Context) -> Result { let mut copied_count = 0; let mut skipped_count = 0; @@ -69,7 +70,16 @@ pub fn copy(src: &PathBuf, dest: &PathBuf, skip: &Vec) -> Result