diff options
author | Luca Barbato <lu_zero@gentoo.org> | 2021-07-29 16:16:18 +0200 |
---|---|---|
committer | Georgy Yakovlev <gyakovlev@gentoo.org> | 2021-08-25 08:11:24 -0700 |
commit | 8357787285a25600de45743c9ecfd5e16ff576ef (patch) | |
tree | 5a492db44cd3ebecc42acd2e98797a60dba17b13 | |
parent | Update dependencies (diff) | |
download | cargo-ebuild-8357787285a25600de45743c9ecfd5e16ff576ef.tar.gz cargo-ebuild-8357787285a25600de45743c9ecfd5e16ff576ef.tar.bz2 cargo-ebuild-8357787285a25600de45743c9ecfd5e16ff576ef.zip |
Convert the default template to tera
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
Signed-off-by: Georgy Yakovlev <gyakovlev@gentoo.org>
-rw-r--r-- | Cargo.toml | 5 | ||||
-rw-r--r-- | src/ebuild.tera (renamed from src/ebuild.template) | 19 | ||||
-rw-r--r-- | src/lib.rs | 35 | ||||
-rw-r--r-- | src/metadata.rs | 2 |
4 files changed, 35 insertions, 26 deletions
@@ -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.tera index b979f2f..b83675d 100644 --- a/src/ebuild.template +++ b/src/ebuild.tera @@ -1,24 +1,27 @@ -# Copyright 2017-{this_year} Gentoo Authors +# 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} +# Auto-Generated by cargo-ebuild {{ cargo_ebuild_ver }} EAPI=7 CRATES=" -{crates}" +{% for crate in crates -%} +{{ crate }} +{%- endfor -%}" inherit cargo -DESCRIPTION="{description}" +DESCRIPTION="{{ description | trim }}" # 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}})" - +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}" +LICENSE="{{ license }}" SLOT="0" KEYWORDS="~amd64" RESTRICT="mirror" @@ -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<PathBuf>) -> Result<EbuildConfig> { 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<Path>) -> 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, |