mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-21 01:56:37 +00:00
Refactor: Move Strategy
to binstalk-types
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
eba07fb147
commit
832a6c13ae
3 changed files with 65 additions and 26 deletions
crates
|
@ -13,12 +13,12 @@ use binstalk::{
|
||||||
ops::resolve::{CrateName, VersionReqExt},
|
ops::resolve::{CrateName, VersionReqExt},
|
||||||
registry::Registry,
|
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 compact_str::CompactString;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use semver::VersionReq;
|
use semver::VersionReq;
|
||||||
use strum::EnumCount;
|
use strum::EnumCount;
|
||||||
use strum_macros::EnumCount;
|
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
|
@ -162,13 +162,13 @@ pub struct Args {
|
||||||
value_delimiter(','),
|
value_delimiter(','),
|
||||||
env = "BINSTALL_STRATEGIES"
|
env = "BINSTALL_STRATEGIES"
|
||||||
)]
|
)]
|
||||||
pub(crate) strategies: Vec<Strategy>,
|
pub(crate) strategies: Vec<StrategyWrapped>,
|
||||||
|
|
||||||
/// Disable the strategies specified.
|
/// Disable the strategies specified.
|
||||||
/// If a strategy is specified in `--strategies` and `--disable-strategies`,
|
/// If a strategy is specified in `--strategies` and `--disable-strategies`,
|
||||||
/// then it will be removed.
|
/// then it will be removed.
|
||||||
#[clap(help_heading = "Overrides", long, value_delimiter(','))]
|
#[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`
|
/// If `--github-token` or environment variable `GITHUB_TOKEN`/`GH_TOKEN`
|
||||||
/// is not specified, then cargo-binstall will try to extract github token from
|
/// is not specified, then cargo-binstall will try to extract github token from
|
||||||
|
@ -431,16 +431,20 @@ impl Default for RateLimit {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Strategy for installing the package
|
/// Strategy for installing the package
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum, EnumCount)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
#[repr(u8)]
|
pub(crate) struct StrategyWrapped(pub(crate) Strategy);
|
||||||
pub(crate) enum Strategy {
|
|
||||||
/// Attempt to download official pre-built artifacts using
|
impl ValueEnum for StrategyWrapped {
|
||||||
/// information provided in `Cargo.toml`.
|
fn value_variants<'a>() -> &'a [Self] {
|
||||||
CrateMetaData,
|
&[
|
||||||
/// Query third-party QuickInstall for the crates.
|
Self(Strategy::CrateMetaData),
|
||||||
QuickInstall,
|
Self(Strategy::QuickInstall),
|
||||||
/// Build the crates from source using `cargo-build`.
|
Self(Strategy::Compile),
|
||||||
Compile,
|
]
|
||||||
|
}
|
||||||
|
fn to_possible_value(&self) -> Option<PossibleValue> {
|
||||||
|
Some(PossibleValue::new(self.0.to_str()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse() -> Args {
|
pub fn parse() -> Args {
|
||||||
|
@ -532,7 +536,7 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
||||||
let mut is_variant_present = [false; Strategy::COUNT];
|
let mut is_variant_present = [false; Strategy::COUNT];
|
||||||
|
|
||||||
for strategy in &opts.strategies {
|
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] {
|
if is_variant_present[index] {
|
||||||
new_dup_strategy_err().exit()
|
new_dup_strategy_err().exit()
|
||||||
} else {
|
} else {
|
||||||
|
@ -543,9 +547,9 @@ You cannot use --{option} and specify multiple packages at the same time. Do one
|
||||||
// Default strategies if empty
|
// Default strategies if empty
|
||||||
if opts.strategies.is_empty() {
|
if opts.strategies.is_empty() {
|
||||||
opts.strategies = vec![
|
opts.strategies = vec![
|
||||||
Strategy::CrateMetaData,
|
StrategyWrapped(Strategy::CrateMetaData),
|
||||||
Strategy::QuickInstall,
|
StrategyWrapped(Strategy::QuickInstall),
|
||||||
Strategy::Compile,
|
StrategyWrapped(Strategy::Compile),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,7 +577,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
|
// 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
|
command
|
||||||
.error(
|
.error(
|
||||||
ErrorKind::InvalidValue,
|
ErrorKind::InvalidValue,
|
||||||
|
|
|
@ -21,7 +21,9 @@ use binstalk::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use binstalk_manifests::{
|
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 file_format::FileFormat;
|
||||||
use home::cargo_home;
|
use home::cargo_home;
|
||||||
|
@ -30,11 +32,7 @@ use miette::{miette, Report, Result, WrapErr};
|
||||||
use tokio::task::block_in_place;
|
use tokio::task::block_in_place;
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{args::Args, gh_token, git_credentials, install_path, ui::confirm};
|
||||||
args::{Args, Strategy},
|
|
||||||
gh_token, git_credentials, install_path,
|
|
||||||
ui::confirm,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn install_crates(
|
pub fn install_crates(
|
||||||
args: Args,
|
args: Args,
|
||||||
|
@ -46,7 +44,7 @@ pub fn install_crates(
|
||||||
let resolvers: Vec<_> = args
|
let resolvers: Vec<_> = args
|
||||||
.strategies
|
.strategies
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|strategy| match strategy {
|
.filter_map(|strategy| match strategy.0 {
|
||||||
Strategy::CrateMetaData => Some(GhCrateMeta::new as Resolver),
|
Strategy::CrateMetaData => Some(GhCrateMeta::new as Resolver),
|
||||||
Strategy::QuickInstall => Some(QuickInstall::new as Resolver),
|
Strategy::QuickInstall => Some(QuickInstall::new as Resolver),
|
||||||
Strategy::Compile => {
|
Strategy::Compile => {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
use std::{borrow::Cow, collections::BTreeMap};
|
use std::{borrow::Cow, collections::BTreeMap};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use strum_macros::{EnumCount, VariantArray};
|
||||||
|
|
||||||
mod package_formats;
|
mod package_formats;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
|
@ -19,6 +20,41 @@ pub struct Meta {
|
||||||
pub binstall: Option<PkgMeta>,
|
pub binstall: Option<PkgMeta>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Strategies to use for binary discovery
|
||||||
|
#[derive(
|
||||||
|
Debug,
|
||||||
|
Copy,
|
||||||
|
Clone,
|
||||||
|
Eq,
|
||||||
|
PartialEq,
|
||||||
|
Ord,
|
||||||
|
PartialOrd,
|
||||||
|
EnumCount,
|
||||||
|
VariantArray,
|
||||||
|
Deserialize,
|
||||||
|
Serialize,
|
||||||
|
)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Strategy {
|
||||||
|
pub const fn to_str(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Strategy::CrateMetaData => "crate-meta-data",
|
||||||
|
Strategy::QuickInstall => "quick-install",
|
||||||
|
Strategy::Compile => "compile",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Metadata for binary installation use.
|
/// Metadata for binary installation use.
|
||||||
///
|
///
|
||||||
/// Exposed via `[package.metadata]` in `Cargo.toml`
|
/// Exposed via `[package.metadata]` in `Cargo.toml`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue