Skip to content

Commit

Permalink
feat(changelog): Add option to set custom template directory in `.ver…
Browse files Browse the repository at this point in the history
…sionrc`

Refs: #3
  • Loading branch information
hdevalke committed Aug 17, 2020
1 parent ae24b00 commit 01c9ea9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = [
[dependencies]
chrono = { version = "0.4.15", features = ["serde"] }
git2 = { version = "0.13.8", default-features = false, features = [] }
handlebars = "3.4.0"
handlebars = { version = "3.4.0", features = [ "dir_source" ] }
lazy_static = "1.4.0"
regex = "1.3.9"
semver = { version = "0.10.0", features = ["serde"] }
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ convco version --bump
#### TODO

- [x] automatic notes for breaking changes
- [ ] custom template folder
- [x] custom template folder
- [x] use a `.versionrc` file
- [x] limit to a range of versions
- [x] sort sections in changelog
- [x] issue references
- [ ] better documentation

[1]: https://www.conventionalcommits.org/
[2]: https://github.com/conventional-changelog/conventional-changelog
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ impl Command for ChangelogCommand {
let config = make_cl_config(&helper);
let stdout = std::io::stdout();
let stdout = stdout.lock();
let mut writer = ChangelogWriter::new(stdout)?;
let template = config.template.as_deref();
let mut writer = ChangelogWriter::new(template, stdout)?;
writer.write_header(config.header.as_str())?;

let transformer = ChangeLogTransformer::new(&config, &helper)?;
Expand Down
16 changes: 10 additions & 6 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chrono::NaiveDate;
pub(crate) use config::Config;
use handlebars::{no_escape, Handlebars};
use serde::Serialize;
use std::io;
use std::{io, path::Path};

const TEMPLATE: &str = include_str!("changelog/template.hbs");
const HEADER: &str = include_str!("changelog/header.hbs");
Expand Down Expand Up @@ -129,15 +129,19 @@ pub(crate) struct ChangelogWriter<W: io::Write> {
}

impl<W: io::Write> ChangelogWriter<W> {
pub(crate) fn new(writer: W) -> Result<Self, Error> {
pub(crate) fn new(template: Option<&Path>, writer: W) -> Result<Self, Error> {
let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(true);
handlebars.register_escape_fn(no_escape);

handlebars.register_template_string("template", TEMPLATE)?;
handlebars.register_partial("header", HEADER)?;
handlebars.register_partial("commit", COMMIT)?;
handlebars.register_partial("footer", FOOTER)?;
if let Some(path) = template {
handlebars.register_templates_directory(".hbs", dbg!(path))?;
} else {
handlebars.register_template_string("template", TEMPLATE)?;
handlebars.register_partial("header", HEADER)?;
handlebars.register_partial("commit", COMMIT)?;
handlebars.register_partial("footer", FOOTER)?;
}

Ok(Self { writer, handlebars })
}
Expand Down
7 changes: 6 additions & 1 deletion src/conventional/changelog/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub(crate) struct Type {
Expand All @@ -10,7 +11,7 @@ pub(crate) struct Type {
}

/// see: [Conventional Changelog Configuration](https://github.com/conventional-changelog/conventional-changelog-config-spec/blob/master/versions/2.1.0/README.md)
/// Additional config: `host`, `owner`, `repository`.
/// Additional config: `host`, `owner`, `repository` and `template`
/// Those values are derived from `git remote origin get-url` if not set.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -46,6 +47,8 @@ pub(crate) struct Config {
pub(crate) host: Option<String>,
pub(crate) owner: Option<String>,
pub(crate) repository: Option<String>,
/// An optional template directory. The template should be called `template.hbs`. Partials can be used.
pub(crate) template: Option<PathBuf>,
}

impl Default for Config {
Expand All @@ -63,6 +66,7 @@ impl Default for Config {
host: None,
owner: None,
repository: None,
template: None,
}
}
}
Expand Down Expand Up @@ -213,6 +217,7 @@ mod tests {
host: None,
owner: None,
repository: None,
template: None,
}
)
}
Expand Down
11 changes: 10 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use handlebars::{RenderError, TemplateError};
use handlebars::{RenderError, TemplateError, TemplateFileError};
use std::{fmt, io};
use url::ParseError;

Expand All @@ -7,6 +7,7 @@ pub(crate) enum Error {
Git(git2::Error),
Io(io::Error),
Template(TemplateError),
TemplateFile(TemplateFileError),
Render(RenderError),
Url(ParseError),
Check,
Expand All @@ -18,6 +19,7 @@ impl fmt::Display for Error {
Self::Git(ref e) => write!(f, "{}", e),
Self::Io(ref e) => write!(f, "{}", e),
Self::Template(ref e) => write!(f, "{}", e),
Self::TemplateFile(ref e) => write!(f, "{}", e),
Self::Render(ref e) => write!(f, "{}", e),
Self::Url(ref e) => write!(f, "{}", e),
Self::Check => write!(f, "check error"),
Expand All @@ -31,6 +33,7 @@ impl std::error::Error for Error {
Self::Git(ref e) => Some(e),
Self::Io(ref e) => Some(e),
Self::Template(ref e) => Some(e),
Self::TemplateFile(ref e) => Some(e),
Self::Render(ref e) => Some(e),
Self::Url(ref e) => Some(e),
Self::Check => None,
Expand All @@ -56,6 +59,12 @@ impl From<TemplateError> for Error {
}
}

impl From<TemplateFileError> for Error {
fn from(e: TemplateFileError) -> Self {
Self::TemplateFile(e)
}
}

impl From<RenderError> for Error {
fn from(e: RenderError) -> Self {
Self::Render(e)
Expand Down

0 comments on commit 01c9ea9

Please sign in to comment.