mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +00:00
Optimize compilation time (#320)
* Print `rerun-if-changed=build.rs` in `build.rs` * Optimize compile-time: Extract `bin/src/lib.rs` so that `cargo-binstall (lib)` can be compiled in parallel to other deps. * Refactor: Extract new mod `bin/src/bin_utils.rs` * Extract new fn `MainExit::new` * Refactor: Extract new fn `run_tokio_main` * Handle `Runtime::new` err gracefully in `run_tokio_main` instead of `panic!`ing, return the error as `BinstallError` * Avoid mixing `eprintln` and `error` in `MainExit::report` * Set profile for `build-override` to speedup building of `build.rs` and proc macros. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
e39a02aa92
commit
305cda3336
6 changed files with 95 additions and 53 deletions
|
@ -1,3 +1,5 @@
|
|||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
embed_resource::compile("manifest.rc");
|
||||
}
|
||||
|
|
60
crates/bin/src/bin_util.rs
Normal file
60
crates/bin/src/bin_util.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use std::{
|
||||
future::Future,
|
||||
process::{ExitCode, Termination},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use binstall::errors::BinstallError;
|
||||
use binstall::helpers::{signal::cancel_on_user_sig_term, tasks::AutoAbortJoinHandle};
|
||||
use log::{error, info};
|
||||
use miette::Result;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
pub enum MainExit {
|
||||
Success(Duration),
|
||||
Error(BinstallError),
|
||||
Report(miette::Report),
|
||||
}
|
||||
|
||||
impl Termination for MainExit {
|
||||
fn report(self) -> ExitCode {
|
||||
match self {
|
||||
Self::Success(spent) => {
|
||||
info!("Done in {spent:?}");
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Self::Error(err) => err.report(),
|
||||
Self::Report(err) => {
|
||||
error!("Fatal error:\n{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MainExit {
|
||||
pub fn new(result: Result<Result<()>, BinstallError>, done: Duration) -> Self {
|
||||
result.map_or_else(MainExit::Error, |res| {
|
||||
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// This function would start a tokio multithreading runtime,
|
||||
/// spawn a new task on it that runs `f`, then `block_on` it.
|
||||
///
|
||||
/// It will cancel the future if user requested cancellation
|
||||
/// via signal.
|
||||
pub fn run_tokio_main<F, T>(f: F) -> Result<T, BinstallError>
|
||||
where
|
||||
F: Future<Output = T> + Send + 'static,
|
||||
T: Send + 'static,
|
||||
{
|
||||
let rt = Runtime::new()?;
|
||||
let handle = AutoAbortJoinHandle::new(rt.spawn(f));
|
||||
rt.block_on(cancel_on_user_sig_term(handle))
|
||||
}
|
5
crates/bin/src/lib.rs
Normal file
5
crates/bin/src/lib.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
pub mod args;
|
||||
pub mod bin_util;
|
||||
pub mod entry;
|
||||
pub mod install_path;
|
||||
pub mod ui;
|
|
@ -1,22 +1,13 @@
|
|||
use std::{
|
||||
process::{ExitCode, Termination},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::time::Instant;
|
||||
|
||||
use binstall::{
|
||||
errors::BinstallError,
|
||||
helpers::{
|
||||
jobserver_client::LazyJobserverClient, signal::cancel_on_user_sig_term,
|
||||
tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
};
|
||||
use log::{debug, error, info};
|
||||
use tokio::runtime::Runtime;
|
||||
use binstall::helpers::jobserver_client::LazyJobserverClient;
|
||||
use log::debug;
|
||||
|
||||
mod args;
|
||||
mod entry;
|
||||
mod install_path;
|
||||
mod ui;
|
||||
use cargo_binstall::{
|
||||
args,
|
||||
bin_util::{run_tokio_main, MainExit},
|
||||
entry, ui,
|
||||
};
|
||||
|
||||
#[cfg(feature = "mimalloc")]
|
||||
#[global_allocator]
|
||||
|
@ -35,44 +26,10 @@ fn main() -> MainExit {
|
|||
|
||||
let start = Instant::now();
|
||||
|
||||
let result = {
|
||||
let rt = Runtime::new().unwrap();
|
||||
let handle =
|
||||
AutoAbortJoinHandle::new(rt.spawn(entry::install_crates(args, jobserver_client)));
|
||||
rt.block_on(cancel_on_user_sig_term(handle))
|
||||
};
|
||||
let result = run_tokio_main(entry::install_crates(args, jobserver_client));
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
result.map_or_else(MainExit::Error, |res| {
|
||||
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
enum MainExit {
|
||||
Success(Duration),
|
||||
Error(BinstallError),
|
||||
Report(miette::Report),
|
||||
}
|
||||
|
||||
impl Termination for MainExit {
|
||||
fn report(self) -> ExitCode {
|
||||
match self {
|
||||
Self::Success(spent) => {
|
||||
info!("Done in {spent:?}");
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Self::Error(err) => err.report(),
|
||||
Self::Report(err) => {
|
||||
error!("Fatal error:");
|
||||
eprintln!("{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
MainExit::new(result, done)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue