From 5f66580a55c55958f7336423b36203c76ba7d76d Mon Sep 17 00:00:00 2001 From: Dylan Anthony Date: Sun, 14 Jul 2024 11:29:08 -0600 Subject: [PATCH] Use actions instead of strings for tags to apply --- crates/knope-versioning/src/action.rs | 2 +- crates/knope/src/step/releases/changelog.rs | 28 +++++++++----------- crates/knope/src/step/releases/mod.rs | 11 +++++--- crates/knope/src/step/releases/package.rs | 14 +++++----- crates/knope/src/step/releases/semver/mod.rs | 10 +++---- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/crates/knope-versioning/src/action.rs b/crates/knope-versioning/src/action.rs index 39b39bab..33948157 100644 --- a/crates/knope-versioning/src/action.rs +++ b/crates/knope-versioning/src/action.rs @@ -1,7 +1,7 @@ use relative_path::RelativePathBuf; /// Actions to take to finish updating a package -#[derive(Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum Action { WriteToFile { path: RelativePathBuf, diff --git a/crates/knope/src/step/releases/changelog.rs b/crates/knope/src/step/releases/changelog.rs index 42873f2f..cacc0177 100644 --- a/crates/knope/src/step/releases/changelog.rs +++ b/crates/knope/src/step/releases/changelog.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, fmt::Display, path::PathBuf, str::FromStr}; use itertools::Itertools; -use knope_versioning::{changelog::Sections, changes::Change, GoVersioning, Version}; +use knope_versioning::{changelog::Sections, changes::Change, Action, GoVersioning, Version}; use miette::Diagnostic; use thiserror::Error; use time::{macros::format_description, Date, OffsetDateTime}; @@ -13,7 +13,7 @@ use crate::{dry_run::DryRun, fs}; pub(crate) struct Changelog { /// The path to the CHANGELOG file pub(crate) path: PathBuf, - /// The content that has been written to `path` + /// The content that's been written to `path` pub(crate) content: String, pub(crate) section_header_level: HeaderLevel, } @@ -96,21 +96,19 @@ impl Changelog { release_sections, &format!("{section_header_level}#"), )); - let additional_tags = package + let actions = package .set_version(&version, go_versioning) .unwrap_or_default() .into_iter() - .filter_map(|action| match action { - knope_versioning::Action::AddTag { tag } => Some(tag), - knope_versioning::Action::WriteToFile { .. } => None, - }) + // If the changelog was already written for this release, we don't need to write _any_ files + .filter(|action| matches!(action, Action::AddTag { .. })) .collect(); Ok(Some(Release { version, date, sections, header_level, - additional_tags, + actions, })) } @@ -165,10 +163,8 @@ pub(crate) struct Release { /// /// Content within is written expecting that the release title will be written at this level header_level: HeaderLevel, - /// Any tags that should be added for the sake of the versioned files (specifically `go.mod`s) - /// This doesn't include the package-level tags, since those will get added by GitHub/Gitea - /// sometimes. - pub(crate) additional_tags: Vec, + /// Actions that should be taken to complete this release + pub(crate) actions: Vec, } impl Release { pub(crate) fn new( @@ -176,7 +172,7 @@ impl Release { changes: &[Change], changelog_sections: &Sections, header_level: HeaderLevel, - additional_tags: Vec, + actions: Vec, ) -> Self { let sections = changelog_sections .iter() @@ -210,17 +206,17 @@ impl Release { date, sections, header_level, - additional_tags, + actions, } } - pub(crate) fn empty(version: Version, additional_tags: Vec) -> Self { + pub(crate) fn empty(version: Version, actions: Vec) -> Self { Self { version, date: Some(OffsetDateTime::now_utc().date()), sections: None, header_level: HeaderLevel::H2, - additional_tags, + actions, } } diff --git a/crates/knope/src/step/releases/mod.rs b/crates/knope/src/step/releases/mod.rs index c7638b6c..acd1ff09 100644 --- a/crates/knope/src/step/releases/mod.rs +++ b/crates/knope/src/step/releases/mod.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use itertools::Itertools; -use knope_versioning::{PreVersion, StableVersion, Version}; +use knope_versioning::{Action, PreVersion, StableVersion, Version}; use miette::Diagnostic; pub(crate) use non_empty_map::PrereleaseMap; @@ -333,9 +333,14 @@ pub(crate) fn release(run_type: RunType) -> Result { package_to_release .release - .additional_tags + .actions .iter() - .filter(|additional_tag| **additional_tag != tag) + .filter_map(|action| match action { + Action::AddTag { + tag: additional_tag, + } if *additional_tag != tag => Some(additional_tag), + _ => None, + }) .try_for_each(|additional_tag| create_tag(&mut dry_run_stdout, additional_tag))?; } diff --git a/crates/knope/src/step/releases/package.rs b/crates/knope/src/step/releases/package.rs index 86212b75..6cfba63f 100644 --- a/crates/knope/src/step/releases/package.rs +++ b/crates/knope/src/step/releases/package.rs @@ -12,7 +12,7 @@ use itertools::Itertools; use knope_config::changelog_section::convert_to_versioning; use knope_versioning::{ changes::{Change, ChangeSource, CHANGESET_DIR, DEFAULT_PACKAGE_NAME}, - GoVersioning, Label, PackageNewError, Version, VersionedFile, VersionedFileError, + Action, GoVersioning, Label, PackageNewError, Version, VersionedFile, VersionedFileError, }; use miette::Diagnostic; use serde::{Deserialize, Serialize}; @@ -44,7 +44,7 @@ pub(crate) struct Package { pub(crate) changelog: Option, pub(crate) name: Option, pub(crate) pending_changes: Vec, - pub(crate) pending_tags: Vec, + pub(crate) pending_actions: Vec, pub(crate) prepared_release: Option, /// Version manually set by the caller to use instead of the one determined by semantic rule pub(crate) override_version: Option, @@ -113,7 +113,7 @@ impl Package { GoVersioning::default() }, pending_changes: Vec::new(), - pending_tags: Vec::new(), + pending_actions: Vec::new(), prepared_release: None, override_version: None, }) @@ -229,8 +229,8 @@ impl Package { version: Version, dry_run: DryRun, ) -> Result { - let mut additional_tags = Vec::new(); - swap(&mut self.pending_tags, &mut additional_tags); + let mut actions = Vec::new(); + swap(&mut self.pending_actions, &mut actions); let release = Release::new( version, &self.pending_changes, @@ -238,7 +238,7 @@ impl Package { self.changelog .as_ref() .map_or(HeaderLevel::H2, |it| it.section_header_level), - additional_tags, + actions, ); if let Some(changelog) = self.changelog.as_mut() { @@ -272,7 +272,7 @@ impl Package { changelog: None, name: None, pending_changes: vec![], - pending_tags: vec![], + pending_actions: vec![], prepared_release: None, override_version: None, assets: None, diff --git a/crates/knope/src/step/releases/semver/mod.rs b/crates/knope/src/step/releases/semver/mod.rs index d40f28c3..2c232009 100644 --- a/crates/knope/src/step/releases/semver/mod.rs +++ b/crates/knope/src/step/releases/semver/mod.rs @@ -123,9 +123,9 @@ pub(crate) fn bump_version_and_update_state( } }; let mut package = package.write_version(&version, &mut dry_run_stdout)?; - let additional_tags = package.pending_tags; - package.pending_tags = Vec::new(); - package.prepared_release = Some(Release::empty(version.version, additional_tags)); + package.prepared_release = + Some(Release::empty(version.version, package.pending_actions)); + package.pending_actions = Vec::new(); Ok(package) }) .collect::, Error>>()?; @@ -162,7 +162,7 @@ impl Package { /// Consumes a [`Package`], writing it back to the file it came from. Returns the new version /// that was written. Adds all modified package files to Git. /// - /// If `dry_run` is `true`, the version will not be written to any files. + /// If `dry_run` is `true`, the version won't be written to any files. pub(crate) fn write_version( mut self, version: &VersionFromSource, @@ -185,7 +185,7 @@ impl Package { Action::WriteToFile { path, content } => { fs::write(dry_run, &version_str, &path.to_path(""), content)?; } - Action::AddTag { tag } => self.pending_tags.push(tag), + Action::AddTag { .. } => self.pending_actions.push(action), } } Ok(self)