Skip to content

Commit

Permalink
Use actions instead of strings for tags to apply
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanty committed Jul 14, 2024
1 parent bc12017 commit 5f66580
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion crates/knope-versioning/src/action.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
28 changes: 12 additions & 16 deletions crates/knope/src/step/releases/changelog.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}))
}

Expand Down Expand Up @@ -165,18 +163,16 @@ 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<String>,
/// Actions that should be taken to complete this release
pub(crate) actions: Vec<Action>,
}
impl Release {
pub(crate) fn new(
version: Version,
changes: &[Change],
changelog_sections: &Sections,
header_level: HeaderLevel,
additional_tags: Vec<String>,
actions: Vec<Action>,
) -> Self {
let sections = changelog_sections
.iter()
Expand Down Expand Up @@ -210,17 +206,17 @@ impl Release {
date,
sections,
header_level,
additional_tags,
actions,
}
}

pub(crate) fn empty(version: Version, additional_tags: Vec<String>) -> Self {
pub(crate) fn empty(version: Version, actions: Vec<Action>) -> Self {
Self {
version,
date: Some(OffsetDateTime::now_utc().date()),
sections: None,
header_level: HeaderLevel::H2,
additional_tags,
actions,
}
}

Expand Down
11 changes: 8 additions & 3 deletions crates/knope/src/step/releases/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -333,9 +333,14 @@ pub(crate) fn release(run_type: RunType) -> Result<RunType, Error> {

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))?;
}

Expand Down
14 changes: 7 additions & 7 deletions crates/knope/src/step/releases/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(crate) struct Package {
pub(crate) changelog: Option<Changelog>,
pub(crate) name: Option<PackageName>,
pub(crate) pending_changes: Vec<Change>,
pub(crate) pending_tags: Vec<String>,
pub(crate) pending_actions: Vec<Action>,
pub(crate) prepared_release: Option<Release>,
/// Version manually set by the caller to use instead of the one determined by semantic rule
pub(crate) override_version: Option<Version>,
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -229,16 +229,16 @@ impl Package {
version: Version,
dry_run: DryRun,
) -> Result<Release, crate::step::releases::changelog::Error> {
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,
&self.versioning.changelog_sections,
self.changelog
.as_ref()
.map_or(HeaderLevel::H2, |it| it.section_header_level),
additional_tags,
actions,
);

if let Some(changelog) = self.changelog.as_mut() {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions crates/knope/src/step/releases/semver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<Package>, Error>>()?;
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 5f66580

Please sign in to comment.