Skip to content

Commit

Permalink
Postprocess extra generated file
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-mman committed Mar 7, 2025
1 parent b86df55 commit a6a649b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ impl Generator<'_> {

let out_file = BufWriter::new(File::create(&file_path)?);

self.tpl.render_to_write(ctx, out_file)?;
let state = self.tpl.render_to_write(ctx, out_file)?;

if !self.flags.no_postprocess {
if let Some(extra_generated_file) = state.get_temp("extra_generated_file") {
self.postprocessor
.add_path(Utf8Path::new(extra_generated_file.as_str().unwrap()));
}
self.postprocessor.add_path(&file_path);
}

Expand Down
5 changes: 3 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use heck::{
ToLowerCamelCase as _, ToShoutySnakeCase as _, ToSnakeCase as _, ToUpperCamelCase as _,
};
use itertools::Itertools as _;
use minijinja::{path_loader, value::Kwargs, Value};
use minijinja::{path_loader, value::Kwargs, State, Value};

pub(crate) fn env(tpl_dir: &Utf8Path) -> Result<minijinja::Environment<'static>, minijinja::Error> {
let mut env = minijinja::Environment::new();
Expand Down Expand Up @@ -146,8 +146,9 @@ pub(crate) fn env(tpl_dir: &Utf8Path) -> Result<minijinja::Environment<'static>,
env.add_function(
// For java lib we need to create extra files.
"generate_extra_file",
|filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
|state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
fs::write(&*filename, file_contents.as_bytes()).unwrap();
state.set_temp("extra_generated_file", filename.into());
},
);

Expand Down

0 comments on commit a6a649b

Please sign in to comment.