commit:     8357787285a25600de45743c9ecfd5e16ff576ef
Author:     Luca Barbato <lu_zero <AT> gentoo <DOT> org>
AuthorDate: Thu Jul 29 14:16:18 2021 +0000
Commit:     Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
CommitDate: Wed Aug 25 15:11:24 2021 +0000
URL:        https://gitweb.gentoo.org/proj/cargo-ebuild.git/commit/?id=83577872

Convert the default template to tera

Signed-off-by: Luca Barbato <lu_zero <AT> gentoo.org>
Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org>

 Cargo.toml                           |  5 +++++
 src/{ebuild.template => ebuild.tera} | 19 +++++++++++--------
 src/lib.rs                           | 35 +++++++++++++++++------------------
 src/metadata.rs                      |  2 ++
 4 files changed, 35 insertions(+), 26 deletions(-)

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.tera
similarity index 54%
rename from src/ebuild.template
rename to 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"

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<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,

Reply via email to