Skip to content

Commit

Permalink
chore(forge): Fix MiniJinja template loader for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
lquerel committed May 8, 2024
1 parent 2f2ce45 commit dd0dc61
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

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

11 changes: 7 additions & 4 deletions crates/weaver_codegen_test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ fn create_single_generated_rs_file(root: &Path) {
.to_str()
.expect("Failed to convert to string");
let parent_modules = relative_path.parent().map_or(vec![], |parent| {
parent.components().filter_map(|component| match component {
Component::Normal(part) => Some(part.to_string_lossy().into_owned()),
_ => None,
}).collect()
parent
.components()
.filter_map(|component| match component {
Component::Normal(part) => Some(part.to_string_lossy().into_owned()),
_ => None,
})
.collect()
});

// Skip generated.rs
Expand Down
83 changes: 41 additions & 42 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

#![doc = include_str!("../README.md")]

use std::{fs, io};
use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::{fs, io};

use minijinja::{Environment, ErrorKind, State, Value};
use minijinja::syntax::SyntaxConfig;
use minijinja::value::{from_args, Object};
use minijinja::{Environment, ErrorKind, State, Value};
use rayon::iter::IntoParallelIterator;
use rayon::iter::ParallelIterator;
use serde::Serialize;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Object for TemplateObject {
args: &[Value],
) -> Result<Value, minijinja::Error> {
if name == "set_file_name" {
let (file_name, ): (&str, ) = from_args(args)?;
let (file_name,): (&str,) = from_args(args)?;
file_name.clone_into(&mut self.file_name.lock().expect("Lock poisoned"));
Ok(Value::from(""))
} else {
Expand Down Expand Up @@ -254,13 +254,7 @@ impl TemplateEngine {
}
};

// @todo - remove
println!("cargo:warning=Scanning file {:?}", relative_path);

for template in tmpl_matcher.matches(relative_path) {
// @todo - remove
println!("cargo:warning=Processing file {:?}", relative_path);

let filtered_result = match template.filter.apply(context.clone()) {
Ok(result) => result,
Err(e) => return Some(e),
Expand All @@ -274,8 +268,8 @@ impl TemplateEngine {
NewContext {
ctx: &filtered_result,
}
.try_into()
.ok()?,
.try_into()
.ok()?,
relative_path,
output_dir,
) {
Expand Down Expand Up @@ -309,8 +303,8 @@ impl TemplateEngine {
NewContext {
ctx: &filtered_result,
}
.try_into()
.ok()?,
.try_into()
.ok()?,
relative_path,
output_dir,
) {
Expand Down Expand Up @@ -347,19 +341,18 @@ impl TemplateEngine {
engine.add_global("template", Value::from_object(template_object.clone()));

log.loading(&format!("Generating file {}", template_file));
// @todo - remove
println!("cargo:warning=Loading file {:?}", template_file);

let template = engine
.get_template(template_file)
.map_err(|e| {
let templates = engine.templates().map(|(name, _)| name.to_owned()).collect::<Vec<_>>();
let error = format!("{}. Available templates: {:?}", e, templates);
InvalidTemplateFile {
template: template_file.into(),
error,
}
})?;
let template = engine.get_template(template_file).map_err(|e| {
let templates = engine
.templates()
.map(|(name, _)| name.to_owned())
.collect::<Vec<_>>();
let error = format!("{}. Available templates: {:?}", e, templates);
InvalidTemplateFile {
template: template_file.into(),
error,

Check warning on line 353 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L346-L353

Added lines #L346 - L353 were not covered by tests
}
})?;

let output = template
.render(ctx.clone())
Expand Down Expand Up @@ -521,13 +514,14 @@ fn cross_platform_loader<'x, P: AsRef<Path> + 'x>(
let dir = dir.as_ref().to_path_buf();
move |name| {
let path = safe_join(&dir, name)?;
println!("cargo:warning=Loader loading template base: {:?}, path:{:?}", dir, path);
match fs::read_to_string(path) {
Ok(result) => Ok(Some(result)),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(
minijinja::Error::new(ErrorKind::InvalidOperation, "could not read template").with_source(err),
),
Err(err) => Err(minijinja::Error::new(
ErrorKind::InvalidOperation,
"could not read template",

Check warning on line 522 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L519-L522

Added lines #L519 - L522 were not covered by tests
)
.with_source(err)),

Check warning on line 524 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L524

Added line #L524 was not covered by tests
}
}
}
Expand All @@ -539,14 +533,18 @@ fn safe_join(root: &Path, template: &str) -> Result<PathBuf, minijinja::Error> {
path.push(template);

// Canonicalize the paths to resolve any `..` or `.` components
let canonical_root = root.canonicalize()
.map_err(|e| minijinja::Error::new(
let canonical_root = root.canonicalize().map_err(|e| {
minijinja::Error::new(
ErrorKind::InvalidOperation,
format!("Failed to canonicalize root path: {}", e)))?;
let canonical_combined = path.canonicalize()
.map_err(|e| minijinja::Error::new(
format!("Failed to canonicalize root path: {}", e),

Check warning on line 539 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L537-L539

Added lines #L537 - L539 were not covered by tests
)
})?;
let canonical_combined = path.canonicalize().map_err(|e| {
minijinja::Error::new(
ErrorKind::InvalidOperation,
format!("Failed to canonicalize combined path: {}", e)))?;
format!("Failed to canonicalize combined path: {}", e),

Check warning on line 545 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L543-L545

Added lines #L543 - L545 were not covered by tests
)
})?;

// Verify that the canonical combined path starts with the canonical root path
if canonical_combined.starts_with(&canonical_root) {
Expand All @@ -557,7 +555,8 @@ fn safe_join(root: &Path, template: &str) -> Result<PathBuf, minijinja::Error> {
format!(

Check warning on line 555 in crates/weaver_forge/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_forge/src/lib.rs#L553-L555

Added lines #L553 - L555 were not covered by tests
"The combined path is not a subdirectory of the root path: {:?} -> {:?}",
canonical_root, canonical_combined
)))
),
))
}
}

Expand Down Expand Up @@ -756,12 +755,12 @@ mod tests {
schema.registry(registry_id).expect("registry not found"),
schema.catalog(),
)
.unwrap_or_else(|e| {
panic!(
"Failed to create the context for the template evaluation: {:?}",
e
)
});
.unwrap_or_else(|e| {
panic!(
"Failed to create the context for the template evaluation: {:?}",
e
)
});

engine
.generate(
Expand Down

0 comments on commit dd0dc61

Please sign in to comment.