Skip to content

Commit

Permalink
Add third party registry support
Browse files Browse the repository at this point in the history
This adds a `--registry` command line flag to the check release
capabilities. The argument to the flag is the name of the registry to
use to lookup the crates when using the default behavior or
--baseline-version. If left unset, the default behavior remains of
--checking crates.io.
  • Loading branch information
drewkett committed Sep 25, 2024
1 parent 259395b commit 28dfdcc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct GlobalConfig {
stdout: AutoStream<Box<dyn Write + 'static>>,
stderr: AutoStream<Box<dyn Write + 'static>>,
feature_flags: HashSet<FeatureFlag>,
/// Registry name to look up crates in
registry: Option<String>,
}

impl Default for GlobalConfig {
Expand All @@ -41,6 +43,7 @@ impl GlobalConfig {
stdout: AutoStream::new(Box::new(std::io::stdout()), stdout_choice),
stderr: AutoStream::new(Box::new(std::io::stderr()), stderr_choice),
feature_flags: HashSet::new(),
registry: None,
}
}

Expand Down Expand Up @@ -316,6 +319,19 @@ impl GlobalConfig {
pub fn feature_flags(&self) -> &HashSet<FeatureFlag> {
&self.feature_flags
}

/// Set (overwrite) the name of the registry to use for crate lookup
#[inline]
pub fn set_registry(&mut self, registry: String) -> &mut Self {
self.registry = Some(registry);
self
}

/// Return the name of the registry to use for crate lookup
#[inline]
pub fn registry(&self) -> Option<&str> {
self.registry.as_deref()
}
}

/// A feature flag for gating unstable `cargo-semver-checks` features.
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ fn main() {
None => args.check_release,
};

if let Some(registry) = &check_release.registry {
config.set_registry(registry.clone());
}

let check: cargo_semver_checks::Check = check_release.into();

let report = exit_on_error(config.is_error(), || check.check_release(&mut config));
Expand Down Expand Up @@ -532,6 +536,11 @@ struct CheckRelease {
#[arg(long = "target")]
build_target: Option<String>,

/// Name of registry to use for crate lookups. Used with default behavior
/// and with `--baseline-version`.
#[arg(long = "registry")]
registry: Option<String>,

#[clap(flatten)]
unstable_options: UnstableOptions,
}
Expand Down
5 changes: 4 additions & 1 deletion src/rustdoc_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,14 @@ fn create_placeholder_rustdoc_manifest(
},
dependencies: {
let project_with_features: DependencyDetail = match crate_source {
CrateSource::Registry { version, .. } => DependencyDetail {
CrateSource::Registry {
version, index_url, ..
} => DependencyDetail {
// We need the *exact* version as a dependency, or else cargo will
// give us the latest semver-compatible version which is not we want.
// Fixes: https://github.com/obi1kenobi/cargo-semver-checks/issues/261
version: Some(format!("={version}")),
registry_index: Some(index_url.clone()),
features: crate_source
.feature_list_from_config(config, crate_data.feature_config),
default_features: matches!(
Expand Down
66 changes: 43 additions & 23 deletions src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::GlobalConfig;
pub(crate) enum CrateSource<'a> {
Registry {
crate_: &'a tame_index::IndexVersion,
/// The url of the registry index that holds the specified crate
index_url: String,
version: String,
},
ManifestPath {
Expand Down Expand Up @@ -634,6 +636,8 @@ pub(crate) struct RustdocFromRegistry {
target_root: PathBuf,
version: Option<semver::Version>,
index: tame_index::index::ComboIndex,
/// The url of the index for the given registry
index_url: String,
}

impl core::fmt::Debug for RustdocFromRegistry {
Expand All @@ -648,32 +652,46 @@ impl core::fmt::Debug for RustdocFromRegistry {

impl RustdocFromRegistry {
pub fn new(target_root: &std::path::Path, config: &mut GlobalConfig) -> anyhow::Result<Self> {
let index_url = tame_index::IndexUrl::crates_io(
// This is the config root, where .cargo/config.toml configuration files
// are crawled to determine if crates.io has been source replaced
// <https://doc.rust-lang.org/cargo/reference/source-replacement.html>
// if not specified it defaults to the current working directory,
// which is the same default that cargo uses, though note this can be
// extremely confusing if one can specify the manifest path of the
// crate from a different current working directory, though AFAICT
// this is not how this binary works
None,
// If set this overrides the CARGO_HOME that is used for both finding
// the "global" default config if not overriden during directory
// traversal to the root, as well as where the various registry
// indices/git sources are rooted. This is generally only useful
// for testing
None,
// If set, overrides the version of the cargo binary used, this is used
// as a fallback to determine if the version is 1.70.0+, which means
// the default crates.io registry to use is the sparse registry, else
// it is the old git registry
None,
)
.context("failed to obtain crates.io url")?;
let index_url = match config.registry() {
Some(registry_name) => {
tame_index::IndexUrl::for_registry_name(
// No need to override the config root. See comment below for more information.
None,
// No need to override the cargo home. See comment below for more information.
None,
registry_name,
)
.with_context(|| format!("failed to obtain url for registry '{}'", registry_name))?
}
None => tame_index::IndexUrl::crates_io(
// This is the config root, where .cargo/config.toml configuration files
// are crawled to determine if crates.io has been source replaced
// <https://doc.rust-lang.org/cargo/reference/source-replacement.html>
// if not specified it defaults to the current working directory,
// which is the same default that cargo uses, though note this can be
// extremely confusing if one can specify the manifest path of the
// crate from a different current working directory, though AFAICT
// this is not how this binary works
None,
// If set this overrides the CARGO_HOME that is used for both finding
// the "global" default config if not overriden during directory
// traversal to the root, as well as where the various registry
// indices/git sources are rooted. This is generally only useful
// for testing
None,
// If set, overrides the version of the cargo binary used, this is used
// as a fallback to determine if the version is 1.70.0+, which means
// the default crates.io registry to use is the sparse registry, else
// it is the old git registry
None,
)
.context("failed to obtain crates.io url")?,
};

use tame_index::index::{self, ComboIndexCache};

let index_url_str = index_url.as_str().to_string();

let index_cache = ComboIndexCache::new(tame_index::IndexLocation::new(index_url))
.context("failed to open crates.io index cache")?;

Expand Down Expand Up @@ -706,6 +724,7 @@ impl RustdocFromRegistry {
target_root: target_root.to_owned(),
version: None,
index,
index_url: index_url_str,
})
}

Expand Down Expand Up @@ -818,6 +837,7 @@ impl RustdocGenerator for RustdocFromRegistry {
self.target_root.clone(),
CrateSource::Registry {
version: crate_.version.to_string(),
index_url: self.index_url.clone(),
crate_,
},
crate_data,
Expand Down

0 comments on commit 28dfdcc

Please sign in to comment.