Optimize: Launch install immediately if confirmation is not required

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-18 17:09:29 +10:00
parent 8ca85382af
commit 90059c11cf
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -256,6 +256,17 @@ async fn entry() -> Result<()> {
})
.collect();
let desired_targets = desired_targets.get().await;
let target = Arc::from(
desired_targets
.first()
.ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?
.as_str(),
);
let temp_dir_path = Arc::from(temp_dir.path());
let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm {
let mut resolutions = Vec::with_capacity(tasks.len());
for task in tasks {
resolutions.push(await_task(task).await??);
@ -303,24 +314,34 @@ async fn entry() -> Result<()> {
}
}
if !opts.dry_run {
uithread.confirm().await?;
}
let desired_targets = desired_targets.get().await;
let target = Arc::from(
desired_targets
.first()
.ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?
.as_str(),
);
let temp_dir_path = Arc::from(temp_dir.path());
let tasks: Vec<_> = resolutions
resolutions
.into_iter()
.map(|resolution| install(resolution, &opts, &temp_dir_path, &target))
.collect();
.map(|resolution| {
tokio::spawn(install(
resolution,
Arc::clone(&opts),
Arc::clone(&temp_dir_path),
Arc::clone(&target),
))
})
.collect()
} else {
tasks
.into_iter()
.map(|task| {
let opts = Arc::clone(&opts);
let temp_dir_path = Arc::clone(&temp_dir_path);
let target = Arc::clone(&target);
tokio::spawn(async move {
let resolution = await_task(task).await??;
install(resolution, opts, temp_dir_path, target).await
})
})
.collect()
};
for task in tasks {
await_task(task).await??;
@ -501,12 +522,12 @@ fn collect_bin_files(
Ok(bin_files)
}
fn install(
async fn install(
resolution: Resolution,
opts: &Arc<Options>,
temp_dir: &Arc<Path>,
target: &Arc<str>,
) -> tokio::task::JoinHandle<Result<()>> {
opts: Arc<Options>,
temp_dir: Arc<Path>,
target: Arc<str>,
) -> Result<()> {
match resolution {
Resolution::Fetch {
fetcher,
@ -515,25 +536,21 @@ fn install(
version,
bin_path,
bin_files,
} => tokio::spawn(install_from_package(
fetcher,
opts.clone(),
package,
crate_name,
Arc::clone(temp_dir),
version,
bin_path,
bin_files,
)),
} => {
install_from_package(
fetcher, opts, package, crate_name, temp_dir, version, bin_path, bin_files,
)
.await
}
Resolution::InstallFromSource { package } => {
if !opts.dry_run {
tokio::spawn(install_from_source(package, Arc::clone(target)))
install_from_source(package, target).await
} else {
info!(
"Dry-run: running `cargo install {} --version {} --target {target}`",
package.name, package.version
);
tokio::spawn(async { Ok(()) })
Ok(())
}
}
}