From 8357787285a25600de45743c9ecfd5e16ff576ef Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 29 Jul 2021 16:16:18 +0200 Subject: Convert the default template to tera Signed-off-by: Luca Barbato Signed-off-by: Georgy Yakovlev --- Cargo.toml | 5 +++++ src/ebuild.template | 24 ------------------------ src/ebuild.tera | 27 +++++++++++++++++++++++++++ src/lib.rs | 35 +++++++++++++++++------------------ src/metadata.rs | 2 ++ 5 files changed, 51 insertions(+), 42 deletions(-) delete mode 100644 src/ebuild.template create mode 100644 src/ebuild.tera diff --git a/Cargo.toml b/Cargo.toml index b934115..a15752f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,13 @@ cargo-lock = "^7.0" cargo_metadata = "^0.14" itertools = "^0.10" structopt = "^0.3" +serde = { version = "1.0", features = ["derive"] } time = "^0.2" [dependencies.phf] version = "0.9" features = ["macros"] + +[dependencies.tera] +version = "1" +default-features = false diff --git a/src/ebuild.template b/src/ebuild.template deleted file mode 100644 index b979f2f..0000000 --- a/src/ebuild.template +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2017-{this_year} Gentoo Authors -# Distributed under the terms of the GNU General Public License v2 - -# Auto-Generated by cargo-ebuild {cargo_ebuild_ver} - -EAPI=7 - -CRATES=" -{crates}" - -inherit cargo - -DESCRIPTION="{description}" -# Double check the homepage as the cargo_metadata crate -# does not provide this value so instead repository is used -HOMEPAGE="{homepage}" -SRC_URI="$(cargo_crate_uris ${{CRATES}})" - -# License set may be more restrictive as OR is not respected -# use cargo-license for a more accurate license picture -LICENSE="{license}" -SLOT="0" -KEYWORDS="~amd64" -RESTRICT="mirror" diff --git a/src/ebuild.tera b/src/ebuild.tera new file mode 100644 index 0000000..b83675d --- /dev/null +++ b/src/ebuild.tera @@ -0,0 +1,27 @@ +# Copyright 2017-{{ this_year }} Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# Auto-Generated by cargo-ebuild {{ cargo_ebuild_ver }} + +EAPI=7 + +CRATES=" +{% for crate in crates -%} +{{ crate }} +{%- endfor -%}" + +inherit cargo + +DESCRIPTION="{{ description | trim }}" +# Double check the homepage as the cargo_metadata crate +# does not provide this value so instead repository is used +HOMEPAGE="{{ homepage }}" +{% raw -%} +SRC_URI="$(cargo_crate_uris ${CRATES})" +{%- endraw %} +# License set may be more restrictive as OR is not respected +# use cargo-license for a more accurate license picture +LICENSE="{{ license }}" +SLOT="0" +KEYWORDS="~amd64" +RESTRICT="mirror" diff --git a/src/lib.rs b/src/lib.rs index 9a4aa49..2ecb1f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,11 +13,10 @@ mod metadata; use anyhow::{format_err, Context, Result}; use cargo_lock::Lockfile; -use cargo_metadata::MetadataCommand; use cargo_metadata::CargoOpt; +use cargo_metadata::MetadataCommand; use std::collections::BTreeSet; use std::fs::OpenOptions; -use std::io::Write; use std::path::{Path, PathBuf}; use std::process::Command; @@ -86,7 +85,10 @@ pub fn gen_ebuild_data(manifest_path: Option) -> Result { licenses.insert(norm.to_string()); } else { // Add the unknown license name to be corrected manually - println!("WARNING: unknown license \"{}\", please correct manually", &lic); + println!( + "WARNING: unknown license \"{}\", please correct manually", + &lic + ); licenses.insert(lic.to_string()); } } @@ -133,19 +135,16 @@ pub fn write_ebuild(ebuild_data: EbuildConfig, ebuild_path: impl AsRef) -> ebuild_path.as_ref().display() ))?; - // write the contents out - write!( - file, - include_str!("ebuild.template"), - description = ebuild_data.description.trim(), - homepage = ebuild_data.homepage.trim(), - license = ebuild_data.license.trim(), - crates = ebuild_data.crates.join(""), - cargo_ebuild_ver = env!("CARGO_PKG_VERSION"), - this_year = time::OffsetDateTime::now_utc().year(), - ) - .context(format!( - "Failed to write to {}", - ebuild_path.as_ref().display() - )) + 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"))?; + + context.insert("cargo_ebuild_ver", env!("CARGO_PKG_VERSION")); + context.insert("this_year", &time::OffsetDateTime::now_utc().year()); + + tera.render_to("ebuild.tera", &context, &mut file) + .context(format!( + "Failed to write to {}", + ebuild_path.as_ref().display() + )) } diff --git a/src/metadata.rs b/src/metadata.rs index d221825..2d081d8 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -10,8 +10,10 @@ use cargo_metadata::Package; use itertools::Itertools; +use serde::Serialize; use std::collections::BTreeSet; +#[derive(Serialize)] pub struct EbuildConfig { pub name: String, pub version: String, -- cgit v1.2.3-65-gdbad