Skip to content

Commit

Permalink
Merge pull request #638 from epage/metadata
Browse files Browse the repository at this point in the history
feat(version): Give more control over metadata handling
  • Loading branch information
epage authored Jan 6, 2023
2 parents ddc0379 + 54c12f4 commit 0283002
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Workspace configuration is read from the following (in precedence order)
| `enable-all-features` | `--all-features` | bool | `false` | Signal to `cargo publish`, that all features should be used (requires rust 1.33+) |
| `target` | \- | string | \- | Target triple to use for the verification build |
| `dependent-version` | \- | `upgrade`, `fix`, `error`, `warn`, `ignore` | `upgrade` | Policy for upgrading path dependency versions within the workspace |
| `metadata` | \- | `optional`, `required`, `ignore`, `persistent` | `optional` | Policy for presence of absence of `--metadata` flag when changing the version |


Note: fields are from the package-configuration unless otherwise specified.
Expand Down
29 changes: 29 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Config {
pub enable_features: Option<Vec<String>>,
pub enable_all_features: Option<bool>,
pub dependent_version: Option<DependentVersion>,
pub metadata: Option<MetadataPolicy>,
pub target: Option<String>,
}

Expand Down Expand Up @@ -81,6 +82,7 @@ impl Config {
enable_features: Some(empty.enable_features().to_vec()),
enable_all_features: Some(empty.enable_all_features()),
dependent_version: Some(empty.dependent_version()),
metadata: Some(empty.metadata()),
target: None,
}
}
Expand Down Expand Up @@ -155,6 +157,9 @@ impl Config {
if let Some(dependent_version) = source.dependent_version {
self.dependent_version = Some(dependent_version);
}
if let Some(metadata) = source.metadata {
self.metadata = Some(metadata);
}
if let Some(target) = source.target.as_deref() {
self.target = Some(target.to_owned());
}
Expand Down Expand Up @@ -289,6 +294,10 @@ impl Config {
pub fn dependent_version(&self) -> DependentVersion {
self.dependent_version.unwrap_or_default()
}

pub fn metadata(&self) -> MetadataPolicy {
self.metadata.unwrap_or_default()
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -337,6 +346,26 @@ impl Default for DependentVersion {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
pub enum MetadataPolicy {
/// Apply when set, clear when not
Optional,
/// Error if not set
Required,
/// Never apply the set metadata
Ignore,
/// Keep the prior metadata if not set
Persistent,
}

impl Default for MetadataPolicy {
fn default() -> Self {
MetadataPolicy::Optional
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
#[serde(rename_all = "kebab-case")]
Expand Down
29 changes: 26 additions & 3 deletions src/steps/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,34 @@ impl PackageRelease {
self.prior_tag = Some(prior_tag);
}

pub fn bump(
&mut self,
pub fn bump<'s>(
&'s mut self,
level_or_version: &super::TargetVersion,
metadata: Option<&str>,
mut metadata: Option<&'s str>,
) -> CargoResult<()> {
match self.config.metadata() {
crate::config::MetadataPolicy::Optional => {}
crate::config::MetadataPolicy::Required => {
if metadata.is_none() {
anyhow::bail!(
"`{}` requires the metadata to be overridden",
self.meta.name
)
}
}
crate::config::MetadataPolicy::Ignore => {
if let Some(metadata) = metadata {
log::debug!("ignoring metadata `{}` for `{}`", metadata, self.meta.name);
}
metadata = None;
}
crate::config::MetadataPolicy::Persistent => {
let initial_metadata = &self.initial_version.full_version.build;
if !initial_metadata.is_empty() {
metadata.get_or_insert(initial_metadata.as_str());
}
}
}
self.planned_version =
level_or_version.bump(&self.initial_version.full_version, metadata)?;
Ok(())
Expand Down

0 comments on commit 0283002

Please sign in to comment.