use cargo_metadata::camino::Utf8PathBuf;
use next_version::VersionUpdater;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateConfig {
pub changelog_path: Option<Utf8PathBuf>,
pub semver_check: bool,
pub changelog_update: bool,
pub release: bool,
pub publish: bool,
pub features_always_increment_minor: bool,
pub tag_name_template: Option<String>,
pub custom_minor_increment_regex: Option<String>,
pub custom_major_increment_regex: Option<String>,
pub git_only: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PackageUpdateConfig {
pub generic: UpdateConfig,
pub changelog_include: Vec<String>,
pub version_group: Option<String>,
}
impl From<UpdateConfig> for PackageUpdateConfig {
fn from(config: UpdateConfig) -> Self {
Self {
generic: config,
changelog_include: vec![],
version_group: None,
}
}
}
impl PackageUpdateConfig {
pub fn semver_check(&self) -> bool {
self.generic.semver_check
}
pub fn should_update_changelog(&self) -> bool {
self.generic.changelog_update
}
pub fn should_publish(&self) -> bool {
self.generic.publish
}
pub fn git_only(&self) -> Option<bool> {
self.generic.git_only
}
}
impl Default for UpdateConfig {
fn default() -> Self {
Self {
semver_check: true,
changelog_update: true,
release: true,
publish: true,
features_always_increment_minor: false,
git_only: None,
tag_name_template: None,
changelog_path: None,
custom_minor_increment_regex: None,
custom_major_increment_regex: None,
}
}
}
impl UpdateConfig {
pub fn with_semver_check(self, semver_check: bool) -> Self {
Self {
semver_check,
..self
}
}
pub fn with_features_always_increment_minor(
self,
features_always_increment_minor: bool,
) -> Self {
Self {
features_always_increment_minor,
..self
}
}
pub fn with_changelog_update(self, changelog_update: bool) -> Self {
Self {
changelog_update,
..self
}
}
pub fn with_publish(self, publish: bool) -> Self {
Self { publish, ..self }
}
pub fn version_updater(&self) -> Result<VersionUpdater, regex::Error> {
let mut updater = VersionUpdater::default()
.with_features_always_increment_minor(self.features_always_increment_minor);
if let Some(regex) = &self.custom_minor_increment_regex {
updater = updater.with_custom_minor_increment_regex(regex)?;
}
if let Some(regex) = &self.custom_major_increment_regex {
updater = updater.with_custom_major_increment_regex(regex)?;
}
Ok(updater)
}
}
#[cfg(test)]
mod tests {
use super::*;
use cargo_metadata::semver::Version;
#[test]
fn version_updater_with_custom_minor_regex() {
let config = UpdateConfig {
custom_minor_increment_regex: Some("minor|enhancement".to_string()),
..Default::default()
};
let updater = config.version_updater().unwrap();
let commits = ["enhancement: add new feature"];
let version = Version::new(1, 2, 3);
let new_version = updater.increment(&version, commits);
assert_eq!(new_version, Version::new(1, 3, 0));
}
#[test]
fn version_updater_with_invalid_regex() {
let config = UpdateConfig {
custom_minor_increment_regex: Some("[invalid".to_string()),
..Default::default()
};
assert!(config.version_updater().is_err());
}
#[test]
fn version_updater_without_custom_regex() {
let config = UpdateConfig::default();
let updater = config.version_updater().unwrap();
let commits = ["some change"];
let version = Version::new(1, 2, 3);
let new_version = updater.increment(&version, commits);
assert_eq!(new_version, Version::new(1, 2, 4));
}
#[test]
fn version_updater_with_custom_major_regex() {
let config = UpdateConfig {
custom_major_increment_regex: Some("major|breaking".to_string()),
..Default::default()
};
let updater = config.version_updater().unwrap();
let commits = ["breaking: remove old API"];
let version = Version::new(1, 2, 3);
let new_version = updater.increment(&version, commits);
assert_eq!(new_version, Version::new(2, 0, 0));
}
}