From fe6608425e6217097aab82b89d4a72b79d94c8da Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 29 Jul 2021 16:24:27 +0200 Subject: Add an option to provide a custom tera template Signed-off-by: Luca Barbato Signed-off-by: Georgy Yakovlev --- src/lib.rs | 12 ++++++++++-- src/main.rs | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2ecb1f3..8f53e17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,7 +123,11 @@ pub fn gen_ebuild_data(manifest_path: Option) -> Result { Ok(EbuildConfig::from_package(root_pkg, crates, licenses)) } -pub fn write_ebuild(ebuild_data: EbuildConfig, ebuild_path: impl AsRef) -> Result<()> { +pub fn write_ebuild( + ebuild_data: EbuildConfig, + ebuild_path: impl AsRef, + template_path: Option>, +) -> Result<()> { // Open the file where we'll write the ebuild let mut file = OpenOptions::new() .write(true) @@ -137,7 +141,11 @@ pub fn write_ebuild(ebuild_data: EbuildConfig, ebuild_path: impl AsRef) -> let mut tera = tera::Tera::default(); let mut context = tera::Context::from_serialize(ebuild_data)?; - tera.add_raw_template("ebuild.tera", include_str!("ebuild.tera"))?; + if let Some(template) = template_path { + tera.add_template_file(template, Some("ebuild.tera"))?; + } else { + tera.add_raw_template("ebuild.tera", include_str!("ebuild.tera"))?; + } context.insert("cargo_ebuild_ver", env!("CARGO_PKG_VERSION")); context.insert("this_year", &time::OffsetDateTime::now_utc().year()); diff --git a/src/main.rs b/src/main.rs index 94aa1af..fe8881c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,9 @@ struct Args { #[structopt(name = "PATH", long = "manifest-path", parse(from_os_str))] /// Path to Cargo.toml. manifest_path: Option, + #[structopt(name = "TEMPLATE", long = "template-path", short)] + /// Non-standard template + template_path: Option, } #[derive(StructOpt, Debug)] @@ -46,7 +49,7 @@ fn main() -> Result<()> { let ebuild_path = format!("{}-{}.ebuild", ebuild_data.name, ebuild_data.version); - write_ebuild(ebuild_data, &ebuild_path)?; + write_ebuild(ebuild_data, &ebuild_path, opt.template_path.as_ref())?; println!("Wrote: {}", ebuild_path); -- cgit v1.2.3-65-gdbad