Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
feat: support package spec ID
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Dec 1, 2023
1 parent 6a5c431 commit b438761
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 53 deletions.
5 changes: 3 additions & 2 deletions src/bin/cargo-info/command/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cargo::util::command_prelude::*;
use cargo::{core::PackageIdSpec, util::command_prelude::*};
use cargo_information::ops;

pub fn cli() -> Command {
Expand Down Expand Up @@ -101,8 +101,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
.get_one::<String>("package")
.map(String::as_str)
.unwrap();
let spec = PackageIdSpec::parse(package)?;
let reg_or_index = args.registry_or_index(config)?;
ops::info(package, config, reg_or_index)?;
ops::info(&spec, config, reg_or_index)?;
Ok(())
}

Expand Down
40 changes: 0 additions & 40 deletions src/ops/crate_spec.rs

This file was deleted.

38 changes: 31 additions & 7 deletions src/ops/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::task::Poll;

use anyhow::{bail, Context as _};
use cargo::core::registry::PackageRegistry;
use cargo::core::{Dependency, PackageId, Registry, SourceId, Summary, Workspace};
use cargo::core::{Dependency, PackageId, PackageIdSpec, Registry, SourceId, Summary, Workspace};
use cargo::ops::RegistryOrIndex;
use cargo::sources::source::{QueryKind, Source};
use cargo::sources::{RegistrySource, SourceConfigMap};
Expand All @@ -18,7 +18,11 @@ use crates_io::User;

use super::view::{pretty_view, suggest_cargo_tree};

pub fn info(spec: &str, config: &Config, reg_or_index: Option<RegistryOrIndex>) -> CargoResult<()> {
pub fn info(
spec: &PackageIdSpec,
config: &Config,
reg_or_index: Option<RegistryOrIndex>,
) -> CargoResult<()> {
let mut registry = PackageRegistry::new(config)?;
// Make sure we get the lock before we download anything.
let _lock = config.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
Expand All @@ -29,11 +33,20 @@ pub fn info(spec: &str, config: &Config, reg_or_index: Option<RegistryOrIndex>)
if let Ok(root) = root_manifest(None, config) {
let ws = Workspace::new(&root, config)?;
if let Some(resolve) = ops::load_pkg_lockfile(&ws)? {
if let Ok(p) = resolve.query(spec) {
if let Ok(p) = resolve.query(spec.name()) {
package_id = Some(p)
}
}
}
// If uses specific version, check if the version matches.
// If not, we need to query the specified version from the registry.
if let Some(version) = spec.version() {
if let Some(id) = package_id {
if id.version() != &version {
package_id = None
}
}
}

let (use_package_source_id, source_ids) = get_source_id(config, reg_or_index, package_id)?;
// If we don't use the package's source, we need to query the package ID from the specified registry.
Expand All @@ -47,7 +60,7 @@ pub fn info(spec: &str, config: &Config, reg_or_index: Option<RegistryOrIndex>)
// Query the package registry and pretty print the result.
// If package_id is None, find the latest version.
fn query_and_pretty_view(
spec: &str,
spec: &PackageIdSpec,
package_id: Option<PackageId>,
config: &Config,
mut registry: PackageRegistry,
Expand All @@ -65,7 +78,8 @@ fn query_and_pretty_view(
}
}
// Query without version requirement to get all index summaries.
let dep = Dependency::parse(spec, None, source_ids.original)?;
let version_req = spec.version().map(|v| v.to_string());
let dep = Dependency::parse(spec.name(), version_req.as_deref(), source_ids.original)?;
let summaries = loop {
// Exact to avoid returning all for path/git
match registry.query_vec(&dep, QueryKind::Exact) {
Expand All @@ -79,8 +93,18 @@ fn query_and_pretty_view(
let package_id = match package_id {
Some(id) => id,
None => {
// Find the latest version.
let summary = summaries.iter().max_by_key(|s| s.package_id().version());
let summary = match spec.version() {
Some(ref version_req) => {
// Find the summary which matches the version requirement.
summaries
.iter()
.find(|s| s.package_id().version() == version_req)
}
None => {
// Find the latest version.
summaries.iter().max_by_key(|s| s.package_id().version())
}
};

// If can not find the latest version, return an error.
match summary {
Expand Down
1 change: 0 additions & 1 deletion src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use info::info;
mod crate_spec;
pub mod info;
mod style;
mod view;
6 changes: 3 additions & 3 deletions tests/testsuite/cargo_information/help/stdout.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ Display info about a package in the registry

Usage: cargo info [OPTIONS] <SPEC>

Arguments:
<SPEC>

Options:
--index <INDEX> Registry index URL to search packages in
--registry <REGISTRY> Registry to search packages in
Expand All @@ -15,6 +12,9 @@ Options:
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
-h, --help Print help

Package Selection:
<SPEC> Package to inspect

Manifest Options:
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
Expand Down

0 comments on commit b438761

Please sign in to comment.