Skip to content

Commit

Permalink
Merge pull request #2 from A2-ai/file-renaming
Browse files Browse the repository at this point in the history
wip: add copy context for file name replacement without j2 extension
  • Loading branch information
andriygm authored Jul 5, 2024
2 parents 4860567 + e543b6b commit 8fd9caf
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
16 changes: 15 additions & 1 deletion cli/src/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use spackle::{
},
util::copy,
};
use tera::Context;

use crate::Cli;

Expand Down Expand Up @@ -79,7 +80,20 @@ pub fn run(

let start_time = Instant::now();

match copy::copy(&project_dir, 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();
slot_data.iter().for_each(|(key, value)| {
context.insert(key, value);
});
match copy::copy(&project_dir, out, &config.ignore, &context) {
Ok(r) => {
println!(
"{} {} {} {}",
Expand Down
8 changes: 0 additions & 8 deletions lefthook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
use std::{collections::HashMap, path::PathBuf};

use futures::Stream;
use tera::Context;
use util::copy;

pub mod core;
Expand Down Expand Up @@ -60,7 +61,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 = template::fill(project_dir, out_dir, slot_data)
Expand Down Expand Up @@ -94,7 +97,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 = template::fill(project_dir, out_dir, slot_data)
Expand Down
21 changes: 19 additions & 2 deletions src/util/copy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fmt::Display, fs, path::PathBuf};

use tera::{Context, Tera};
use walkdir::WalkDir;

use crate::core::{config::CONFIG_FILE, template::TEMPLATE_EXT};
Expand Down Expand Up @@ -27,7 +28,12 @@ pub struct CopyResult {
pub skipped_count: usize,
}

pub fn copy(src: &PathBuf, dest: &PathBuf, skip: &Vec<String>) -> Result<CopyResult, Error> {
pub fn copy(
src: &PathBuf,
dest: &PathBuf,
skip: &Vec<String>,
context: &Context,
) -> Result<CopyResult, Error> {
let mut copied_count = 0;
let mut skipped_count = 0;

Expand Down Expand Up @@ -69,7 +75,18 @@ pub fn copy(src: &PathBuf, dest: &PathBuf, skip: &Vec<String>) -> Result<CopyRes
source: e.into(),
path: src_path.to_path_buf(),
})?;
let dst_path = dest.join(relative_path);
let dst_path_maybe_template = dest.join(relative_path);

let dst_path: PathBuf = Tera::one_off(
&dst_path_maybe_template.to_string_lossy(),
context,
false,
// TODO: fixup unwrap - not sure what situations this could panic in
// assuming without need for escaping this should just replace a template
// if it exists but otherwise will just carry on forward.
)
.unwrap()
.into();

if entry.file_type().is_dir() {
fs::create_dir_all(&dst_path).map_err(|e| Error {
Expand Down
34 changes: 32 additions & 2 deletions tests/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs;

use spackle::util::copy;
use tempdir::TempDir;
use tera::Context;

#[test]
fn ignore_one() {
Expand All @@ -16,7 +17,9 @@ fn ignore_one() {
.unwrap();
}

copy::copy(&src_dir, &dst_dir, &vec!["file-0.txt".to_string()]).unwrap();
let mut context = Context::new();
context.insert("foo", &"bar");
copy::copy(&src_dir, &dst_dir, &vec!["file-0.txt".to_string()], &context).unwrap();

for i in 0..3 {
if i == 0 {
Expand Down Expand Up @@ -45,7 +48,9 @@ fn ignore_subdir() {

fs::write(subdir.join("file-0.txt"), "file-0.txt").unwrap();

copy::copy(&src_dir, &dst_dir, &vec!["file-0.txt".to_string()]).unwrap();
let mut context = Context::new();
context.insert("foo", &"bar");
copy::copy(&src_dir, &dst_dir, &vec!["file-0.txt".to_string()], &context).unwrap();

assert!(!dst_dir.join("subdir").join("file-0.txt").exists());

Expand All @@ -57,3 +62,28 @@ fn ignore_subdir() {
}
}
}

#[test]
fn replace_file_name() {
let src_dir = TempDir::new("spackle").unwrap().into_path();
let dst_dir = TempDir::new("spackle").unwrap().into_path();

// a file that has template structure in its name but does not end with .j2
// should still be replaced, while leavings its contents untouched.
// .j2 extensions should representing which files have _contents_ that need
// replacing.
fs::write(
src_dir.join(format!("{}.tmpl", "{{template_name}}")),
// copy will not do any replacement so contents should remain as is
"{{project_name}}",
)
.unwrap();
assert!(src_dir.join("{{template_name}}.tmpl").exists());

let mut context = Context::new();
context.insert("template_name", &"template");
context.insert("project_name", &"foo");
copy::copy(&src_dir, &dst_dir, &vec![], &context).unwrap();

assert!(dst_dir.join("template.tmpl").exists(), "template.tmpl does not exist");
}

0 comments on commit 8fd9caf

Please sign in to comment.