diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-09-03 10:21:08 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-09-04 10:30:18 +0300 |
commit | 803a8b1d879e0c7ec005b2652f06459f58956379 (patch) | |
tree | d7e87cb4c403bede8d4b83bc72f2d44149a6341d | |
parent | 6c88187985ce5bb9203619181ab667b541c7cd55 (diff) | |
download | subplot-803a8b1d879e0c7ec005b2652f06459f58956379.tar.gz |
feat(codegen): allow templates to list helper files
A template's YAML file can now list any additional files, called
helpers. An empty list is OK, as is not having the 'helper' key in the
YAML file. The helpers are loaded as Tera templates so they can be
included in the main template.
This will allow us to split out un-templated code out of the main
template. The helpers can then be linted and unit tested separately.
This commit only adds the mechanism, and fixes up the YAML files.
Later commits will actually split the main Python template into
helpers, and add unit tests.
refactor: use .join
-rw-r--r-- | src/codegen.rs | 7 | ||||
-rw-r--r-- | src/templatespec.rs | 16 | ||||
-rw-r--r-- | templates/python/template.yaml | 1 |
3 files changed, 22 insertions, 2 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index c1da2f8..ca70bcf 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -31,7 +31,12 @@ pub fn generate_test_program( filename: &Path, ) -> Result<()> { let context = context(doc)?; - let tera = tera(&spec)?; + let mut tera = tera(&spec)?; + let dirname = spec.template_filename().parent().unwrap(); + for helper in spec.helpers() { + let helper_path = dirname.join(helper); + tera.add_template_file(helper_path, helper.to_str())?; + } let code = tera.render("template", &context).expect("render"); write(filename, &code)?; Ok(()) diff --git a/src/templatespec.rs b/src/templatespec.rs index dc85f1d..23f5c86 100644 --- a/src/templatespec.rs +++ b/src/templatespec.rs @@ -15,6 +15,8 @@ use std::path::{Path, PathBuf}; #[derive(Debug, Deserialize)] pub struct TemplateSpec { template: PathBuf, + #[serde(default)] + helpers: Vec<PathBuf>, run: Option<String>, } @@ -25,9 +27,15 @@ impl TemplateSpec { } // Create a new TemplateSpec. - fn new(basedir: &Path, template: &Path, run: Option<&str>) -> TemplateSpec { + fn new( + basedir: &Path, + template: &Path, + helpers: Vec<PathBuf>, + run: Option<&str>, + ) -> TemplateSpec { TemplateSpec { template: basedir.to_path_buf().join(template), + helpers, run: match run { Some(x) => Some(x.to_string()), None => None, @@ -52,6 +60,7 @@ impl TemplateSpec { Ok(TemplateSpec::new( &dirname, spec.template_filename(), + spec.helpers().map(|p| p.to_path_buf()).collect(), spec.run(), )) } @@ -61,6 +70,11 @@ impl TemplateSpec { &self.template } + /// Return iterator for names of helper files. + pub fn helpers(&self) -> impl Iterator<Item = &Path> { + self.helpers.iter().map(|p| p.as_path()) + } + /// Return command to run the generated test program, if specified. /// /// The name of the test program gets appended. diff --git a/templates/python/template.yaml b/templates/python/template.yaml index dca668d..c3ded5a 100644 --- a/templates/python/template.yaml +++ b/templates/python/template.yaml @@ -1,2 +1,3 @@ template: template.py +helpers: [] run: python3 |