Refactor: Mv confirm into entry and remove unncessary confirm

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-11 21:49:28 +10:00
parent 114c199e98
commit 7a85cae859
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,13 +1,13 @@
use std::{ use std::{
collections::BTreeSet, collections::BTreeSet,
ffi::OsString, ffi::OsString,
path::PathBuf, path::{Path, PathBuf},
process::{ExitCode, Termination}, process::{ExitCode, Termination},
str::FromStr, str::FromStr,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use cargo_toml::{Package, Product}; use cargo_toml::Package;
use clap::Parser; use clap::Parser;
use log::{debug, error, info, warn, LevelFilter}; use log::{debug, error, info, warn, LevelFilter};
use miette::{miette, IntoDiagnostic, Result, WrapErr}; use miette::{miette, IntoDiagnostic, Result, WrapErr};
@ -311,20 +311,83 @@ async fn entry() -> Result<()> {
} }
meta.merge(&cli_overrides); 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::<Result<Vec<_>, BinstallError>>()?;
// Prompt user for confirmation
debug!( debug!(
"Found a binary install source: {} ({fetcher_target})", "Found a binary install source: {} ({fetcher_target})",
fetcher.source_name() 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( install_from_package(
binaries,
fetcher.as_ref(), fetcher.as_ref(),
install_path,
meta,
opts, opts,
package, package,
temp_dir, temp_dir,
&mut uithread, &bin_data.bin_path,
&bin_files,
) )
.await .await
} }
@ -346,51 +409,18 @@ async fn entry() -> Result<()> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn install_from_package( async fn install_from_package(
binaries: Vec<Product>,
fetcher: &dyn Fetcher, fetcher: &dyn Fetcher,
install_path: PathBuf,
mut meta: PkgMeta,
opts: Options, opts: Options,
package: Package<Meta>, package: Package<Meta>,
temp_dir: TempDir, temp_dir: TempDir,
uithread: &mut UIThread, bin_path: &Path,
bin_files: &[bins::BinFile],
) -> Result<()> { ) -> 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 // Download package
if opts.dry_run { if opts.dry_run {
info!("Dry run, not downloading package"); info!("Dry run, not downloading package");
} else { } else {
fetcher.fetch_and_extract(&bin_path).await?; 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)"
));
}
} }
#[cfg(incomplete)] #[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::<Result<Vec<_>, 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 { if opts.dry_run {
info!("Dry run, not proceeding"); info!("Dry run, not proceeding");
return Ok(()); return Ok(());
} }
uithread.confirm().await?;
let cvs = metafiles::CrateVersionSource { let cvs = metafiles::CrateVersionSource {
name: opts.name, name: opts.name,
version: package.version.parse().into_diagnostic()?, version: package.version.parse().into_diagnostic()?,
@ -464,13 +462,13 @@ async fn install_from_package(
info!("Installing binaries..."); info!("Installing binaries...");
block_in_place(|| { block_in_place(|| {
for file in &bin_files { for file in bin_files {
file.install_bin()?; file.install_bin()?;
} }
// Generate symlinks // Generate symlinks
if !opts.no_symlinks { if !opts.no_symlinks {
for file in &bin_files { for file in bin_files {
file.install_link()?; file.install_link()?;
} }
} }