Skip to content

Commit

Permalink
refactor(template)!: add name parameter to the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 15, 2024
1 parent 1d8a2fe commit e577113
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
10 changes: 7 additions & 3 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ impl<'a> Changelog<'a> {
Ok(Self {
releases,
header_template: match &config.changelog.header {
Some(header) => Some(Template::new(header.to_string(), trim)?),
Some(header) => {
Some(Template::new("header", header.to_string(), trim)?)
}
None => None,
},
body_template: get_body_template(config, trim)?,
footer_template: match &config.changelog.footer {
Some(footer) => Some(Template::new(footer.to_string(), trim)?),
Some(footer) => {
Some(Template::new("footer", footer.to_string(), trim)?)
}
None => None,
},
config,
Expand Down Expand Up @@ -612,7 +616,7 @@ fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
.as_deref()
.unwrap_or_default()
.to_string();
let template = Template::new(template_str, trim)?;
let template = Template::new("body", template_str, trim)?;
let deprecated_vars = [
"commit.github",
"commit.gitea",
Expand Down
26 changes: 15 additions & 11 deletions git-cliff-core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use tera::{
/// Wrapper for [`Tera`].
#[derive(Debug)]
pub struct Template {
/// Template name.
name: String,
/// Internal Tera instance.
tera: Tera,
/// Template variables.
#[cfg_attr(not(feature = "github"), allow(dead_code))]
Expand All @@ -30,16 +33,16 @@ pub struct Template {

impl Template {
/// Constructs a new instance.
pub fn new(mut template: String, trim: bool) -> Result<Self> {
pub fn new(name: &str, mut content: String, trim: bool) -> Result<Self> {
if trim {
template = template
content = content
.lines()
.map(|v| v.trim())
.collect::<Vec<&str>>()
.join("\n");
}
let mut tera = Tera::default();
if let Err(e) = tera.add_raw_template("template", &template) {
if let Err(e) = tera.add_raw_template(&name, &content) {
return if let Some(error_source) = e.source() {
Err(Error::TemplateParseError(error_source.to_string()))
} else {
Expand All @@ -48,7 +51,8 @@ impl Template {
}
tera.register_filter("upper_first", Self::upper_first_filter);
Ok(Self {
variables: Self::get_template_variables(&tera)?,
name: name.to_string(),
variables: Self::get_template_variables(name, &tera)?,
tera,
})
}
Expand Down Expand Up @@ -129,13 +133,13 @@ impl Template {
}

/// Returns the variable names that are used in the template.
fn get_template_variables(tera: &Tera) -> Result<Vec<String>> {
fn get_template_variables(name: &str, tera: &Tera) -> Result<Vec<String>> {
let mut variables = HashSet::new();
let ast = &tera.get_template("template")?.ast;
let ast = &tera.get_template(name)?.ast;
for node in ast {
Self::find_identifiers(node, &mut variables);
}
trace!("Template variables: {variables:?}");
trace!("Template variables for {name}: {variables:?}");
Ok(variables.into_iter().collect())
}

Expand All @@ -159,7 +163,7 @@ impl Template {
context.insert(key.clone(), &value);
}
}
match self.tera.render("template", &context) {
match self.tera.render(&self.name, &context) {
Ok(mut v) => {
for postprocessor in postprocessors {
postprocessor.replace(&mut v, vec![])?;
Expand Down Expand Up @@ -242,7 +246,7 @@ mod test {
### {{ commit.group }}
- {{ commit.message | upper_first }}
{% endfor %}"#;
let mut template = Template::new(template.to_string(), false)?;
let mut template = Template::new("test", template.to_string(), false)?;
let release = get_fake_release_data();
assert_eq!(
"\n\t\t## 1.0 - 2023\n\t\t\n\t\t### feat\n\t\t- Add xyz\n\t\t\n\t\t### \
Expand Down Expand Up @@ -281,7 +285,7 @@ mod test {
let template = r#"
## {{ version }}
"#;
let template = Template::new(template.to_string(), true)?;
let template = Template::new("test", template.to_string(), true)?;
let release = get_fake_release_data();
assert_eq!(
"\n## 1.0\n",
Expand All @@ -300,7 +304,7 @@ mod test {
let template =
"{% set hello_variable = 'hello' %}{{ hello_variable | upper_first }}";
let release = get_fake_release_data();
let template = Template::new(template.to_string(), true)?;
let template = Template::new("test", template.to_string(), true)?;
let r = template.render(
&release,
Option::<HashMap<&str, String>>::None.as_ref(),
Expand Down
2 changes: 1 addition & 1 deletion git-cliff-core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn generate_changelog() -> Result<()> {
];

let out = &mut String::new();
let template = Template::new(changelog_config.body.unwrap(), false)?;
let template = Template::new("test", changelog_config.body.unwrap(), false)?;

writeln!(out, "{}", changelog_config.header.unwrap()).unwrap();
for release in releases {
Expand Down

0 comments on commit e577113

Please sign in to comment.