Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow publishing to crates with a publish = ["custom-registry"] set #19

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn main() -> anyhow::Result<()> {
dependencies: !no_dependencies,
generator_segments: names_to_segment_selection(&without)?,
capitalize_commit,
registry: None, // TODO: remove from args? not useful for a changelog
},
crates,
)?
Expand Down
8 changes: 7 additions & 1 deletion src/command/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ pub fn changelog(opts: Options, crates: Vec<String>) -> anyhow::Result<()> {
} = opts;
let bump_spec = if dependencies { BumpSpec::Auto } else { BumpSpec::Keep };
let force_history_segmentation = false;
let ctx = crate::Context::new(crates.clone(), force_history_segmentation, bump_spec, bump_spec)?;
let ctx = crate::Context::new(
crates.clone(),
force_history_segmentation,
bump_spec,
bump_spec,
opts.registry.clone(),
)?;
let crates: Vec<_> = {
crate::traverse::dependencies(
&ctx,
Expand Down
3 changes: 2 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use release_impl::release;
pub mod changelog {
use crate::changelog::section::segment;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct Options {
pub dry_run: bool,
pub dependencies: bool,
Expand All @@ -46,6 +46,7 @@ pub mod changelog {
pub generator_segments: segment::Selection,
pub no_links: bool,
pub capitalize_commit: bool,
pub registry: Option<String>,
}
}
#[path = "changelog.rs"]
Expand Down
29 changes: 23 additions & 6 deletions src/command/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ impl Context {
bump_dependencies: BumpSpec,
changelog: bool,
changelog_links: bool,
registry: Option<String>,
) -> anyhow::Result<Self> {
let base = crate::Context::new(crate_names, changelog, bump, bump_dependencies)?;
let base = crate::Context::new(crate_names, changelog, bump, bump_dependencies, registry)?;
let changelog_links = if changelog_links {
crate::git::remote_url(&base.repo)?.map_or(Linkables::AsText, |url| Linkables::AsLinks {
repository_url: url.into(),
Expand Down Expand Up @@ -69,7 +70,14 @@ pub fn release(opts: Options, crates: Vec<String>, bump: BumpSpec, bump_dependen
);
}

let ctx = Context::new(crates, bump, bump_dependencies, allow_changelog, opts.changelog_links)?;
let ctx = Context::new(
crates,
bump,
bump_dependencies,
allow_changelog,
opts.changelog_links,
opts.registry.clone(),
)?;
if !ctx.base.crates_index.exists() {
log::warn!("Crates.io index doesn't exist. Consider using --update-crates-index to help determining if release versions are published already");
}
Expand Down Expand Up @@ -416,7 +424,7 @@ fn perform_release(ctx: &Context, options: Options, crates: &[Dependency<'_>]) -
if let Some((crate_, version)) = successful_publishees_and_version.last() {
if let Err(err) = wait_for_release(crate_, version, options.clone()) {
log::warn!(
"Failed to wait for crates-index update - trying to publish '{} v{}' anyway: {}.",
"Failed to wait for crates-index update - trying to publish '{} v{}' anyway: {:?}.",
publishee.name,
new_version,
err
Expand Down Expand Up @@ -478,7 +486,12 @@ fn wait_for_release(
let sleep_time = std::time::Duration::from_secs(1);
let crate_version = crate_version.to_string();

log::info!("Waiting for '{} v{}' to arrive in index…", crate_.name, crate_version);
log::info!(
"Waiting for '{} v{}' to arrive in index for {:.0?}…",
crate_.name,
crate_version,
timeout
);
let mut crates_index = crates_index::GitIndex::new_cargo_default()?;
let mut attempt = 0;
while start.elapsed() < timeout {
Expand All @@ -498,13 +511,17 @@ fn wait_for_release(
.rev()
.any(|version| version.version() == crate_version)
{
break;
return Ok(());
}

std::thread::sleep(sleep_time);
log::info!("attempt {}", attempt);
}
Ok(())
Err(anyhow::anyhow!(
"Timed out waiting for'{} v{}' to arrive in the index",
crate_.name,
crate_version
))
}

enum WriteMode {
Expand Down
3 changes: 3 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Context {
pub history: Option<crate::commit::History>,
pub bump: BumpSpec,
pub bump_dependencies: BumpSpec,
pub registry: Option<String>,
}

impl Context {
Expand All @@ -22,6 +23,7 @@ impl Context {
force_history_segmentation: bool,
bump: BumpSpec,
bump_dependencies: BumpSpec,
registry: Option<String>,
) -> anyhow::Result<Self> {
let meta = cargo_metadata::MetadataCommand::new().exec()?;
let root = meta.workspace_root.clone();
Expand All @@ -42,6 +44,7 @@ impl Context {
history,
bump,
bump_dependencies,
registry,
})
}

Expand Down
18 changes: 14 additions & 4 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn dependencies(
crates_this_round.push(Dependency {
package,
kind: dependency::Kind::UserSelection,
mode: if package_may_be_published(package) {
mode: if package_may_be_published(package, ctx.registry.as_deref()) {
dependency::Mode::ToBePublished {
adjustment: VersionAdjustment::Changed {
change: user_package_change,
Expand Down Expand Up @@ -262,7 +262,7 @@ fn forward_propagate_breaking_changes_for_manifest_updates<'meta>(
.workspace_members
.iter()
.map(|wmid| package_by_id(&ctx.meta, wmid))
.filter(|p| package_may_be_published(p)) // will publish, non-publishing ones need no safety bumps
.filter(|p| package_may_be_published(p, ctx.registry.as_deref())) // will publish, non-publishing ones need no safety bumps
.collect();
let mut set_to_expand_from = &backing;
let mut seen = BTreeSet::default();
Expand Down Expand Up @@ -340,8 +340,18 @@ fn forward_propagate_breaking_changes_for_manifest_updates<'meta>(
Ok(())
}

fn package_may_be_published(p: &Package) -> bool {
p.publish.is_none()
fn package_may_be_published(p: &Package, registry: Option<&str>) -> bool {
match &p.publish {
// Empty vec seems to be the translation of `publish = false` in Cargo.toml
Some(registries) if registries.is_empty() => false,
Some(registries) => match registry {
Some(registry) => registries.iter().any(|r| r == registry),
// TODO: The user didn't specify any registry on the cmdline, assume they want to publish anything
None => true,
},
// TODO: Only do this if registry was unset, or equals the default crates-io registry?
None => true,
}
}

fn forward_propagate_breaking_changes_for_publishing(
Expand Down
Loading