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