mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 14:28:42 +00:00
feature: faster self-install without rate limit issue (#1963)
* Add new hidden option `--self-install` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix typo Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Optimize: Only call `LazyJobserverClient::new` when necessary Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * `--self-install` should include a path to a binary On windows, we cannot just copy an executable with process running from it, so better to copy it to a temporary location. Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Impl manifest update and basic API of `self_install` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Add dep atomic-file-install to cargo-binstall Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Impl `self_install` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Make `--self-install` a boolean flag Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Accept no duration in `MainExit::new` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Impl self-install mode in main_impl.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Use `--self-install` mode in install-from-binstall-release.sh Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Use `--self-install` in install-from-binstall-release.ps1 Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix import in mod entry Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix call of `self_install` in `main_impl` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `--self-install` clap doc Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix `entry::self_install` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Apply suggestions from code review Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix parsing semver number in entry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix compilation in entry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * fix entry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt in bin_util.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix fmt in entry.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Use --self-install if supported in unix install script Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Use --self-install if available in powershell install script Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix install-from-binstall-release.ps1 Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fi install-from-binstall-release.ps1 Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix install-from-binstall-release.ps1 Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Create self-install.sh Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Add e2e-test-self-install to justfile Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix args::parse() for self-install mode Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix args parsing: Do no require positional arg if --self-install is present Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --------- Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
parent
b1137ece8c
commit
c9d5e6de40
9 changed files with 89 additions and 15 deletions
|
@ -22,6 +22,7 @@ pkg-fmt = "zip"
|
|||
pkg-fmt = "zip"
|
||||
|
||||
[dependencies]
|
||||
atomic-file-install = { version = "1.0.5", path = "../atomic-file-install" }
|
||||
binstalk = { path = "../binstalk", version = "0.28.11", default-features = false }
|
||||
binstalk-manifests = { path = "../binstalk-manifests", version = "0.15.8" }
|
||||
clap = { version = "4.5.3", features = ["derive", "env"] }
|
||||
|
|
|
@ -52,7 +52,7 @@ pub struct Args {
|
|||
#[clap(
|
||||
help_heading = "Package selection",
|
||||
value_name = "crate[@version]",
|
||||
required_unless_present_any = ["version", "help"],
|
||||
required_unless_present_any = ["version", "self_install", "help"],
|
||||
)]
|
||||
pub(crate) crate_names: Vec<CrateName>,
|
||||
|
||||
|
@ -404,6 +404,9 @@ pub struct Args {
|
|||
/// This would override the `log_level`.
|
||||
#[clap(help_heading = "Meta", short, long, conflicts_with("verbose"))]
|
||||
pub(crate) quiet: bool,
|
||||
|
||||
#[clap(long, hide(true))]
|
||||
pub(crate) self_install: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -515,6 +518,10 @@ pub fn parse() -> (Args, PkgOverride) {
|
|||
// Load options
|
||||
let mut opts = Args::parse_from(args);
|
||||
|
||||
if opts.self_install {
|
||||
return (opts, Default::default());
|
||||
}
|
||||
|
||||
if opts.log_level.is_none() {
|
||||
if let Some(log) = env::var("BINSTALL_LOG_LEVEL")
|
||||
.ok()
|
||||
|
|
|
@ -36,13 +36,12 @@ impl Termination for MainExit {
|
|||
}
|
||||
|
||||
impl MainExit {
|
||||
pub fn new(res: Result<()>, done: Duration) -> Self {
|
||||
res.map(|()| MainExit::Success(Some(done)))
|
||||
.unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
pub fn new(res: Result<()>, done: Option<Duration>) -> Self {
|
||||
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
use atomic_file_install::atomic_install;
|
||||
use binstalk::{
|
||||
errors::{BinstallError, CrateContextError},
|
||||
fetchers::{Fetcher, GhCrateMeta, QuickInstall, SignaturePolicy},
|
||||
|
@ -20,16 +21,20 @@ use binstalk::{
|
|||
resolve::{CrateName, Resolution, ResolutionFetch, VersionReqExt},
|
||||
CargoTomlFetchOverride, Options, Resolver,
|
||||
},
|
||||
TARGET,
|
||||
};
|
||||
use binstalk_manifests::{
|
||||
cargo_config::Config,
|
||||
cargo_toml_binstall::{PkgOverride, Strategy},
|
||||
crate_info::{CrateInfo, CrateSource},
|
||||
crates_manifests::Manifests,
|
||||
};
|
||||
use compact_str::CompactString;
|
||||
use file_format::FileFormat;
|
||||
use home::cargo_home;
|
||||
use log::LevelFilter;
|
||||
use miette::{miette, Report, Result, WrapErr};
|
||||
use semver::Version;
|
||||
use tokio::task::block_in_place;
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
|
@ -582,3 +587,44 @@ fn do_install_fetches_continue_on_failure(
|
|||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn self_install(args: Args) -> Result<()> {
|
||||
// Load .cargo/config.toml
|
||||
let cargo_home = cargo_home().map_err(BinstallError::from)?;
|
||||
let mut config = Config::load_from_path(cargo_home.join("config.toml"))?;
|
||||
|
||||
// Compute paths
|
||||
let cargo_root = args.root;
|
||||
let (install_path, manifests, _) = compute_paths_and_load_manifests(
|
||||
cargo_root.clone(),
|
||||
args.install_path,
|
||||
args.no_track,
|
||||
cargo_home,
|
||||
&mut config,
|
||||
)?;
|
||||
|
||||
let mut dest = install_path.join("cargo-binstall");
|
||||
if cfg!(windows) {
|
||||
assert!(dest.set_extension("exe"));
|
||||
}
|
||||
|
||||
atomic_install(&env::current_exe().map_err(BinstallError::from)?, &dest)
|
||||
.map_err(BinstallError::from)?;
|
||||
|
||||
if let Some(manifests) = manifests {
|
||||
manifests.update(vec![CrateInfo {
|
||||
name: CompactString::const_new("cargo-binstall"),
|
||||
version_req: CompactString::const_new("*"),
|
||||
current_version: Version::new(
|
||||
env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
|
||||
env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
|
||||
env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
|
||||
),
|
||||
source: CrateSource::cratesio_registry(),
|
||||
target: CompactString::const_new(TARGET),
|
||||
bins: vec![CompactString::const_new("cargo-binstall")],
|
||||
}])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -12,9 +12,6 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn do_main() -> impl Termination {
|
||||
// This must be the very first thing to happen
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let (args, cli_overrides) = args::parse();
|
||||
|
||||
if args.version {
|
||||
|
@ -46,6 +43,8 @@ rustc-llvm-version: {rustc_llvm_version}"#
|
|||
println!("{cargo_binstall_version}");
|
||||
}
|
||||
MainExit::Success(None)
|
||||
} else if args.self_install {
|
||||
MainExit::new(entry::self_install(args), None)
|
||||
} else {
|
||||
logging(
|
||||
args.log_level.unwrap_or(LevelFilter::Info),
|
||||
|
@ -54,12 +53,14 @@ rustc-llvm-version: {rustc_llvm_version}"#
|
|||
|
||||
let start = Instant::now();
|
||||
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let result =
|
||||
run_tokio_main(|| entry::install_crates(args, cli_overrides, jobserver_client));
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
MainExit::new(result, done)
|
||||
MainExit::new(result, Some(done))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue