commit: fe6608425e6217097aab82b89d4a72b79d94c8da Author: Luca Barbato <lu_zero <AT> gentoo <DOT> org> AuthorDate: Thu Jul 29 14:24:27 2021 +0000 Commit: Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org> CommitDate: Wed Aug 25 15:11:28 2021 +0000 URL: https://gitweb.gentoo.org/proj/cargo-ebuild.git/commit/?id=fe660842
Add an option to provide a custom tera template Signed-off-by: Luca Barbato <lu_zero <AT> gentoo.org> Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org> 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<PathBuf>) -> Result<EbuildConfig> { Ok(EbuildConfig::from_package(root_pkg, crates, licenses)) } -pub fn write_ebuild(ebuild_data: EbuildConfig, ebuild_path: impl AsRef<Path>) -> Result<()> { +pub fn write_ebuild( + ebuild_data: EbuildConfig, + ebuild_path: impl AsRef<Path>, + template_path: Option<impl AsRef<Path>>, +) -> 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<Path>) -> 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<PathBuf>, + #[structopt(name = "TEMPLATE", long = "template-path", short)] + /// Non-standard template + template_path: Option<PathBuf>, } #[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);