100 lines
3.0 KiB
Rust
100 lines
3.0 KiB
Rust
use crate::common::{SummaryItem, format_size, print_summary, yesno};
|
|
use clap::Parser;
|
|
use indicatif::ProgressBar;
|
|
use packie::{PackieBuilder, install::InstallOptions, package::PkgSpec};
|
|
use rust_i18n::t;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Parser)]
|
|
pub struct Cli {
|
|
/// Don't require manual confirmation
|
|
#[arg(short, long)]
|
|
yes: bool,
|
|
|
|
items: Option<Vec<PkgSpec>>,
|
|
}
|
|
|
|
pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
|
let mut packie = PackieBuilder::new().build()?;
|
|
let upgradation_summary = packie.calculate_upgradation(cli.items.unwrap_or_default())?;
|
|
|
|
if upgradation_summary.is_empty() {
|
|
eprintln!("{}", t!("upgrade.nothing_to_do"));
|
|
return Ok(());
|
|
}
|
|
|
|
println!("{}", t!("upgrade.prompt_to_upgrade"));
|
|
println!();
|
|
let mut ui_summary = Vec::new();
|
|
for install in &upgradation_summary.installs {
|
|
ui_summary.push(SummaryItem {
|
|
pkgspec: install.install_pkgspec.clone(),
|
|
pkgident: install.repo_pkg.pkg_manifest.pkg_ident(),
|
|
operation: SummaryItem::INSTALL,
|
|
});
|
|
}
|
|
for remove in &upgradation_summary.removes {
|
|
let pkgspec = PkgSpec {
|
|
name: remove.name.clone(),
|
|
version: packie::version::VersionFilter::Any,
|
|
arch: None,
|
|
};
|
|
ui_summary.push(SummaryItem {
|
|
pkgspec,
|
|
pkgident: remove.clone(),
|
|
operation: SummaryItem::REMOVE,
|
|
});
|
|
}
|
|
print_summary(ui_summary);
|
|
println!();
|
|
|
|
let mut installs: Vec<_> = upgradation_summary
|
|
.installs
|
|
.into_iter()
|
|
.map(|x| (x, PathBuf::new()))
|
|
.collect();
|
|
let removes = upgradation_summary.removes;
|
|
|
|
let download_size = installs.iter().map(|(x, _)| x.repo_pkg.download_size).sum();
|
|
let installed_size = installs
|
|
.iter()
|
|
.map(|(x, _)| x.repo_pkg.installed_size)
|
|
.sum();
|
|
println!(
|
|
"{}",
|
|
t!("upgrade.download_size", size = format_size(download_size))
|
|
);
|
|
println!(
|
|
"{}",
|
|
t!("upgrade.installed_size", size = format_size(installed_size))
|
|
);
|
|
|
|
if cli.yes {
|
|
eprintln!("y");
|
|
} else if !yesno(&t!("upgrade.confirm_upgradation"), true) {
|
|
eprintln!("{}", t!("upgrade.aborting"));
|
|
return Ok(());
|
|
}
|
|
|
|
crate::common::download_packages(&mut packie, &mut installs, |x| &x.repo_pkg)?;
|
|
|
|
let progress_bar = ProgressBar::new(installs.len() as u64 + removes.len() as u64);
|
|
for removal in removes {
|
|
progress_bar.println(format!("Removing \"{removal}\"..."));
|
|
packie.remove(&removal.into())?;
|
|
progress_bar.inc(1);
|
|
}
|
|
for (package, file_path) in installs {
|
|
progress_bar.println(format!("Installing \"{}\"...", package.install_pkgspec));
|
|
|
|
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(())
|
|
}
|