Add changelog to book

Add the changelog to the book. It's no longer comitted, so it's good to
have the current version available somewhere.

type: documentation
This commit is contained in:
Casey Rodarmor 2020-04-30 00:05:54 -07:00
parent 60a72cf057
commit bf29d74b3e
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
7 changed files with 81 additions and 43 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
/book/book /book/book
/book/src/SUMMARY.md /book/src/SUMMARY.md
/book/src/bittorrent.md /book/src/bittorrent.md
/book/src/changelog.md
/book/src/commands.md /book/src/commands.md
/book/src/commands/ /book/src/commands/
/book/src/faq.md /book/src/faq.md

View File

@ -99,18 +99,22 @@ impl Changelog {
Self { releases } Self { releases }
} }
}
impl Display for Changelog { #[throws]
#[throws(fmt::Error)] pub(crate) fn render(&self, book: bool) -> String {
fn fmt(&self, f: &mut Formatter) { let mut lines: Vec<String> = Vec::new();
writeln!(f, "Changelog")?;
writeln!(f, "=========")?; lines.push("Changelog".into());
lines.push("=========".into());
for release in &self.releases { for release in &self.releases {
writeln!(f)?; lines.push("".into());
writeln!(f)?; lines.push("".into());
write!(f, "{}", release)?; release.render(&mut lines, book)?;
} }
let mut text = lines.join("\n");
text.push('\n');
text
} }
} }

View File

@ -43,48 +43,47 @@ impl Entry {
} }
} }
fn permanent_shorthash(&self) -> String { fn shorthash(&self) -> String {
if self.head {
"x".repeat(12)
} else {
self.hash[..12].into() self.hash[..12].into()
} }
}
}
impl Display for Entry { #[throws]
#[throws(fmt::Error)] pub(crate) fn render(&self, lines: &mut Vec<String>, book: bool) {
fn fmt(&self, f: &mut Formatter) { let mut line = "- ".to_string();
let url = self.url(); let url = self.url();
let shorthash = self.permanent_shorthash(); line.push_str(&format!(
write!(
f,
"{} [`{}`]({}) {}", "{} [`{}`]({}) {}",
self.metadata.kind.emoji(), if book {
shorthash, self.metadata.kind.emoji_character()
} else {
self.metadata.kind.emoji_name()
},
self.shorthash(),
url, url,
self.summary self.summary
)?; ));
if let Some(pr) = &self.metadata.pr { if let Some(pr) = &self.metadata.pr {
let n = pr.path_segments().unwrap().last().unwrap(); let n = pr.path_segments().unwrap().last().unwrap();
write!(f, " ([#{}]({}))", n, pr)?; line.push_str(&format!(" ([#{}]({}))", n, pr));
} }
if !self.metadata.fixes.is_empty() { if !self.metadata.fixes.is_empty() {
write!(f, " - Fixes ")?; line.push_str(" - Fixes ");
for (i, issue) in self.metadata.fixes.iter().enumerate() { for (i, issue) in self.metadata.fixes.iter().enumerate() {
if i > 0 { if i > 0 {
write!(f, ", ")?; line.push_str(", ");
} }
let n = issue.path_segments().unwrap().last().unwrap(); let n = issue.path_segments().unwrap().last().unwrap();
write!(f, "[#{}]({})", n, issue)?; line.push_str(&format!("[#{}]({})", n, issue));
} }
} }
write!(f, " - _{}_", self.author)?; line.push_str(&format!(" - _{}_", self.author));
lines.push(line);
} }
} }

View File

