Skip to content

Commit c3327d2

Browse files
committed
Add --create-file-parents flag
1 parent 108c343 commit c3327d2

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/generator.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
postprocessing::Postprocessor,
1313
template,
1414
types::Types,
15+
GenerateFlags,
1516
};
1617

1718
#[derive(Default, Deserialize)]
@@ -29,7 +30,7 @@ pub(crate) fn generate(
2930
types: Types,
3031
tpl_name: String,
3132
output_dir: &Utf8Path,
32-
no_postprocess: bool,
33+
flags: GenerateFlags,
3334
) -> anyhow::Result<()> {
3435
let (name_without_jinja_suffix, tpl_path) = match tpl_name.strip_suffix(".jinja") {
3536
Some(basename) => (basename, &tpl_name),
@@ -70,7 +71,7 @@ pub(crate) fn generate(
7071
tpl_file_ext,
7172
output_dir,
7273
postprocessor: &postprocessor,
73-
no_postprocess,
74+
flags,
7475
};
7576

7677
match tpl_kind {
@@ -80,7 +81,7 @@ pub(crate) fn generate(
8081
TemplateKind::Summary => generator.generate_summary(types, api)?,
8182
}
8283

83-
if !no_postprocess {
84+
if !flags.no_postprocess {
8485
postprocessor.run_postprocessor();
8586
}
8687

@@ -92,7 +93,7 @@ struct Generator<'a> {
9293
tpl_file_ext: &'a str,
9394
output_dir: &'a Utf8Path,
9495
postprocessor: &'a Postprocessor,
95-
no_postprocess: bool,
96+
flags: GenerateFlags,
9697
}
9798

9899
impl Generator<'_> {
@@ -172,11 +173,15 @@ impl Generator<'_> {
172173
};
173174

174175
let file_path = self.output_dir.join(format!("{basename}.{tpl_file_ext}"));
176+
177+
if self.flags.create_file_parents && !self.output_dir.exists() {
178+
fs::create_dir_all(self.output_dir)?;
179+
}
175180
let out_file = BufWriter::new(File::create(&file_path)?);
176181

177182
self.tpl.render_to_write(ctx, out_file)?;
178183

179-
if !self.no_postprocess {
184+
if !self.flags.no_postprocess {
180185
self.postprocessor.add_path(&file_path);
181186
}
182187

src/main.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ struct GenerateFlags {
6464
/// Write `.codegen.json` file
6565
#[clap(long)]
6666
write_codegen_metadata: bool,
67+
68+
/// When generating a file the parent directories are crated if they don't exist
69+
#[clap(long)]
70+
create_file_parents: bool,
6771
}
6872

6973
fn main() -> anyhow::Result<()> {
@@ -141,7 +145,7 @@ fn analyze_and_generate(
141145
writeln!(types_file, "{types:#?}")?;
142146
}
143147

144-
generate(api, types, template, path, flags.no_postprocess)?;
148+
generate(api, types, template, path, flags)?;
145149
}
146150

147151
println!("done! output written to {path}");

0 commit comments

Comments
 (0)