Support specifing ver via crate_name@version

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-12 17:08:54 +10:00
parent 41961ce218
commit ff0bd4d948
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -36,14 +36,14 @@ struct Options {
/// ///
/// This must be a crates.io package name. /// This must be a crates.io package name.
#[clap(value_name = "crate")] #[clap(value_name = "crate")]
name: String, crate_name: CrateName,
/// Semver filter to select the package version to install. /// Semver filter to select the package version to install.
/// ///
/// This is in Cargo.toml dependencies format: `--version 1.2.3` is equivalent to /// This is in Cargo.toml dependencies format: `--version 1.2.3` is equivalent to
/// `--version "^1.2.3"`. Use `=1.2.3` to install a specific version. /// `--version "^1.2.3"`. Use `=1.2.3` to install a specific version.
#[clap(long, default_value = "*")] #[clap(long)]
version: String, version: Option<String>,
/// Override binary target set. /// Override binary target set.
/// ///
@ -237,26 +237,33 @@ async fn entry() -> Result<()> {
.map_err(BinstallError::from) .map_err(BinstallError::from)
.wrap_err("Creating a temporary directory failed.")?; .wrap_err("Creating a temporary directory failed.")?;
info!("Installing package: '{}'", opts.name); info!("Installing package: '{}'", opts.crate_name);
let version = match (&opts.crate_name.version, &opts.version) {
(Some(version), None) => version.to_string(),
(None, Some(version)) => version.to_string(),
(Some(_), Some(_)) => Err(BinstallError::DuplicateVersionReq)?,
(None, None) => "*".to_string(),
};
// Fetch crate via crates.io, git, or use a local manifest path // Fetch crate via crates.io, git, or use a local manifest path
// TODO: work out which of these to do based on `opts.name` // TODO: work out which of these to do based on `opts.name`
// TODO: support git-based fetches (whole repo name rather than just crate name) // TODO: support git-based fetches (whole repo name rather than just crate name)
let manifest = match opts.manifest_path.clone() { let manifest = match opts.manifest_path.clone() {
Some(manifest_path) => load_manifest_path(manifest_path.join("Cargo.toml"))?, Some(manifest_path) => load_manifest_path(manifest_path.join("Cargo.toml"))?,
None => fetch_crate_cratesio(&client, &opts.name, &opts.version).await?, None => fetch_crate_cratesio(&client, &opts.crate_name.name, &version).await?,
}; };
let package = manifest.package.unwrap(); let package = manifest.package.unwrap();
let is_plain_version = semver::Version::from_str(&opts.version).is_ok(); let is_plain_version = semver::Version::from_str(&version).is_ok();
if is_plain_version && package.version != opts.version { if is_plain_version && package.version != version {
warn!("Warning!"); warn!("Warning!");
eprintln!( eprintln!(
"{:?}", "{:?}",
miette::Report::new(BinstallError::VersionWarning { miette::Report::new(BinstallError::VersionWarning {
ver: package.version.clone(), ver: package.version.clone(),
req: opts.version.clone() req: version.clone(),
}) })
); );
@ -312,7 +319,9 @@ async fn entry() -> Result<()> {
meta.merge(&cli_overrides); meta.merge(&cli_overrides);
// Generate temporary binary path // Generate temporary binary path
let bin_path = temp_dir.path().join(format!("bin-{}", opts.name)); let bin_path = temp_dir
.path()
.join(format!("bin-{}", opts.crate_name.name));
debug!("Using temporary binary path: {}", bin_path.display()); debug!("Using temporary binary path: {}", bin_path.display());
let bin_files = collect_bin_files( let bin_files = collect_bin_files(
@ -363,6 +372,7 @@ async fn entry() -> Result<()> {
opts, opts,
package, package,
temp_dir, temp_dir,
version,
&bin_path, &bin_path,
&bin_files, &bin_files,
) )
@ -438,6 +448,7 @@ async fn install_from_package(
opts: Options, opts: Options,
package: Package<Meta>, package: Package<Meta>,
temp_dir: TempDir, temp_dir: TempDir,
version: String,
bin_path: &Path, bin_path: &Path,
bin_files: &[bins::BinFile], bin_files: &[bins::BinFile],
) -> Result<()> { ) -> Result<()> {
@ -478,7 +489,7 @@ async fn install_from_package(
} }
let cvs = metafiles::CrateVersionSource { let cvs = metafiles::CrateVersionSource {
name: opts.name, name: opts.crate_name.name,
version: package.version.parse().into_diagnostic()?, version: package.version.parse().into_diagnostic()?,
source: metafiles::Source::Registry( source: metafiles::Source::Registry(
url::Url::parse("https://github.com/rust-lang/crates.io-index").unwrap(), url::Url::parse("https://github.com/rust-lang/crates.io-index").unwrap(),
@ -521,7 +532,7 @@ async fn install_from_package(
c2.insert( c2.insert(
cvs.clone(), cvs.clone(),
metafiles::v2::CrateInfo { metafiles::v2::CrateInfo {
version_req: Some(opts.version), version_req: Some(version),
bins, bins,
profile: "release".into(), profile: "release".into(),
target: fetcher.target().to_string(), target: fetcher.target().to_string(),