mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +00:00
Refactor cargo-binstall
(#1302)
- Move implementation of `main` into the library part of `cargo-binstall` to speedup codegen. - Move `manifests.rs` into `binstalk-manifests` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
4e73d0095f
commit
43973d7e86
8 changed files with 120 additions and 103 deletions
|
@ -28,7 +28,6 @@ clap = { version = "4.3.0", features = ["derive", "env"] }
|
|||
compact_str = "0.7.0"
|
||||
dirs = "5.0.1"
|
||||
file-format = { version = "0.18.0", default-features = false }
|
||||
fs-lock = { version = "0.1.0", path = "../fs-lock" }
|
||||
gh-token = "0.1.2"
|
||||
home = "0.5.5"
|
||||
log = { version = "0.4.18", features = ["std"] }
|
||||
|
|
|
@ -21,8 +21,9 @@ use binstalk::{
|
|||
CargoTomlFetchOverride, Options, Resolver,
|
||||
},
|
||||
};
|
||||
use binstalk_manifests::cargo_config::Config;
|
||||
use binstalk_manifests::cargo_toml_binstall::PkgOverride;
|
||||
use binstalk_manifests::{
|
||||
cargo_config::Config, cargo_toml_binstall::PkgOverride, crates_manifests::Manifests,
|
||||
};
|
||||
use file_format::FileFormat;
|
||||
use home::cargo_home;
|
||||
use log::LevelFilter;
|
||||
|
@ -33,7 +34,6 @@ use tracing::{debug, error, info, warn};
|
|||
use crate::{
|
||||
args::{Args, Strategy},
|
||||
install_path,
|
||||
manifests::Manifests,
|
||||
ui::confirm,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
|
||||
pub mod args;
|
||||
pub mod bin_util;
|
||||
pub mod entry;
|
||||
mod args;
|
||||
mod bin_util;
|
||||
mod entry;
|
||||
mod git_credentials;
|
||||
mod install_path;
|
||||
pub mod logging;
|
||||
mod manifests;
|
||||
mod logging;
|
||||
mod main_impl;
|
||||
mod signal;
|
||||
mod ui;
|
||||
|
||||
pub use main_impl::do_main;
|
||||
|
|
|
@ -1,68 +1,11 @@
|
|||
use std::time::Instant;
|
||||
use std::process::Termination;
|
||||
|
||||
use binstalk::{helpers::jobserver_client::LazyJobserverClient, TARGET};
|
||||
use log::LevelFilter;
|
||||
use tracing::debug;
|
||||
|
||||
use cargo_binstall::{
|
||||
args,
|
||||
bin_util::{run_tokio_main, MainExit},
|
||||
entry,
|
||||
logging::logging,
|
||||
};
|
||||
use cargo_binstall::do_main;
|
||||
|
||||
#[cfg(feature = "mimalloc")]
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
fn main() -> MainExit {
|
||||
// This must be the very first thing to happen
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let args = args::parse();
|
||||
|
||||
if args.version {
|
||||
let cargo_binstall_version = env!("CARGO_PKG_VERSION");
|
||||
if args.verbose {
|
||||
let build_date = env!("VERGEN_BUILD_DATE");
|
||||
|
||||
let features = env!("VERGEN_CARGO_FEATURES");
|
||||
|
||||
let git_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN");
|
||||
let git_commit_date = option_env!("VERGEN_GIT_COMMIT_DATE").unwrap_or("UNKNOWN");
|
||||
|
||||
let rustc_semver = env!("VERGEN_RUSTC_SEMVER");
|
||||
let rustc_commit_hash = env!("VERGEN_RUSTC_COMMIT_HASH");
|
||||
let rustc_llvm_version = env!("VERGEN_RUSTC_LLVM_VERSION");
|
||||
|
||||
println!(
|
||||
r#"cargo-binstall: {cargo_binstall_version}
|
||||
build-date: {build_date}
|
||||
build-target: {TARGET}
|
||||
build-features: {features}
|
||||
build-commit-hash: {git_sha}
|
||||
build-commit-date: {git_commit_date}
|
||||
rustc-version: {rustc_semver}
|
||||
rustc-commit-hash: {rustc_commit_hash}
|
||||
rustc-llvm-version: {rustc_llvm_version}"#
|
||||
);
|
||||
} else {
|
||||
println!("{cargo_binstall_version}");
|
||||
}
|
||||
MainExit::Success(None)
|
||||
} else {
|
||||
logging(
|
||||
args.log_level.unwrap_or(LevelFilter::Info),
|
||||
args.json_output,
|
||||
);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let result = run_tokio_main(|| entry::install_crates(args, jobserver_client));
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
MainExit::new(result, done)
|
||||
}
|
||||
fn main() -> impl Termination {
|
||||
do_main()
|
||||
}
|
||||
|
|
64
crates/bin/src/main_impl.rs
Normal file
64
crates/bin/src/main_impl.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use std::{process::Termination, time::Instant};
|
||||
|
||||
use binstalk::{helpers::jobserver_client::LazyJobserverClient, TARGET};
|
||||
use log::LevelFilter;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::{
|
||||
args,
|
||||
bin_util::{run_tokio_main, MainExit},
|
||||
entry,
|
||||
logging::logging,
|
||||
};
|
||||
|
||||
pub fn do_main() -> impl Termination {
|
||||
// This must be the very first thing to happen
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let args = args::parse();
|
||||
|
||||
if args.version {
|
||||
let cargo_binstall_version = env!("CARGO_PKG_VERSION");
|
||||
if args.verbose {
|
||||
let build_date = env!("VERGEN_BUILD_DATE");
|
||||
|
||||
let features = env!("VERGEN_CARGO_FEATURES");
|
||||
|
||||
let git_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN");
|
||||
let git_commit_date = option_env!("VERGEN_GIT_COMMIT_DATE").unwrap_or("UNKNOWN");
|
||||
|
||||
let rustc_semver = env!("VERGEN_RUSTC_SEMVER");
|
||||
let rustc_commit_hash = env!("VERGEN_RUSTC_COMMIT_HASH");
|
||||
let rustc_llvm_version = env!("VERGEN_RUSTC_LLVM_VERSION");
|
||||
|
||||
println!(
|
||||
r#"cargo-binstall: {cargo_binstall_version}
|
||||
build-date: {build_date}
|
||||
build-target: {TARGET}
|
||||
build-features: {features}
|
||||
build-commit-hash: {git_sha}
|
||||
build-commit-date: {git_commit_date}
|
||||
rustc-version: {rustc_semver}
|
||||
rustc-commit-hash: {rustc_commit_hash}
|
||||
rustc-llvm-version: {rustc_llvm_version}"#
|
||||
);
|
||||
} else {
|
||||
println!("{cargo_binstall_version}");
|
||||
}
|
||||
MainExit::Success(None)
|
||||
} else {
|
||||
logging(
|
||||
args.log_level.unwrap_or(LevelFilter::Info),
|
||||
args.json_output,
|
||||
);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let result = run_tokio_main(|| entry::install_crates(args, jobserver_client));
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
MainExit::new(result, done)
|
||||
}
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
use std::{collections::BTreeMap, fs, io::Seek, path::Path};
|
||||
|
||||
use binstalk::errors::BinstallError;
|
||||
use binstalk_manifests::{
|
||||
binstall_crates_v1::Records as BinstallCratesV1Records, cargo_crates_v1::CratesToml,
|
||||
crate_info::CrateInfo, CompactString, Version,
|
||||
};
|
||||
use fs_lock::FileLock;
|
||||
use miette::{Error, Result};
|
||||
use tracing::debug;
|
||||
|
||||
pub struct Manifests {
|
||||
binstall: BinstallCratesV1Records,
|
||||
cargo_crates_v1: FileLock,
|
||||
}
|
||||
|
||||
impl Manifests {
|
||||
pub fn open_exclusive(cargo_roots: &Path) -> Result<Self> {
|
||||
// Read cargo_binstall_metadata
|
||||
let metadata_path = cargo_roots.join("binstall/crates-v1.json");
|
||||
fs::create_dir_all(metadata_path.parent().unwrap()).map_err(BinstallError::Io)?;
|
||||
|
||||
debug!(
|
||||
"Reading binstall metadata from {} and obtaining exclusive lock",
|
||||
metadata_path.display()
|
||||
);
|
||||
|
||||
let binstall = BinstallCratesV1Records::load_from_path(&metadata_path)?;
|
||||
|
||||
// Read cargo_install_v1_metadata
|
||||
let manifest_path = cargo_roots.join(".crates.toml");
|
||||
|
||||
debug!(
|
||||
"Obtaining exclusive lock of cargo install v1 metadata in path {}",
|
||||
manifest_path.display()
|
||||
);
|
||||
|
||||
let cargo_crates_v1 = fs::File::options()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(manifest_path)
|
||||
.and_then(FileLock::new_exclusive)
|
||||
.map_err(BinstallError::Io)?;
|
||||
|
||||
Ok(Self {
|
||||
binstall,
|
||||
cargo_crates_v1,
|
||||
})
|
||||
}
|
||||
|
||||
fn rewind_cargo_crates_v1(&mut self) -> Result<()> {
|
||||
self.cargo_crates_v1
|
||||
.rewind()
|
||||
.map_err(BinstallError::Io)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// `cargo-uninstall` can be called to uninstall crates,
|
||||
/// but it only updates .crates.toml.
|
||||
///
|
||||
/// So here we will honour .crates.toml only.
|
||||
pub fn load_installed_crates(&mut self) -> Result<BTreeMap<CompactString, Version>> {
|
||||
self.rewind_cargo_crates_v1()?;
|
||||
|
||||
CratesToml::load_from_reader(&mut self.cargo_crates_v1)
|
||||
.and_then(CratesToml::collect_into_crates_versions)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn update(mut self, metadata_vec: Vec<CrateInfo>) -> Result<()> {
|
||||
self.rewind_cargo_crates_v1()?;
|
||||
|
||||
debug!("Writing .crates.toml");
|
||||
CratesToml::append_to_file(&mut self.cargo_crates_v1, &metadata_vec)?;
|
||||
|
||||
debug!("Writing binstall/crates-v1.json");
|
||||
for metadata in metadata_vec {
|
||||
self.binstall.replace(metadata);
|
||||
}
|
||||
self.binstall.overwrite()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue