Refactor main.rs: Extract new fn install

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-18 16:58:04 +10:00
parent 40a872dbe3
commit d514219ee4
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -2,7 +2,7 @@ use std::{
collections::BTreeSet, collections::BTreeSet,
ffi::OsString, ffi::OsString,
mem::take, mem::take,
path::PathBuf, path::{Path, PathBuf},
process::{ExitCode, Termination}, process::{ExitCode, Termination},
sync::Arc, sync::Arc,
time::{Duration, Instant}, time::{Duration, Instant},
@ -317,36 +317,7 @@ async fn entry() -> Result<()> {
let tasks: Vec<_> = resolutions let tasks: Vec<_> = resolutions
.into_iter() .into_iter()
.map(|resolution| match resolution { .map(|resolution| install(resolution, &opts, temp_dir.path(), &target))
Resolution::Fetch {
fetcher,
package,
crate_name,
version,
bin_path,
bin_files,
} => tokio::spawn(install_from_package(
fetcher,
opts.clone(),
package,
crate_name,
temp_dir.path().to_path_buf(),
version,
bin_path,
bin_files,
)),
Resolution::InstallFromSource { package } => {
if !opts.dry_run {
tokio::spawn(install_from_source(package, Arc::clone(&target)))
} else {
info!(
"Dry-run: running `cargo install {} --version {} --target {target}`",
package.name, package.version
);
tokio::spawn(async { Ok(()) })
}
}
})
.collect(); .collect();
for task in tasks { for task in tasks {
@ -528,6 +499,44 @@ fn collect_bin_files(
Ok(bin_files) Ok(bin_files)
} }
fn install(
resolution: Resolution,
opts: &Arc<Options>,
temp_dir: &Path,
target: &Arc<str>,
) -> tokio::task::JoinHandle<Result<()>> {
match resolution {
Resolution::Fetch {
fetcher,
package,
crate_name,
version,
bin_path,
bin_files,
} => tokio::spawn(install_from_package(
fetcher,
opts.clone(),
package,
crate_name,
temp_dir.to_path_buf(),
version,
bin_path,
bin_files,
)),
Resolution::InstallFromSource { package } => {
if !opts.dry_run {
tokio::spawn(install_from_source(package, Arc::clone(target)))
} else {
info!(
"Dry-run: running `cargo install {} --version {} --target {target}`",
package.name, package.version
);
tokio::spawn(async { Ok(()) })
}
}
}
}
#[allow(unused, clippy::too_many_arguments)] #[allow(unused, clippy::too_many_arguments)]
async fn install_from_package( async fn install_from_package(
fetcher: Arc<dyn Fetcher>, fetcher: Arc<dyn Fetcher>,