1 parent
b5e91ce8b6
commit
6cbfa14b64
16 files changed
+624
-190
No files matched your search
+1
-1
@@ -13,8 +13,8 @@ chrono = "0.4"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
console = "0.16"
|
||||
nix = { version = "0.31", features = ["sched"] }
|
||||
itertools = "0.15"
|
||||
packie = { path = "../" }
|
||||
rust-i18n = "4"
|
||||
tabled = { version = "0.22", features = ["ansi"] }
|
||||
indicatif = "0.18"
|
||||
dialoguer = "0.12"
|
||||
+87
-17
@@ -1,8 +1,19 @@
|
||||
use std::{fmt::Display, path::PathBuf};
|
||||
|
||||
use chrono::{Local, TimeZone};
|
||||
use console::{Term, style};
|
||||
use indicatif::ProgressStyle;
|
||||
use packie::{Packie, local::Installation, package::PkgSpec};
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use packie::{
|
||||
Packie,
|
||||
local::Installation,
|
||||
package::{PkgIdent, PkgSpec},
|
||||
repo::{DownloadPackageEvent, RepoPackage},
|
||||
};
|
||||
use rust_i18n::t;
|
||||
use tabled::{
|
||||
Table, Tabled,
|
||||
settings::{Style, Width},
|
||||
};
|
||||
|
||||
pub fn progress_style_multidownload() -> ProgressStyle {
|
||||
let mut lines = Vec::with_capacity(3);
|
||||
@@ -45,23 +56,51 @@ pub fn yesno(prompt: &str, default: bool) -> bool {
|
||||
["y", "Y"].contains(&line) || (line.is_empty() && default)
|
||||
}
|
||||
|
||||
pub fn print_list(indent: u32, list: impl Iterator<Item = impl AsRef<str>>) {
|
||||
let (_, cols) = Term::stdout().size();
|
||||
let mut line_used: usize = 0;
|
||||
for i in list {
|
||||
let mut i = i.as_ref().to_string();
|
||||
if line_used >= cols as _ {
|
||||
line_used = 0;
|
||||
println!();
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SummaryItem {
|
||||
pub pkgspec: PkgSpec,
|
||||
pub pkgident: PkgIdent,
|
||||
pub operation: &'static str,
|
||||
}
|
||||
impl SummaryItem {
|
||||
pub const INSTALL: &str = "install";
|
||||
pub const REMOVE: &str = "remove";
|
||||
}
|
||||
|
||||
#[derive(Debug, Tabled)]
|
||||
struct PrintedSummaryItem {
|
||||
pkgspec: String,
|
||||
version: String,
|
||||
operation: String,
|
||||
notes: String,
|
||||
}
|
||||
impl From<SummaryItem> for PrintedSummaryItem {
|
||||
fn from(value: SummaryItem) -> Self {
|
||||
let wrap_color = |s: &dyn Display| {
|
||||
if value.operation == SummaryItem::INSTALL {
|
||||
format!("{}", console::style(s).green())
|
||||
} else if value.operation == SummaryItem::REMOVE {
|
||||
format!("{}", console::style(s).red())
|
||||
} else {
|
||||
format!("{}", console::style(s).blue())
|
||||
}
|
||||
};
|
||||
Self {
|
||||
pkgspec: wrap_color(&value.pkgspec),
|
||||
version: wrap_color(&value.pkgident.version),
|
||||
operation: wrap_color(&value.operation),
|
||||
notes: String::new(),
|
||||
}
|
||||
if i.contains(' ') {
|
||||
i = format!("'{i}'");
|
||||
}
|
||||
let to_print = format!("{}{}", " ".repeat(indent as _), i);
|
||||
print!("{to_print}");
|
||||
line_used += to_print.len();
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
pub fn print_summary(summary: Vec<SummaryItem>) {
|
||||
let data: Vec<_> = summary.into_iter().map(PrintedSummaryItem::from).collect();
|
||||
let mut table = Table::new(data);
|
||||
let term_width = console::Term::stdout().size().1 as usize;
|
||||
table.with(Width::wrap(term_width));
|
||||
table.with(Style::psql());
|
||||
println!("{}", table);
|
||||
}
|
||||
|
||||
pub fn format_size(bytes: u64) -> String {
|
||||
@@ -96,3 +135,34 @@ pub fn search_unique_package(
|
||||
}
|
||||
Ok(found.remove(0))
|
||||
}
|
||||
|
||||
pub fn download_packages<T>(
|
||||
packie: &mut Packie,
|
||||
packages: &mut Vec<(T, PathBuf)>,
|
||||
pkginfo: impl Fn(&T) -> &RepoPackage,
|
||||
) -> Result<(), crate::Error> {
|
||||
let download_size = packages.iter().map(|x| pkginfo(&x.0).download_size).sum();
|
||||
let progress_bar =
|
||||
ProgressBar::new(download_size).with_style(crate::common::progress_style_multidownload());
|
||||
for (package, file_path) in packages.iter_mut() {
|
||||
let repo_pkg = pkginfo(package);
|
||||
let init_pos = progress_bar.position();
|
||||
progress_bar.println(format!(
|
||||
"Downloading \"{}\"...",
|
||||
repo_pkg.pkg_manifest.pkg_ident()
|
||||
));
|
||||
progress_bar.set_prefix(repo_pkg.pkg_manifest.pkg_ident().to_string());
|
||||
let path = packie.download_package(&repo_pkg.repo_pkg_ident().unwrap(), |ev| match ev {
|
||||
DownloadPackageEvent::Progress(_, sum, _) => {
|
||||
progress_bar.set_position(init_pos + sum);
|
||||
}
|
||||
DownloadPackageEvent::Error(url, err) => {
|
||||
progress_bar.println(format!("error: failed to download from \"{url}\": {err}"));
|
||||
}
|
||||
})?;
|
||||
*file_path = path;
|
||||
progress_bar.set_position(init_pos + repo_pkg.download_size);
|
||||
}
|
||||
drop(progress_bar);
|
||||
Ok(())
|
||||
}
|
||||
+33
-46
@@ -1,12 +1,11 @@
|
||||
use crate::common::{format_size, print_list, yesno};
|
||||
use crate::common::{SummaryItem, format_size, print_summary, yesno};
|
||||
use clap::Parser;
|
||||
use console::style;
|
||||
use indicatif::ProgressBar;
|
||||
use itertools::Itertools;
|
||||
use packie::{
|
||||
PackieBuilder, install::InstallOptions, package::PkgSpec, repo::DownloadPackageEvent,
|
||||
PackieBuilder, graph::CalculateInstallationOptions, install::InstallOptions, package::PkgSpec,
|
||||
};
|
||||
use rust_i18n::t;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct Cli {
|
||||
@@ -31,30 +30,36 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut packages = Vec::with_capacity(cli.items.len() * 4);
|
||||
let mut items = Vec::with_capacity(cli.items.len());
|
||||
for i in cli.items {
|
||||
packages.append(&mut packie.calculate_depgraph(i.parse::<PkgSpec>()?)?);
|
||||
items.push(i.parse::<PkgSpec>()?);
|
||||
}
|
||||
let packages: Vec<_> = packages
|
||||
.into_iter()
|
||||
.unique_by(|x| x.repo_pkg.pkg_manifest.pkg_ident())
|
||||
.collect();
|
||||
|
||||
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.repo_pkg.download_size).sum();
|
||||
let installed_size: u64 = packages.iter().map(|x| x.repo_pkg.installed_size).sum();
|
||||
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"));
|
||||
print_list(
|
||||
4,
|
||||
packages
|
||||
.iter()
|
||||
.map(|x| style(&x.pkgspec).green().to_string()),
|
||||
);
|
||||
println!();
|
||||
print_summary(summary);
|
||||
println!();
|
||||
println!(
|
||||
"{}",
|
||||
t!("install.download_size", size = format_size(download_size))
|
||||
@@ -70,35 +75,17 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let progress_bar =
|
||||
ProgressBar::new(download_size).with_style(crate::common::progress_style_multidownload());
|
||||
let mut package_files = Vec::with_capacity(packages.len());
|
||||
for package in packages.iter() {
|
||||
let init_pos = progress_bar.position();
|
||||
progress_bar.println(format!("Downloading \"{}\"...", package.pkgspec));
|
||||
progress_bar.set_prefix(package.repo_pkg.pkg_manifest.pkg_ident().to_string());
|
||||
let path = packie.download_package(
|
||||
&package.repo_name,
|
||||
&package.repo_pkg.pkg_manifest.pkg_ident(),
|
||||
|ev| match ev {
|
||||
DownloadPackageEvent::Progress(_, sum, _) => {
|
||||
progress_bar.set_position(init_pos + sum);
|
||||
}
|
||||
DownloadPackageEvent::Error(url, err) => {
|
||||
progress_bar
|
||||
.println(format!("error: failed to download from \"{url}\": {err}"));
|
||||
}
|
||||
},
|
||||
)?;
|
||||
package_files.push(path);
|
||||
progress_bar.set_position(init_pos + package.repo_pkg.download_size);
|
||||
}
|
||||
drop(progress_bar);
|
||||
crate::common::download_packages(&mut packie, &mut packages, |x| &x.repo_pkg)?;
|
||||
|
||||
let progress_bar = ProgressBar::new(packages.len() as _);
|
||||
for (package, file_path) in packages {
|
||||
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)?;
|
||||
|
||||
let progress_bar = ProgressBar::new(package_files.len() as _);
|
||||
for package in package_files.iter() {
|
||||
progress_bar.println(format!("Installing \"{}\"...", package.display()));
|
||||
packie.install_package_file(package, &InstallOptions::default())?;
|
||||
progress_bar.inc(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ mod remove;
|
||||
mod repo_admin;
|
||||
mod select;
|
||||
mod update;
|
||||
mod upgrade;
|
||||
|
||||
use clap::Parser;
|
||||
use console::style;
|
||||
@@ -40,6 +41,9 @@ enum Subcommand {
|
||||
/// Install one or more packages
|
||||
Install(install::Cli),
|
||||
|
||||
/// Upgrade installed packages
|
||||
Upgrade(upgrade::Cli),
|
||||
|
||||
/// Print configuration information
|
||||
Print(print::Cli),
|
||||
|
||||
@@ -78,6 +82,7 @@ fn main() {
|
||||
Subcommand::Info(cli) => info::main(cli),
|
||||
Subcommand::InitDb(cli) => initdb::main(cli),
|
||||
Subcommand::Install(cli) => install::main(cli),
|
||||
Subcommand::Upgrade(cli) => upgrade::main(cli),
|
||||
Subcommand::Print(cli) => print::main(cli),
|
||||
Subcommand::Remove(cli) => remove::main(cli),
|
||||
Subcommand::RepoAdmin(cli) => repo_admin::main(cli),
|
||||
|
||||
+13
-8
@@ -1,7 +1,6 @@
|
||||
use crate::common::{print_list, search_unique_package, yesno};
|
||||
use crate::common::{SummaryItem, print_summary, search_unique_package, yesno};
|
||||
use anyhow::anyhow;
|
||||
use clap::Parser;
|
||||
use console::style;
|
||||
use packie::{PackieBuilder, package::PkgSpec};
|
||||
use rust_i18n::t;
|
||||
|
||||
@@ -19,13 +18,19 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
||||
for pkgspec in cli.items {
|
||||
packages.push(search_unique_package(&mut packie, &pkgspec)?);
|
||||
}
|
||||
|
||||
let summary = packages
|
||||
.iter()
|
||||
.map(|x| SummaryItem {
|
||||
pkgspec: x.install_pkgspec.clone(),
|
||||
pkgident: x.pkg_manifest.pkg_ident(),
|
||||
operation: SummaryItem::REMOVE,
|
||||
})
|
||||
.collect();
|
||||
println!("{}", t!("remove.prompt_to_remove"));
|
||||
print_list(
|
||||
4,
|
||||
packages
|
||||
.iter()
|
||||
.map(|x| style(&x.install_pkgspec).green().to_string()),
|
||||
);
|
||||
println!();
|
||||
print_summary(summary);
|
||||
println!();
|
||||
if cli.yes {
|
||||
println!("y");
|
||||
} else if !yesno(&t!("remove.confirm_removal"), false) {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
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(())
|
||||
}
|
||||
Reference in new issue
Block a user