Skip to content

Commit

Permalink
feat(whiskers): write results to filename in single file mode (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter authored May 23, 2024
1 parent 5acca10 commit 4715155
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions whiskers/examples/single-file/overrides/input.tera
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
whiskers:
version: "2.0.0"
filename: "output.md"

# Set default accent color
accent: "mauve"
Expand Down
2 changes: 1 addition & 1 deletion whiskers/examples/single-file/overrides/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ whiskers_cmd := "cargo run --bin whiskers --"

# Generate a single file containing all four flavors
gen:
@{{whiskers_cmd}} input.tera > output.md
@{{whiskers_cmd}} input.tera
1 change: 1 addition & 0 deletions whiskers/examples/single-file/simple/input.tera
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
whiskers:
version: "2.0.0"
filename: "output.md"
---
# Catppuccin Palette v0.2.0

Expand Down
2 changes: 1 addition & 1 deletion whiskers/examples/single-file/simple/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ whiskers_cmd := "cargo run --bin whiskers --"

# Generate a single file containing all four flavors
gen:
@{{whiskers_cmd}} input.tera > output.md
@{{whiskers_cmd}} input.tera
47 changes: 34 additions & 13 deletions whiskers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,16 @@ fn main() -> anyhow::Result<()> {
c.ok_or_else(|| anyhow!("--check requires a file argument in single-output mode"))
})
.transpose()?;
render_single_output(&ctx, &tera, &template_name, check)
.context("Single-output render failed")?;

render_single_output(
&ctx,
&tera,
&template_name,
check,
template_opts.filename,
args.dry_run,
)
.context("Single-output render failed")?;
}

Ok(())
Expand Down Expand Up @@ -274,18 +282,40 @@ fn template_is_compatible(template_opts: &TemplateOptions) -> bool {
true
}

fn write_template(dry_run: bool, filename: String, result: String) -> Result<(), anyhow::Error> {
let filename = Path::new(&filename);

if dry_run || cfg!(test) {
println!(
"Would write {} bytes into {}",
result.as_bytes().len(),
filename.display()
);
} else {
maybe_create_parents(filename)?;
std::fs::write(filename, result)
.with_context(|| format!("Couldn't write to {}", filename.display()))?;
}

Ok(())
}

fn render_single_output(
ctx: &tera::Context,
tera: &tera::Tera,
template_name: &str,
check: Option<PathBuf>,
filename: Option<String>,
dry_run: bool,
) -> Result<(), anyhow::Error> {
let result = tera
.render(template_name, ctx)
.context("Template render failed")?;

if let Some(path) = check {
check_result_with_file(&path, &result).context("Check mode failed")?;
} else if let Some(filename) = filename {
write_template(dry_run, filename, result)?;
} else {
print!("{result}");
}
Expand Down Expand Up @@ -327,20 +357,11 @@ fn render_multi_output(
.context("Main template render failed")?;
let filename = tera::Tera::one_off(filename_template, &ctx, false)
.context("Filename template render failed")?;
let filename = Path::new(&filename);

if args.dry_run || cfg!(test) {
println!(
"Would write {} bytes into {}",
result.as_bytes().len(),
filename.display()
);
} else if args.check.is_some() {
if args.check.is_some() {
check_result_with_file(&filename, &result).context("Check mode failed")?;
} else {
maybe_create_parents(filename)?;
std::fs::write(filename, result)
.with_context(|| format!("Couldn't write to {}", filename.display()))?;
write_template(args.dry_run, filename, result)?;
}
}

Expand Down

0 comments on commit 4715155

Please sign in to comment.