Refactor: Extract fn collect_bin_files

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

View file

@ -7,7 +7,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use cargo_toml::Package; use cargo_toml::{Package, Product};
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,41 +311,18 @@ 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 // Generate temporary binary path
let bin_path = temp_dir.path().join(format!("bin-{}", opts.name)); let bin_path = temp_dir.path().join(format!("bin-{}", opts.name));
debug!("Using temporary binary path: {}", bin_path.display()); debug!("Using temporary binary path: {}", bin_path.display());
// List files to be installed let bin_files = collect_bin_files(
// based on those found via Cargo.toml fetcher.as_ref(),
let bin_data = bins::Data { &package,
name: package.name.clone(),
target: fetcher.target().to_string(),
version: package.version.clone(),
repo: package.repository.clone(),
meta, meta,
bin_path, binaries,
bin_path.clone(),
install_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 // Prompt user for confirmation
debug!( debug!(
@ -386,7 +363,7 @@ async fn entry() -> Result<()> {
opts, opts,
package, package,
temp_dir, temp_dir,
&bin_data.bin_path, &bin_path,
&bin_files, &bin_files,
) )
.await .await
@ -407,6 +384,49 @@ async fn entry() -> Result<()> {
} }
} }
fn collect_bin_files(
fetcher: &dyn Fetcher,
package: &Package<Meta>,
mut meta: PkgMeta,
binaries: Vec<Product>,
bin_path: PathBuf,
install_path: PathBuf,
) -> Result<Vec<bins::BinFile>> {
// 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)"
));
}
// 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>>()?;
Ok(bin_files)
}
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn install_from_package( async fn install_from_package(
fetcher: &dyn Fetcher, fetcher: &dyn Fetcher,