mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +00:00
Feature: SupportdDisable of strategies for crate using Cargo.toml
(#1828)
* Refactor: Move `Strategy` to `binstalk-types` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add serialisation test for `Strategy` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add support to disable strategies via crate `Cargo.toml` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add e2e-test Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix `Cargo.toml` disabled strategy checking for compile strategy Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Optimize `resolve_inner`: Cache meta override Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add compile-time length checking for `Strategy` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * More optimization Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix order of override: cli options alwayus takes precedence Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add missing manifest for e2e-test Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
09d61d081d
commit
3f29fbe83a
11 changed files with 206 additions and 57 deletions
|
@ -13,12 +13,12 @@ use binstalk::{
|
|||
ops::resolve::{CrateName, VersionReqExt},
|
||||
registry::Registry,
|
||||
};
|
||||
use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
|
||||
use binstalk_manifests::cargo_toml_binstall::Strategy;
|
||||
use clap::{builder::PossibleValue, error::ErrorKind, CommandFactory, Parser, ValueEnum};
|
||||
use compact_str::CompactString;
|
||||
use log::LevelFilter;
|
||||
use semver::VersionReq;
|
||||
use strum::EnumCount;
|
||||
use strum_macros::EnumCount;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
|
@ -162,13 +162,13 @@ pub struct Args {
|
|||
value_delimiter(','),
|
||||
env = "BINSTALL_STRATEGIES"
|
||||
)]
|
||||
pub(crate) strategies: Vec<Strategy>,
|
||||
pub(crate) strategies: Vec<StrategyWrapped>,
|
||||
|
||||
/// Disable the strategies specified.
|
||||
/// If a strategy is specified in `--strategies` and `--disable-strategies`,
|
||||
/// then it will be removed.
|
||||
#[clap(help_heading = "Overrides", long, value_delimiter(','))]
|
||||
pub(crate) disable_strategies: Vec<Strategy>,
|
||||
pub(crate) disable_strategies: Vec<StrategyWrapped>,
|
||||
|
||||
/// If `--github-token` or environment variable `GITHUB_TOKEN`/`GH_TOKEN`
|
||||
/// is not specified, then cargo-binstall will try to extract github token from
|
||||
|
@ -431,16 +431,24 @@ impl Default for RateLimit {
|
|||
}
|
||||
|
||||
/// Strategy for installing the package
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum, EnumCount)]
|
||||
#[repr(u8)]
|
||||
pub(crate) enum Strategy {
|
||||
/// Attempt to download official pre-built artifacts using
|
||||
/// information provided in `Cargo.toml`.
|
||||
CrateMetaData,
|
||||
/// Query third-party QuickInstall for the crates.
|
||||
QuickInstall,
|
||||
/// Build the crates from source using `cargo-build`.
|
||||
Compile,
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||
pub(crate) struct StrategyWrapped(pub(crate) Strategy);
|
||||
|
||||
impl StrategyWrapped {
|
||||
const VARIANTS: &'static [Self; 3] = &[
|
||||
Self(Strategy::CrateMetaData),
|
||||
Self(Strategy::QuickInstall),
|
||||
Self(Strategy::Compile),
|
||||
];
|
||||
}
|
||||
|
||||
impl ValueEnum for StrategyWrapped {
|
||||
fn value_variants<'a>() -> &'a [Self] {
|
||||
Self::VARIANTS
|
||||
}
|
||||
fn to_possible_value(&self) -> Option<PossibleValue> {
|
||||
Some(PossibleValue::new(self.0.to_str()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse() -> Args {
|
||||
|
@ -532,7 +540,7 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
|||
let mut is_variant_present = [false; Strategy::COUNT];
|
||||
|
||||
for strategy in &opts.strategies {
|
||||
let index = *strategy as u8 as usize;
|
||||
let index = strategy.0 as u8 as usize;
|
||||
if is_variant_present[index] {
|
||||
new_dup_strategy_err().exit()
|
||||
} else {
|
||||
|
@ -543,9 +551,9 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
|||
// Default strategies if empty
|
||||
if opts.strategies.is_empty() {
|
||||
opts.strategies = vec![
|
||||
Strategy::CrateMetaData,
|
||||
Strategy::QuickInstall,
|
||||
Strategy::Compile,
|
||||
StrategyWrapped(Strategy::CrateMetaData),
|
||||
StrategyWrapped(Strategy::QuickInstall),
|
||||
StrategyWrapped(Strategy::Compile),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -573,7 +581,8 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
|||
}
|
||||
|
||||
// Ensure that Strategy::Compile is specified as the last strategy
|
||||
if opts.strategies[..(opts.strategies.len() - 1)].contains(&Strategy::Compile) {
|
||||
if opts.strategies[..(opts.strategies.len() - 1)].contains(&StrategyWrapped(Strategy::Compile))
|
||||
{
|
||||
command
|
||||
.error(
|
||||
ErrorKind::InvalidValue,
|
||||
|
@ -593,10 +602,14 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use strum::VariantArray;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn verify_cli() {
|
||||
Args::command().debug_assert()
|
||||
}
|
||||
|
||||
const _: () = assert!(Strategy::VARIANTS.len() == StrategyWrapped::VARIANTS.len());
|
||||
}
|
||||
|
|
|
@ -21,7 +21,9 @@ use binstalk::{
|
|||
},
|
||||
};
|
||||
use binstalk_manifests::{
|
||||
cargo_config::Config, cargo_toml_binstall::PkgOverride, crates_manifests::Manifests,
|
||||
cargo_config::Config,
|
||||
cargo_toml_binstall::{PkgOverride, Strategy},
|
||||
crates_manifests::Manifests,
|
||||
};
|
||||
use file_format::FileFormat;
|
||||
use home::cargo_home;
|
||||
|
@ -30,11 +32,7 @@ use miette::{miette, Report, Result, WrapErr};
|
|||
use tokio::task::block_in_place;
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
use crate::{
|
||||
args::{Args, Strategy},
|
||||
gh_token, git_credentials, install_path,
|
||||
ui::confirm,
|
||||
};
|
||||
use crate::{args::Args, gh_token, git_credentials, install_path, ui::confirm};
|
||||
|
||||
pub fn install_crates(
|
||||
args: Args,
|
||||
|
@ -46,7 +44,7 @@ pub fn install_crates(
|
|||
let resolvers: Vec<_> = args
|
||||
.strategies
|
||||
.into_iter()
|
||||
.filter_map(|strategy| match strategy {
|
||||
.filter_map(|strategy| match strategy.0 {
|
||||
Strategy::CrateMetaData => Some(GhCrateMeta::new as Resolver),
|
||||
Strategy::QuickInstall => Some(QuickInstall::new as Resolver),
|
||||
Strategy::Compile => {
|
||||
|
@ -87,6 +85,7 @@ pub fn install_crates(
|
|||
pkg_url: args.pkg_url,
|
||||
pkg_fmt: args.pkg_fmt,
|
||||
bin_dir: args.bin_dir,
|
||||
disabled_strategies: None,
|
||||
signing: None,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue