Use VersionReq for Options::version_req and update usage of

`CrateName` in `binstall::install`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-08-07 18:42:59 +10:00
parent 686cae6ae8
commit 065f62a625
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
4 changed files with 21 additions and 24 deletions

View file

@ -1,6 +1,6 @@
use std::path::PathBuf;
use compact_str::CompactString;
use semver::VersionReq;
use crate::{metafiles::binstall_v1::MetaData, DesiredTargets, PkgOverride};
@ -14,7 +14,7 @@ pub struct Options {
pub no_symlinks: bool,
pub dry_run: bool,
pub force: bool,
pub version: Option<CompactString>,
pub version_req: Option<VersionReq>,
pub manifest_path: Option<PathBuf>,
pub cli_overrides: PkgOverride,
pub desired_targets: DesiredTargets,

View file

@ -19,7 +19,7 @@ pub async fn install(
fetcher,
package,
name,
version,
version_req,
bin_path,
bin_files,
} => {
@ -31,7 +31,7 @@ pub async fn install(
.map(|option| {
option.map(|bins| MetaData {
name,
version_req: version,
version_req,
current_version,
source: Source::cratesio_registry(),
target,

View file

@ -4,10 +4,11 @@ use std::{
};
use cargo_toml::{Package, Product};
use compact_str::{format_compact, CompactString};
use compact_str::{CompactString, ToCompactString};
use log::{debug, error, info, warn};
use miette::{miette, Result};
use reqwest::Client;
use semver::VersionReq;
use super::Options;
use crate::{
@ -21,7 +22,7 @@ pub enum Resolution {
fetcher: Arc<dyn Fetcher>,
package: Package<Meta>,
name: CompactString,
version: CompactString,
version_req: CompactString,
bin_path: PathBuf,
bin_files: Vec<bins::BinFile>,
},
@ -83,30 +84,26 @@ pub async fn resolve(
) -> Result<Resolution> {
info!("Installing package: '{}'", crate_name);
let mut version: CompactString = match (&crate_name.version, &opts.version) {
let version_req: VersionReq = match (&crate_name.version_req, &opts.version_req) {
(Some(version), None) => version.clone(),
(None, Some(version)) => version.clone(),
(Some(_), Some(_)) => Err(BinstallError::SuperfluousVersionOption)?,
(None, None) => "*".into(),
(None, None) => VersionReq::STAR,
};
// Treat 0.1.2 as =0.1.2
if version
.chars()
.next()
.map(|ch| ch.is_ascii_digit())
.unwrap_or(false)
{
version = format_compact!("={version}");
}
// 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: support git-based fetches (whole repo name rather than just crate name)
let manifest = match opts.manifest_path.clone() {
Some(manifest_path) => load_manifest_path(manifest_path)?,
None => {
fetch_crate_cratesio(&client, &crates_io_api_client, &crate_name.name, &version).await?
fetch_crate_cratesio(
&client,
&crates_io_api_client,
&crate_name.name,
&version_req,
)
.await?
}
};
@ -175,7 +172,7 @@ pub async fn resolve(
fetcher,
package,
name: crate_name.name,
version,
version_req: version_req.to_compact_string(),
bin_path,
bin_files,
}

View file

@ -9,9 +9,9 @@ use std::{
};
use clap::{builder::PossibleValue, AppSettings, Parser};
use compact_str::CompactString;
use log::{debug, error, info, warn, LevelFilter};
use miette::{miette, Result, WrapErr};
use semver::VersionReq;
use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
use tokio::{runtime::Runtime, task::block_in_place};
@ -46,8 +46,8 @@ struct Options {
///
/// Cannot be used when multiple packages are installed at once, use the attached version
/// syntax in that case.
#[clap(help_heading = "Package selection", long = "version")]
version_req: Option<CompactString>,
#[clap(help_heading = "Package selection", long = "version", parse(try_from_str = parse_version))]
version_req: Option<VersionReq>,
/// Override binary target set.
///
@ -371,7 +371,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
no_symlinks: opts.no_symlinks,
dry_run: opts.dry_run,
force: opts.force,
version: opts.version_req.take(),
version_req: opts.version_req.take(),
manifest_path: opts.manifest_path.take(),
cli_overrides,
desired_targets,