@ -30,7 +30,23 @@ pub(crate) enum Kind {
} }
impl Kind { impl Kind {
pub(crate) fn emoji(self) -> &'static str { pub(crate) fn emoji_character(self) -> &'static str {
match self {
Self::Added => "",
Self::Breaking => "💥",
Self::Changed => "⚡️",
Self::Dependencies => "⬆️",
Self::Development => "🔧",
Self::Distribution => "📦",
Self::Documentation => "📚",
Self::Fixed => "🐛",
Self::Reform => "🎨",
Self::Release => "🔖",
Self::Testing => "",
}
}
pub(crate) fn emoji_name(self) -> &'static str {
match self { match self {
Self::Added => ":sparkles:", Self::Added => ":sparkles:",
Self::Breaking => ":boom:", Self::Breaking => ":boom:",

View File

@ -93,7 +93,7 @@ impl Opt {
let path = project.root.join("CHANGELOG.md"); let path = project.root.join("CHANGELOG.md");
fs::write(&path, changelog.to_string()).context(error::Filesystem { path })?; fs::write(&path, changelog.render(false)?).context(error::Filesystem { path })?;
} }
#[throws] #[throws]
@ -124,6 +124,7 @@ impl Opt {
"/README.md", "/README.md",
"/book/src/SUMMARY.md", "/book/src/SUMMARY.md",
"/book/src/bittorrent.md", "/book/src/bittorrent.md",
"/book/src/changelog.md",
"/book/src/commands.md", "/book/src/commands.md",
"/book/src/commands/*", "/book/src/commands/*",
"/book/src/faq.md", "/book/src/faq.md",
@ -182,9 +183,11 @@ impl Opt {
gen(HEAD)?; gen(HEAD)?;
let head = project.repo.head()?.peel_to_commit()?; let head = project.repo.head()?;
let parent = head.parent(0)?; let head_commit = head.peel_to_commit()?;
let parent = head_commit.parent(0)?;
let parent_hash = parent.id().to_string(); let parent_hash = parent.id().to_string();
@ -192,12 +195,20 @@ impl Opt {
gen(&parent_hash)?; gen(&parent_hash)?;
cmd!("diff", "-r", parent_hash, HEAD) cmd!("colordiff", "-ur", parent_hash, HEAD)
.current_dir(tmp.path()) .current_dir(tmp.path())
.status_into_result() .status_into_result()
.ok(); .ok();
cmd!("git", "checkout", &head.id().to_string()).status_into_result()?; cmd!(
"git",
"checkout",
head
.shorthand()
.map(str::to_owned)
.unwrap_or_else(|| head_commit.id().to_string())
)
.status_into_result()?;
} }
#[throws] #[throws]
@ -245,6 +256,11 @@ impl Opt {
Summary::new(project).render_to(project.root.join("book/src/SUMMARY.md"))?; Summary::new(project).render_to(project.root.join("book/src/SUMMARY.md"))?;
Introduction::new(&project.config).render_to(project.root.join("book/src/introduction.md"))?; Introduction::new(&project.config).render_to(project.root.join("book/src/introduction.md"))?;
let changelog = Changelog::new(&project)?;
let dst = project.root.join("book/src/changelog.md");
fs::write(&dst, changelog.render(true)?).context(error::Filesystem { path: dst })?;
} }
#[throws] #[throws]

View File

@ -6,9 +6,9 @@ pub(crate) struct Release {
pub(crate) entries: Vec<Entry>, pub(crate) entries: Vec<Entry>,
} }
impl Display for Release { impl Release {
#[throws(fmt::Error)] #[throws]
fn fmt(&self, f: &mut Formatter) { pub(crate) fn render(&self, lines: &mut Vec<String>, book: bool) {
let time = self.time.format("%Y-%m-%d"); let time = self.time.format("%Y-%m-%d");
let header = match &self.version { let header = match &self.version {
@ -19,11 +19,11 @@ impl Display for Release {
None => format!("UNRELEASED - {}", time), None => format!("UNRELEASED - {}", time),
}; };
writeln!(f, "{}", header)?; lines.push(header.clone());
writeln!(f, "{}", "-".repeat(header.len()))?; lines.push("-".repeat(header.len()));
for entry in &self.entries { for entry in &self.entries {
writeln!(f, "- {}", entry)?; entry.render(lines, book)?;
} }
} }
} }

View File

@ -5,6 +5,8 @@ Summary
- [FAQ](./faq.md) - [FAQ](./faq.md)
- [Changelog](./changelog.md)
{{commands}} {{commands}}
- [Bittorrent](./bittorrent.md) - [Bittorrent](./bittorrent.md)