Skip to content

Commit

Permalink
Merge pull request #846 from epage/workspace
Browse files Browse the repository at this point in the history
feat(publish): Use Cargo's workspace publish where possible
  • Loading branch information
epage authored Dec 12, 2024
2 parents e60409a + 6003999 commit 2435a4b
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options:
--prev-tag-name <NAME> The name of tag for the previous release
-c, --config <PATH> Custom config file
--isolated Ignore implicit configuration files
-Z <FEATURE> Unstable options
--sign Sign both git commit and tag
--dependent-version <ACTION> Specify how workspace dependencies on this crate should be
handed [possible values: upgrade, fix]
Expand Down
99 changes: 94 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::ops::cargo;
pub struct Config {
#[serde(skip)]
pub is_workspace: bool,
pub unstable: Unstable,
pub allow_branch: Option<Vec<String>>,
pub sign_commit: Option<bool>,
pub sign_tag: Option<bool>,
Expand Down Expand Up @@ -50,6 +51,7 @@ impl Config {
let empty = Config::new();
Config {
is_workspace: true,
unstable: Unstable::from_defaults(),
allow_branch: Some(
empty
.allow_branch()
Expand Down Expand Up @@ -93,6 +95,7 @@ impl Config {
}

pub fn update(&mut self, source: &Config) {
self.unstable.update(&source.unstable);
if let Some(allow_branch) = source.allow_branch.as_deref() {
self.allow_branch = Some(allow_branch.to_owned());
}
Expand Down Expand Up @@ -174,6 +177,10 @@ impl Config {
}
}

pub fn unstable(&self) -> &Unstable {
&self.unstable
}

pub fn allow_branch(&self) -> impl Iterator<Item = &str> {
self.allow_branch
.as_deref()
Expand Down Expand Up @@ -292,11 +299,7 @@ impl Config {
cargo::Features::All
} else {
let features = self.enable_features();
if features.is_empty() {
cargo::Features::None
} else {
cargo::Features::Selective(features.to_owned())
}
cargo::Features::Selective(features.to_owned())
}
}

Expand All @@ -313,6 +316,47 @@ impl Config {
}
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")]
pub struct Unstable {
workspace_publish: Option<bool>,
}

impl Unstable {
pub fn new() -> Self {
Default::default()
}

pub fn from_defaults() -> Self {
let empty = Self::new();
Self {
workspace_publish: Some(empty.workspace_publish()),
}
}
pub fn update(&mut self, source: &Self) {
if let Some(workspace_publish) = source.workspace_publish {
self.workspace_publish = Some(workspace_publish);
}
}

pub fn workspace_publish(&self) -> bool {
self.workspace_publish.unwrap_or(false)
}
}

impl From<Vec<UnstableValues>> for Unstable {
fn from(values: Vec<UnstableValues>) -> Self {
let mut unstable = Unstable::new();
for value in values {
match value {
UnstableValues::WorkspacePublish(value) => unstable.workspace_publish = Some(value),
}
}
unstable
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Replace {
Expand Down Expand Up @@ -596,6 +640,10 @@ pub struct ConfigArgs {
#[arg(long)]
pub isolated: bool,

/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
pub z: Vec<UnstableValues>,

/// Sign both git commit and tag
#[arg(long, overrides_with("no_sign"))]
pub sign: bool,
Expand Down Expand Up @@ -630,6 +678,7 @@ pub struct ConfigArgs {
impl ConfigArgs {
pub fn to_config(&self) -> Config {
let mut config = Config {
unstable: Unstable::from(self.z.clone()),
allow_branch: self.allow_branch.clone(),
sign_commit: self.sign(),
sign_tag: self.sign(),
Expand All @@ -649,6 +698,46 @@ impl ConfigArgs {
}
}

#[derive(Clone, Debug)]
pub enum UnstableValues {
WorkspacePublish(bool),
}

impl std::str::FromStr for UnstableValues {
type Err = anyhow::Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
let (name, mut value) = value.split_once('=').unwrap_or((value, ""));
match name {
"workspace-publish" => {
if value.is_empty() {
value = "true";
}
let value = match value {
"true" => true,
"false" => false,
_ => anyhow::bail!(
"unsupported value `{name}={value}`, expected one of `true`, `false`"
),
};
Ok(UnstableValues::WorkspacePublish(value))
}
_ => {
anyhow::bail!("unsupported unstable feature name `{name}` (value `{value}`)");
}
}
}
}

impl std::fmt::Display for UnstableValues {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::WorkspacePublish(true) => "workspace-publish".fmt(fmt),
Self::WorkspacePublish(false) => "".fmt(fmt),
}
}
}

#[derive(Clone, Default, Debug, clap::Args)]
#[command(next_help_heading = "Commit")]
pub struct CommitArgs {
Expand Down
43 changes: 28 additions & 15 deletions src/ops/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use std::path::Path;

use bstr::ByteSlice;
use itertools::Itertools as _;

use crate::config::{self, CertsSource};
use crate::error::CargoResult;
Expand Down Expand Up @@ -54,8 +55,8 @@ pub fn publish(
dry_run: bool,
verify: bool,
manifest_path: &Path,
pkgid: Option<&str>,
features: &Features,
pkgids: &[&str],
features: &[&Features],
registry: Option<&str>,
target: Option<&str>,
) -> CargoResult<bool> {
Expand All @@ -68,7 +69,10 @@ pub fn publish(
manifest_path.to_str().unwrap(),
];

if let Some(pkgid) = pkgid {
if 1 < pkgids.len() {
command.push("-Zpackage-workspace");
}
for pkgid in pkgids {
command.push("--package");
command.push(pkgid);
}
Expand All @@ -92,18 +96,27 @@ pub fn publish(
command.push(target);
}

let feature_arg;
match features {
Features::None => (),
Features::Selective(vec) => {
feature_arg = vec.join(" ");
command.push("--features");
command.push(&feature_arg);
}
Features::All => {
command.push("--all-features");
}
};
if features.iter().any(|f| matches!(f, Features::None)) {
command.push("--no-default-features");
}
if features.iter().any(|f| matches!(f, Features::All)) {
command.push("--all-features");
}
let selective = features
.iter()
.filter_map(|f| {
if let Features::Selective(f) = f {
Some(f)
} else {
None
}
})
.flatten()
.join(",");
if !selective.is_empty() {
command.push("--features");
command.push(&selective);
}

call(command, false)
}
Expand Down
4 changes: 4 additions & 0 deletions src/steps/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct ChangesStep {
#[arg(long)]
isolated: bool,

/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
z: Vec<crate::config::UnstableValues>,

/// Comma-separated globs of branch names a release can happen from
#[arg(long, value_delimiter = ',')]
allow_branch: Option<Vec<String>>,
Expand Down
5 changes: 5 additions & 0 deletions src/steps/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub struct CommitStep {
#[arg(long)]
isolated: bool,

/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
z: Vec<config::UnstableValues>,

/// Comma-separated globs of branch names a release can happen from
#[arg(long, value_delimiter = ',')]
allow_branch: Option<Vec<String>>,
Expand Down Expand Up @@ -112,6 +116,7 @@ impl CommitStep {
config::ConfigArgs {
custom_config: self.custom_config.clone(),
isolated: self.isolated,
z: self.z.clone(),
allow_branch: self.allow_branch.clone(),
commit: self.commit.clone(),
..Default::default()
Expand Down
5 changes: 5 additions & 0 deletions src/steps/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub struct HookStep {
#[arg(long)]
isolated: bool,

/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
z: Vec<crate::config::UnstableValues>,

/// Comma-separated globs of branch names a release can happen from
#[arg(long, value_delimiter = ',')]
allow_branch: Option<Vec<String>>,
Expand Down Expand Up @@ -158,6 +162,7 @@ impl HookStep {
crate::config::ConfigArgs {
custom_config: self.custom_config.clone(),
isolated: self.isolated,
z: self.z.clone(),
allow_branch: self.allow_branch.clone(),
..Default::default()
}
Expand Down
5 changes: 5 additions & 0 deletions src/steps/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct OwnerStep {
#[arg(long)]
isolated: bool,

/// Unstable options
#[arg(short = 'Z', value_name = "FEATURE")]
z: Vec<crate::config::UnstableValues>,

/// Comma-separated globs of branch names a release can happen from
#[arg(long, value_delimiter = ',')]
allow_branch: Option<Vec<String>>,
Expand Down Expand Up @@ -135,6 +139,7 @@ impl OwnerStep {
crate::config::ConfigArgs {
custom_config: self.custom_config.clone(),
isolated: self.isolated,
z: self.z.clone(),
allow_branch: self.allow_branch.clone(),
..Default::default()
}
Expand Down
Loading

0 comments on commit 2435a4b

Please sign in to comment.