From 7a85cae859c9d935abf34ddea14c023cea557598 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Mon, 11 Jul 2022 21:49:28 +1000 Subject: [PATCH] Refactor: Mv confirm into `entry` and remove unncessary confirm Signed-off-by: Jiahao XU --- src/main.rs | 150 ++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 76 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2da123a4..645fcfb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ use std::{ collections::BTreeSet, ffi::OsString, - path::PathBuf, + path::{Path, PathBuf}, process::{ExitCode, Termination}, str::FromStr, time::{Duration, Instant}, }; -use cargo_toml::{Package, Product}; +use cargo_toml::Package; use clap::Parser; use log::{debug, error, info, warn, LevelFilter}; use miette::{miette, IntoDiagnostic, Result, WrapErr}; @@ -311,20 +311,83 @@ async fn entry() -> Result<()> { } meta.merge(&cli_overrides); + // Update meta + if fetcher.source_name() == "QuickInstall" { + // TODO: less of a hack? + meta.bin_dir = "{ bin }{ binary-ext }".to_string(); + } + + // Check binaries + if binaries.is_empty() { + error!("No binaries specified (or inferred from file system)"); + return Err(miette!( + "No binaries specified (or inferred from file system)" + )); + } + + // Generate temporary binary path + let bin_path = temp_dir.path().join(format!("bin-{}", opts.name)); + debug!("Using temporary binary path: {}", bin_path.display()); + + // List files to be installed + // based on those found via Cargo.toml + let bin_data = bins::Data { + name: package.name.clone(), + target: fetcher.target().to_string(), + version: package.version.clone(), + repo: package.repository.clone(), + meta, + bin_path, + install_path, + }; + + // Create bin_files + let bin_files = binaries + .iter() + .map(|p| bins::BinFile::from_product(&bin_data, p)) + .collect::, BinstallError>>()?; + + // Prompt user for confirmation debug!( "Found a binary install source: {} ({fetcher_target})", fetcher.source_name() ); + if fetcher.is_third_party() { + warn!( + "The package will be downloaded from third-party source {}", + fetcher.source_name() + ); + } else { + info!( + "The package will be downloaded from {}", + fetcher.source_name() + ); + } + + info!("This will install the following binaries:"); + for file in &bin_files { + info!(" - {}", file.preview_bin()); + } + + if !opts.no_symlinks { + info!("And create (or update) the following symlinks:"); + for file in &bin_files { + info!(" - {}", file.preview_link()); + } + } + + if !opts.dry_run { + uithread.confirm().await?; + } + install_from_package( - binaries, fetcher.as_ref(), - install_path, - meta, opts, package, temp_dir, - &mut uithread, + &bin_data.bin_path, + &bin_files, ) .await } @@ -346,51 +409,18 @@ async fn entry() -> Result<()> { #[allow(clippy::too_many_arguments)] async fn install_from_package( - binaries: Vec, fetcher: &dyn Fetcher, - install_path: PathBuf, - mut meta: PkgMeta, opts: Options, package: Package, temp_dir: TempDir, - uithread: &mut UIThread, + bin_path: &Path, + bin_files: &[bins::BinFile], ) -> Result<()> { - // Prompt user for third-party source - if fetcher.is_third_party() { - warn!( - "The package will be downloaded from third-party source {}", - fetcher.source_name() - ); - if !opts.dry_run { - uithread.confirm().await?; - } - } else { - info!( - "The package will be downloaded from {}", - fetcher.source_name() - ); - } - - if fetcher.source_name() == "QuickInstall" { - // TODO: less of a hack? - meta.bin_dir = "{ bin }{ binary-ext }".to_string(); - } - - let bin_path = temp_dir.path().join(format!("bin-{}", opts.name)); - debug!("Using temporary binary path: {}", bin_path.display()); - // Download package if opts.dry_run { info!("Dry run, not downloading package"); } else { - fetcher.fetch_and_extract(&bin_path).await?; - - if binaries.is_empty() { - error!("No binaries specified (or inferred from file system)"); - return Err(miette!( - "No binaries specified (or inferred from file system)" - )); - } + fetcher.fetch_and_extract(bin_path).await?; } #[cfg(incomplete)] @@ -417,43 +447,11 @@ async fn install_from_package( } } - // List files to be installed - // based on those found via Cargo.toml - let bin_data = bins::Data { - name: package.name.clone(), - target: fetcher.target().to_string(), - version: package.version.clone(), - repo: package.repository.clone(), - meta, - bin_path, - install_path, - }; - - let bin_files = binaries - .iter() - .map(|p| bins::BinFile::from_product(&bin_data, p)) - .collect::, BinstallError>>()?; - - // Prompt user for confirmation - info!("This will install the following binaries:"); - for file in &bin_files { - info!(" - {}", file.preview_bin()); - } - - if !opts.no_symlinks { - info!("And create (or update) the following symlinks:"); - for file in &bin_files { - info!(" - {}", file.preview_link()); - } - } - if opts.dry_run { info!("Dry run, not proceeding"); return Ok(()); } - uithread.confirm().await?; - let cvs = metafiles::CrateVersionSource { name: opts.name, version: package.version.parse().into_diagnostic()?, @@ -464,13 +462,13 @@ async fn install_from_package( info!("Installing binaries..."); block_in_place(|| { - for file in &bin_files { + for file in bin_files { file.install_bin()?; } // Generate symlinks if !opts.no_symlinks { - for file in &bin_files { + for file in bin_files { file.install_link()?; } }