Skip to content

Commit

Permalink
Use tame-index (#56)
Browse files Browse the repository at this point in the history
* Checkpoint

* Refactor to use tame-index solely

* Update to follow tame-index changes

* Use published version

* Fix deny

* Speed up non-index tests
  • Loading branch information
Jake-Shadle committed Jul 25, 2023
1 parent fa175ca commit 5dc061e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 274 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-22.04]
features: ["--all-features", "--features prefer-index", null]
features: ["--features targets", "--features prefer-index", null]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ homepage = "https://github.com/EmbarkStudios/krates"
keywords = ["cargo", "metadata", "graph"]
categories = ["visualization"]
exclude = [".github", "tests"]
rust-version = "1.65.0"

[features]
default = []
# Uses the index as the source of truth for what features are available for a crate
prefer-index = []
# Uses crates-index to read the index rather than rely on a user-provided implementation
with-crates-index = ["prefer-index", "crates-index"]
prefer-index = ["dep:tame-index"]
# Adds support for filtering target specific dependencies
targets = ["cfg-expr/targets"]

[dependencies]
# Used for acquiring and/or deserializing `cargo metadata` output
cargo_metadata = "0.15"
cargo_metadata = "0.17"
# Used to parse and evaluate cfg() expressions for dependencies
cfg-expr = "0.15"
# Allows inspection of the cargo registry index(ices)
crates-index = { version = "0.19", optional = true, default-features = false }
tame-index = { version = "0.2", optional = true, default-features = false }
# Used to create and traverse graph structures
petgraph = "0.6"
# Used for checking version requirements
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ skip = [
skip-tree = [
# dev only but still sigh
{ name = "windows-sys", version = "<0.48.0" },
# petgraph uses an older indexmap
{ name = "indexmap", version = "=1.9.3" },
]

[sources]
Expand Down
67 changes: 30 additions & 37 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub struct Builder {
ignore_kinds: u32,
workspace: bool,
#[cfg(feature = "prefer-index")]
crates_io_index: Option<Box<dyn index::CratesIoIndex>>,
crates_io_index: Option<tame_index::index::ComboIndexCache>,
}

impl Builder {
Expand Down Expand Up @@ -490,40 +490,34 @@ impl Builder {
self
}

/// Specifies an implementation that can be used to query the crates.io
/// index to ensure that the features are all correct
///
/// This method _must_ be called with an implementation if not using
/// the `with-crates-index` feature
#[cfg(all(feature = "prefer-index", not(feature = "with-crates-index")))]
pub fn with_crates_io_index(
&mut self,
crates_io_index: Box<dyn index::CratesIoIndex>,
) -> &mut Self {
self.crates_io_index = Some(crates_io_index);
self
}

/// Configures the index implementation to use `crates-index`
///
/// As of 1.70.0 the cargo default is to use the HTTP sparse index, which is
/// vastly faster than the old crates.io git index, and is the recommended
/// one to use if you are using 1.70.0+.
/// Configures the index implementation
///
/// This method allows overriding the location of your `CARGO_HOME`, but note
/// that no fetching from the remote index is performed by this library, so
/// it is your responsibility to have called `cargo fetch` or similar to have
/// an up to date index cache at the location provided
#[cfg(all(feature = "prefer-index", feature = "with-crates-index"))]
///
/// This method takes into account the local environment to open the correct
/// crates.io registry, or replacement registry if the user has configured that
///
/// You can force the usage of the git registry (if not overridden in the
/// local environment) by specifying `Some("1.69.0")` or lower semver as the
/// sparse registry was not made the default until `1.70.0`
#[cfg(feature = "prefer-index")]
pub fn with_crates_io_index(
&mut self,
cargo_home: Option<&Path>,
index_kind: index::IndexKind,
) -> Result<&mut Self, Error> {
self.crates_io_index = Some(match index_kind {
index::IndexKind::Sparse => Box::new(index::sparse(cargo_home)?),
index::IndexKind::Git => Box::new(index::git(cargo_home)?),
});
mut self,
config_root: Option<tame_index::PathBuf>,
cargo_home: Option<tame_index::PathBuf>,
cargo_version: Option<&str>,
) -> Result<Self, Error> {
let crates_io =
tame_index::IndexUrl::crates_io(config_root, cargo_home.as_deref(), cargo_version)?;

let index = tame_index::index::ComboIndexCache::new(
tame_index::IndexLocation::new(crates_io).with_root(cargo_home),
)?;

self.crates_io_index = Some(index);
Ok(self)
}

Expand Down Expand Up @@ -626,16 +620,15 @@ impl Builder {
{
let resolved = md.resolve.ok_or(Error::NoResolveGraph)?;

#[cfg(feature = "prefer-index")]
let index = self.crates_io_index;

let mut packages = md.packages;
packages.sort_by(|a, b| a.id.cmp(&b.id));

// Load all the cache entries from disk for all the possible _unique_
// crates in the graph so that we don't need to access disk again
#[cfg(feature = "prefer-index")]
if let Some(index) = &index {
index.prepare_cache_entries(packages.iter().map(|pkg| pkg.name.clone()).collect());
}
let index = self.crates_io_index.map(|index| {
index::CachingIndex::new(index, packages.iter().map(|pkg| pkg.name.clone()).collect())
});

let mut workspace_members = md.workspace_members;
workspace_members.sort();
Expand Down Expand Up @@ -856,7 +849,7 @@ impl Builder {

#[cfg(feature = "prefer-index")]
if let Some(index) = &index {
index::fix_features(index.as_ref(), krate);
index::fix_features(index, krate);
}

// Cargo puts out a flat list of the enabled features, but we need
Expand Down Expand Up @@ -1462,7 +1455,7 @@ impl Builder {
Ok(Krates {
graph,
workspace_members,
lock_file: md.workspace_root.join("Cargo.lock"),
workspace_root: md.workspace_root,
krates_end,
})
}
Expand Down

0 comments on commit 5dc061e

Please sign in to comment.