| 1 | use codewhale_commands::debug::change::{ |
| 2 | count_changelog_entries, extract_changelog_section_by_version, |
| 3 | }; |
| 4 | use codewhale_localization::{Locale, MessageId, tr}; |
| 5 | |
| 6 | pub(crate) const BUNDLED_CHANGELOG: &str = include_str!("../CHANGELOG.md"); |
| 7 | |
| 8 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 9 | pub struct WhatsNewNotice { |
| 10 | pub version: String, |
| 11 | pub updated: String, |
| 12 | pub counts: Option<String>, |
| 13 | /// The summary paragraph of every release from the one this copy last |
| 14 | /// ran (exclusive) up to this one, newest first. |
| 15 | pub summary: Option<String>, |
| 16 | pub notes: String, |
| 17 | pub url: String, |
| 18 | headline_template: String, |
| 19 | } |
| 20 | |
| 21 | impl WhatsNewNotice { |
| 22 | #[must_use] |
| 23 | pub fn for_version(locale: Locale, version: &str) -> Self { |
| 24 | Self::for_change(locale, None, version) |
| 25 | } |
| 26 | |
| 27 | /// The notice for moving from `previous` to `version`, rolling up the |
| 28 | /// summaries of any releases skipped in between. |
| 29 | #[must_use] |
| 30 | pub fn for_change(locale: Locale, previous: Option<&str>, version: &str) -> Self { |
| 31 | let summary = rolled_up_summary(BUNDLED_CHANGELOG, previous, version); |
| 32 | let section = extract_changelog_section_by_version(BUNDLED_CHANGELOG, version); |
| 33 | let counts = section |
| 34 | .as_deref() |
| 35 | .map(count_changelog_entries) |
| 36 | .filter(|counts| counts.features + counts.fixes + counts.other > 0) |
| 37 | .map(|counts| { |
| 38 | tr(locale, MessageId::UpdateWhatsNewCounts) |
| 39 | .replace( |
| 40 | "{features}", |
| 41 | &count_phrase( |
| 42 | locale, |
| 43 | counts.features, |
| 44 | MessageId::UpdateWhatsNewFeatureSingular, |
| 45 | MessageId::UpdateWhatsNewFeaturesPlural, |
| 46 | ), |
| 47 | ) |
| 48 | .replace( |
| 49 | "{fixes}", |
| 50 | &count_phrase( |
| 51 | locale, |
| 52 | counts.fixes, |
| 53 | MessageId::UpdateWhatsNewFixSingular, |
| 54 | MessageId::UpdateWhatsNewFixesPlural, |
| 55 | ), |
| 56 | ) |
| 57 | .replace( |
| 58 | "{other}", |
| 59 | &count_phrase( |
| 60 | locale, |
| 61 | counts.other, |
| 62 | MessageId::UpdateWhatsNewOtherSingular, |
| 63 | MessageId::UpdateWhatsNewOtherPlural, |
| 64 | ), |
| 65 | ) |
| 66 | }); |
| 67 | let url = release_notes_url(locale, section.is_some().then_some(version)); |
| 68 | Self { |
| 69 | version: version.to_string(), |
| 70 | updated: tr(locale, MessageId::UpdateWhatsNewUpdated).replace("{version}", version), |
| 71 | counts, |
| 72 | summary, |
| 73 | notes: tr(locale, MessageId::UpdateWhatsNewNotes).replace("{url}", &url), |
| 74 | url, |
| 75 | headline_template: tr(locale, MessageId::UpdateWhatsNewHeadline).into_owned(), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[must_use] |
| 80 | pub fn headline(&self) -> String { |
| 81 | if let Some(summary) = &self.summary { |
| 82 | return format!("{} {summary}", self.updated); |
| 83 | } |
| 84 | match &self.counts { |
| 85 | Some(counts) => self |
| 86 | .headline_template |
| 87 | .replace("{updated}", &self.updated) |
| 88 | .replace("{counts}", counts), |
| 89 | None => self.updated.clone(), |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[must_use] |
| 94 | pub fn notes_with_command(&self) -> String { |
| 95 | format!("{} · /change {}", self.notes, self.version) |
| 96 | } |
| 97 | |
| 98 | #[must_use] |
| 99 | pub fn plain_text(&self) -> String { |
| 100 | format!("{}\n{}", self.headline(), self.notes) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | fn version_key(version: &str) -> Option<(u64, u64, u64)> { |
| 105 | let core = version |
| 106 | .trim() |
| 107 | .trim_start_matches('v') |
| 108 | .split(['-', '+']) |
| 109 | .next()?; |
| 110 | let mut parts = core.split('.').map(|part| part.parse::<u64>().ok()); |
| 111 | Some(( |
| 112 | parts.next()??, |
| 113 | parts.next()??, |
| 114 | parts.next().flatten().unwrap_or(0), |
| 115 | )) |
| 116 | } |
| 117 | |
| 118 | /// The summary paragraph (the prose before the first `###`) of each |
| 119 | /// released section after `previous` up to `current`, newest first. |
| 120 | fn rolled_up_summary(changelog: &str, previous: Option<&str>, current: &str) -> Option<String> { |
| 121 | let current_key = version_key(current)?; |
| 122 | let floor = previous.and_then(version_key); |
| 123 | let summaries: Vec<String> = changelog |
| 124 | .lines() |
| 125 | .filter_map(|line| line.strip_prefix("## [")?.split(']').next()) |
| 126 | .filter(|version| { |
| 127 | version_key(version) |
| 128 | .is_some_and(|key| key <= current_key && floor.is_none_or(|floor| key > floor)) |
| 129 | }) |
| 130 | .filter(|version| floor.is_some() || version_key(version) == Some(current_key)) |
| 131 | .filter_map(|version| { |
| 132 | let section = extract_changelog_section_by_version(changelog, version)?; |
| 133 | let paragraph = section |
| 134 | .lines() |
| 135 | .skip_while(|line| line.starts_with("## ") || line.trim().is_empty()) |
| 136 | .take_while(|line| !line.starts_with("### ") && !line.trim().is_empty()) |
| 137 | .collect::<Vec<_>>() |
| 138 | .join(" "); |
| 139 | (!paragraph.trim().is_empty()).then(|| paragraph.trim().to_string()) |
| 140 | }) |
| 141 | .collect(); |
| 142 | (!summaries.is_empty()).then(|| summaries.join("\n")) |
| 143 | } |
| 144 | |
| 145 | fn count_phrase(locale: Locale, count: usize, one: MessageId, many: MessageId) -> String { |
| 146 | if count == 1 { |
| 147 | tr(locale, one).into_owned() |
| 148 | } else { |
| 149 | tr(locale, many).replace("{count}", &count.to_string()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #[must_use] |
| 154 | pub fn release_notes_url(locale: Locale, version: Option<&str>) -> String { |
| 155 | let site_locale = |
| 156 | codewhale_command_contract::navigation::public_site_locale_segment(locale.tag()); |
| 157 | codewhale_release::autoupdate::changelog_url_for_locale(site_locale, version) |
| 158 | } |
| 159 | |
| 160 | pub(crate) fn take_after_update(locale: Locale, show: bool) -> Option<WhatsNewNotice> { |
| 161 | let home = codewhale_config::codewhale_home().ok()?; |
| 162 | let outcome = codewhale_release::record_launch(&home, env!("CARGO_PKG_VERSION")); |
| 163 | if let Some(record_error) = outcome.record_error { |
| 164 | tracing::debug!(error = %record_error, "could not persist the last-launch record"); |
| 165 | } |
| 166 | let change = outcome.change.filter(|_| show)?; |
| 167 | Some(WhatsNewNotice::for_change( |
| 168 | locale, |
| 169 | Some(&change.previous), |
| 170 | &change.current, |
| 171 | )) |
| 172 | } |
| 173 | |
| 174 | #[must_use] |
| 175 | pub fn plain_cli_notice(update_table: Option<&toml::Value>) -> Option<String> { |
| 176 | let show = update_table |
| 177 | .cloned() |
| 178 | .and_then(|table| { |
| 179 | table |
| 180 | .try_into::<codewhale_runtime::config::UpdateConfig>() |
| 181 | .ok() |
| 182 | }) |
| 183 | .unwrap_or_default() |
| 184 | .show_whats_new; |
| 185 | let settings = codewhale_runtime::settings::Settings::load_read_only().unwrap_or_default(); |
| 186 | let locale = codewhale_localization::resolve_locale(&settings.locale); |
| 187 | take_after_update(locale, show).map(|notice| notice.plain_text()) |
| 188 | } |
| 189 | |
| 190 | #[cfg(test)] |
| 191 | mod tests { |
| 192 | use super::rolled_up_summary; |
| 193 | |
| 194 | const CHANGELOG: &str = "## [Unreleased]\n\n## [0.10.5] - 2026-12-01\n\nFive is here.\n\n### Fixed\n\n- A.\n\n## [0.10.4] - 2026-11-20\n\nFour is here.\n\n### Fixed\n\n- B.\n\n## [0.10.3] - 2026-11-01\n\nThree is here.\n"; |
| 195 | |
| 196 | #[test] |
| 197 | fn whats_new_rolls_up_skipped_release_summaries() { |
| 198 | assert_eq!( |
| 199 | rolled_up_summary(CHANGELOG, Some("0.10.3"), "0.10.5").as_deref(), |
| 200 | Some("Five is here.\nFour is here.") |
| 201 | ); |
| 202 | assert_eq!( |
| 203 | rolled_up_summary(CHANGELOG, None, "0.10.4").as_deref(), |
| 204 | Some("Four is here.") |
| 205 | ); |
| 206 | } |
| 207 | } |
| 208 |