|
| 1 | +//! Helper to generate a changelog for a new release. |
| 2 | +
|
| 3 | +use super::Result; |
| 4 | +use std::fs; |
| 5 | +use std::process::Command; |
| 6 | +use std::process::exit; |
| 7 | + |
| 8 | +const CHANGELOG_PATH: &str = "CHANGELOG.md"; |
| 9 | + |
| 10 | +pub(crate) fn changelog() -> Result<()> { |
| 11 | + let previous = get_previous()?; |
| 12 | + let current = get_current()?; |
| 13 | + if current == previous { |
| 14 | + eprintln!( |
| 15 | + "error: Current version is `{current}` which is the same as the \ |
| 16 | + previous version in the changelog. Run `cargo set-version --bump <BUMP> first." |
| 17 | + ); |
| 18 | + exit(1); |
| 19 | + } |
| 20 | + let prs = get_prs(&previous)?; |
| 21 | + update_changelog(&previous, ¤t, &prs)?; |
| 22 | + Ok(()) |
| 23 | +} |
| 24 | + |
| 25 | +fn get_previous() -> Result<String> { |
| 26 | + let contents = fs::read_to_string(CHANGELOG_PATH)?; |
| 27 | + let version = contents |
| 28 | + .lines() |
| 29 | + .filter_map(|line| line.strip_prefix("## mdBook ")) |
| 30 | + .next() |
| 31 | + .expect("at least one entry") |
| 32 | + .to_owned(); |
| 33 | + Ok(version) |
| 34 | +} |
| 35 | + |
| 36 | +fn get_current() -> Result<String> { |
| 37 | + let contents = fs::read_to_string("Cargo.toml")?; |
| 38 | + let mut lines = contents |
| 39 | + .lines() |
| 40 | + .filter_map(|line| line.strip_prefix("version = ")) |
| 41 | + .map(|version| &version[1..version.len() - 1]); |
| 42 | + let version = lines.next().expect("version should exist").to_owned(); |
| 43 | + assert_eq!(lines.next(), None); |
| 44 | + Ok(version) |
| 45 | +} |
| 46 | + |
| 47 | +fn get_prs(previous: &str) -> Result<Vec<(String, String)>> { |
| 48 | + println!("running `git fetch upstream`"); |
| 49 | + let status = Command::new("git").args(["fetch", "upstream"]).status()?; |
| 50 | + if !status.success() { |
| 51 | + eprintln!("error: git fetch failed"); |
| 52 | + exit(1); |
| 53 | + } |
| 54 | + println!("running `git log`"); |
| 55 | + const SEPARATOR: &str = "---COMMIT_SEPARATOR---"; |
| 56 | + let output = Command::new("git") |
| 57 | + .args([ |
| 58 | + "log", |
| 59 | + "--first-parent", |
| 60 | + &format!("--pretty=format:%B%n{SEPARATOR}"), |
| 61 | + "upstream/master", |
| 62 | + &format!("v{previous}...upstream/HEAD"), |
| 63 | + ]) |
| 64 | + .output()?; |
| 65 | + if !output.status.success() { |
| 66 | + eprintln!("error: git log failed"); |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + let stdout = std::str::from_utf8(&output.stdout).unwrap(); |
| 70 | + let prs = stdout |
| 71 | + .split(&format!("{SEPARATOR}\n")) |
| 72 | + .filter_map(|entry| { |
| 73 | + let mut lines = entry.lines(); |
| 74 | + let first = match lines.next().unwrap().strip_prefix("Merge pull request #") { |
| 75 | + Some(f) => f, |
| 76 | + None => { |
| 77 | + println!("warning: merge line not found in {entry}"); |
| 78 | + return None; |
| 79 | + } |
| 80 | + }; |
| 81 | + let number = first.split_whitespace().next().unwrap(); |
| 82 | + assert_eq!(lines.next(), Some("")); |
| 83 | + let title = lines.next().expect("title is set"); |
| 84 | + assert_eq!(lines.next(), Some("")); |
| 85 | + Some((number.to_string(), title.to_string())) |
| 86 | + }) |
| 87 | + .collect(); |
| 88 | + Ok(prs) |
| 89 | +} |
| 90 | + |
| 91 | +fn update_changelog(previous: &str, current: &str, prs: &[(String, String)]) -> Result<()> { |
| 92 | + let prs: String = prs |
| 93 | + .iter() |
| 94 | + .map(|(number, title)| { |
| 95 | + format!( |
| 96 | + "- {title}\n \ |
| 97 | + [#{number}](https://github.com/rust-lang/mdBook/pull/{number})\n" |
| 98 | + ) |
| 99 | + }) |
| 100 | + .collect(); |
| 101 | + let new = format!( |
| 102 | + "## mdBook {current}\n\ |
| 103 | + [v{previous}...v{current}](https://github.com/rust-lang/mdBook/compare/v{previous}...v{current})\n\ |
| 104 | + \n\ |
| 105 | + {prs}\ |
| 106 | + \n\ |
| 107 | + ### Added\n\ |
| 108 | + \n\ |
| 109 | + ### Changed\n\ |
| 110 | + \n\ |
| 111 | + ### Fixed\n\ |
| 112 | + \n" |
| 113 | + ); |
| 114 | + |
| 115 | + let mut contents = fs::read_to_string(CHANGELOG_PATH)?; |
| 116 | + let insertion_point = contents.find("## ").unwrap(); |
| 117 | + contents.insert_str(insertion_point, &new); |
| 118 | + fs::write(CHANGELOG_PATH, contents)?; |
| 119 | + Ok(()) |
| 120 | +} |
0 commit comments