Skip to content

Commit

Permalink
fix: put flavors/flavor into context even when matrix is used (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy authored Apr 2, 2024
1 parent 1d708ac commit 58dc4b5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
1 change: 1 addition & 0 deletions whiskers/examples/multi-output/output-a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Latte variant a
1 change: 1 addition & 0 deletions whiskers/examples/multi-output/output-b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Latte variant b
8 changes: 8 additions & 0 deletions whiskers/examples/multi-output/single-flavor.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
whiskers:
version: "2.0.0"
matrix:
- variant: [a, b]
filename: 'output-{{variant}}.txt'
---
{{flavor.name}} variant {{variant}}
47 changes: 20 additions & 27 deletions whiskers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,31 @@ fn main() -> anyhow::Result<()> {
ctx.insert(key, &value);
}

// build the Tera engine and palette
let mut tera = templating::make_engine();
tera.add_raw_template(&template_name, &doc.body)
.context("Template is invalid")?;
// build the palette and add it to the templating context
let palette = models::build_palette(
template_opts.capitalize_hex,
template_opts.hex_prefix.as_deref(),
args.color_overrides.as_ref(),
)
.context("Palette context cannot be built")?;

ctx.insert("flavors", &palette.flavors);
if let Some(flavor) = args.flavor {
let flavor: catppuccin::FlavorName = flavor.into();
let flavor = &palette.flavors[flavor.identifier()];
ctx.insert("flavor", flavor);

// also throw in the flavor's colors for convenience
for (_, color) in flavor {
ctx.insert(&color.identifier, &color);
}
}

// build the Tera engine
let mut tera = templating::make_engine();
tera.add_raw_template(&template_name, &doc.body)
.context("Template is invalid")?;

if let Some(matrix) = template_opts.matrix {
let Some(filename_template) = template_opts.filename else {
anyhow::bail!("Filename template is required for multi-output render");
Expand All @@ -148,15 +162,8 @@ fn main() -> anyhow::Result<()> {
c.ok_or_else(|| anyhow!("--check requires a file argument in single-output mode"))
})
.transpose()?;
render_single_output(
args.flavor.map(Into::into),
&ctx,
&palette,
&tera,
&template_name,
check,
)
.context("Single-output render failed")?;
render_single_output(&ctx, &tera, &template_name, check)
.context("Single-output render failed")?;
}

Ok(())
Expand Down Expand Up @@ -229,25 +236,11 @@ fn template_is_compatible(template_opts: &TemplateOptions) -> bool {
}

fn render_single_output(
flavor: Option<FlavorName>,
ctx: &tera::Context,
palette: &models::Palette,
tera: &tera::Tera,
template_name: &str,
check: Option<PathBuf>,
) -> Result<(), anyhow::Error> {
let mut ctx = ctx.clone();
ctx.insert("flavors", &palette.flavors);
if let Some(flavor) = flavor {
let flavor = &palette.flavors[flavor.identifier()];
ctx.insert("flavor", flavor);

// also throw in the flavor's colors for convenience
for (_, color) in flavor {
ctx.insert(&color.identifier, &color);
}
}

let result = tera
.render(template_name, &ctx)
.context("Template render failed")?;
Expand Down

0 comments on commit 58dc4b5

Please sign in to comment.