103 lines
3.0 KiB
Rust
103 lines
3.0 KiB
Rust
use crate::common::{SummaryItem, format_size, print_summary, yesno};
|
|
use clap::Parser;
|
|
use console::style;
|
|
use indicatif::ProgressBar;
|
|
use packie::{
|
|
PackieBuilder, graph::CalculateInstallationOptions, install::InstallOptions, package::PkgSpec,
|
|
};
|
|
use rust_i18n::t;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Parser)]
|
|
pub struct Cli {
|
|
/// Force the specified items to be files
|
|
#[arg(short, long)]
|
|
file: bool,
|
|
|
|
/// Don't require manual confirmation
|
|
#[arg(short, long)]
|
|
yes: bool,
|
|
|
|
/// Items to be installed
|
|
items: Vec<String>,
|
|
}
|
|
|
|
pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
|
let mut packie = PackieBuilder::new().build()?;
|
|
if cli.file {
|
|
for item in cli.items {
|
|
packie.install_package_file(item, &InstallOptions::default())?;
|
|
}
|
|
return Ok(());
|
|
}
|
|
|
|
let mut items = Vec::with_capacity(cli.items.len());
|
|
for i in cli.items {
|
|
items.push(i.parse::<PkgSpec>()?);
|
|
}
|
|
|
|
let mut packages: Vec<(_, PathBuf)> = packie
|
|
.calculate_installation(&items, &CalculateInstallationOptions::new())?
|
|
.into_iter()
|
|
.map(|x| (x, PathBuf::new()))
|
|
.collect();
|
|
if packages.is_empty() {
|
|
eprintln!("{}", t!("install.no_package_to_install"));
|
|
return Ok(());
|
|
}
|
|
|
|
let download_size: u64 = packages.iter().map(|x| x.0.repo_pkg.download_size).sum();
|
|
let installed_size: u64 = packages.iter().map(|x| x.0.repo_pkg.installed_size).sum();
|
|
|
|
let summary = packages
|
|
.iter()
|
|
.map(|(x, _)| SummaryItem {
|
|
pkgspec: x.install_pkgspec.clone(),
|
|
pkgident: x.repo_pkg.pkg_manifest.pkg_ident(),
|
|
operation: SummaryItem::INSTALL,
|
|
})
|
|
.collect();
|
|
println!("{}", t!("install.prompt_to_install"));
|
|
println!();
|
|
print_summary(summary);
|
|
println!();
|
|
println!(
|
|
"{}",
|
|
t!("install.download_size", size = format_size(download_size))
|
|
);
|
|
println!(
|
|
"{}",
|
|
t!("install.installed_size", size = format_size(installed_size))
|
|
);
|
|
if cli.yes {
|
|
eprintln!("y");
|
|
} else if !yesno(&t!("install.confirm_installation"), true) {
|
|
eprintln!("{}", t!("install.aborting"));
|
|
return Ok(());
|
|
}
|
|
|
|
crate::common::download_packages(&mut packie, &mut packages, |x| &x.repo_pkg)?;
|
|
|
|
let nr_packages = packages.len();
|
|
let progress_bar = ProgressBar::new(nr_packages as _);
|
|
for (count, (package, file_path)) in packages.into_iter().enumerate() {
|
|
progress_bar.println(format!(
|
|
"[{}/{nr_packages}] {}",
|
|
count + 1,
|
|
t!(
|
|
"install.installing_hint",
|
|
pkgspec = style(&package.install_pkgspec).green()
|
|
)
|
|
));
|
|
|
|
let mut options = InstallOptions::default();
|
|
options.auto_installed = package.auto_installed;
|
|
options.install_pkgspec = Some(package.install_pkgspec);
|
|
packie.install_package_file(file_path, &options)?;
|
|
|
|
progress_bar.inc(1);
|
|
}
|
|
|
|
Ok(())
|
|
}
|