Skip to content

Commit

Permalink
[bndbuild] Add jinja templating !
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusty/Benediction committed Jul 13, 2024
1 parent 4fef05b commit 3503d76
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
16 changes: 13 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cpclib-bndbuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ shlex = "1.3.0"
topologic = "1.1.0"
lazy-regex = "3.1.0"
dot-writer = "0.1.3"
minijinja = "*"

cpclib-common = {workspace=true, features=["cmdline"]}
cpclib-basm = { workspace=true, default-features=false, features=["xferlib"] }
Expand Down
33 changes: 25 additions & 8 deletions cpclib-bndbuild/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::Deref;
use std::path::Path;

use cpclib_common::itertools::Itertools;
use minijinja::{context, Environment};

use crate::rules::{self, Graph, Rule};
use crate::BndBuilderError;
Expand Down Expand Up @@ -46,6 +47,11 @@ impl BndBuilder {
}

pub fn from_fname<P: AsRef<Path>>(fname: P) -> Result<Self, BndBuilderError> {
let content = Self::decode_from_fname(fname)?;
Self::from_string(content)
}

pub fn decode_from_fname<P: AsRef<Path>>(fname: P) -> Result<String, BndBuilderError> {
let fname = fname.as_ref();
let file = std::fs::File::open(fname).map_err(|e| {
BndBuilderError::InputFileError {
Expand All @@ -58,24 +64,20 @@ impl BndBuilder {
let working_directory = if path.is_dir() { Some(path) } else { None };

let rdr = BufReader::new(file);
Self::from_reader(rdr, working_directory)
Self::decode_from_reader(rdr, working_directory)
}

pub fn save<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> {
let contents = self.inner.borrow_owner().to_string();
std::fs::write(path, contents)
}

pub fn from_reader<P: AsRef<Path>>(
pub fn decode_from_reader<P: AsRef<Path>>(
mut rdr: impl Read,
working_directory: Option<P>
) -> Result<Self, BndBuilderError> {
) -> Result<String, BndBuilderError> {
if let Some(working_directory) = working_directory {
let working_directory = working_directory.as_ref();
eprintln!(
"> Set working directory to: {}",
working_directory.display()
);
std::env::set_current_dir(working_directory).map_err(|e| {
BndBuilderError::WorkingDirectoryError {
fname: working_directory.display().to_string(),
Expand All @@ -84,8 +86,23 @@ impl BndBuilder {
})?;
}

// get the content of the file
let mut content = Default::default();
rdr.read_to_string(&mut content)
.map_err(|e| BndBuilderError::AnyError(e.to_string()))?;

// apply jinja templating
let mut env = Environment::new();
env.add_template("bndbuild.yml", &content).unwrap();
let tmpl = env.get_template("bndbuild.yml").unwrap();
tmpl.render(context!())
.map_err(|e| BndBuilderError::AnyError(e.to_string()))
}

pub fn from_string(content: String) -> Result<Self, BndBuilderError> {
// extract information from the file
let rules: rules::Rules =
serde_yaml::from_reader(&mut rdr).map_err(|e| BndBuilderError::ParseError(e))?;
serde_yaml::from_str(&content).map_err(|e| BndBuilderError::ParseError(e))?;

let inner = BndBuilderInner::try_new(rules, |rules| rules.to_deps())?;

Expand Down
17 changes: 15 additions & 2 deletions cpclib-bndbuild/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ fn inner_main() -> Result<(), BndBuilderError> {
.help("Generate the .dot representation of the selected bndbuild.yml file")
.action(ArgAction::SetTrue)
)
.arg(
Arg::new("show")
.long("show")
.help("Show the file AFTER interpreting the templates")
.action(ArgAction::SetTrue)
.conflicts_with("dot")
)
.arg(
Arg::new("file")
.short('f')
Expand All @@ -73,7 +80,7 @@ fn inner_main() -> Result<(), BndBuilderError> {
.long("watch")
.action(ArgAction::SetTrue)
.help("Watch the targets and permanently rebuild them when needed.")
.conflicts_with("dot")
.conflicts_with_all(["dot", "show"])
)
.arg(
Arg::new("list")
Expand Down Expand Up @@ -237,7 +244,13 @@ fn inner_main() -> Result<(), BndBuilderError> {
}
}

let builder = BndBuilder::from_fname(fname)?;
let content = BndBuilder::decode_from_fname(fname)?;
if matches.get_flag("show") {
println!("{content}");
return Ok(());
}

let builder = BndBuilder::from_string(content)?;

if let Some(add) = matches.get_one::<String>("add") {
let targets = [add];
Expand Down
6 changes: 5 additions & 1 deletion docs/bndbuild/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ They are:
- `img2cpc` to make [image conversion](../cpclib-imgconverter)
- `rm` to delete files
- `xfer` to transfer to the CPC with the M4 card
- `extern` to launch any command available on the machine
- `extern` to launch any command available on the machine

## Templating

A jinja-like templating is used to generate the final yaml file : <https://docs.rs/minijinja/latest/minijinja/syntax/index.html>.

0 comments on commit 3503d76

Please sign in to comment.