Skip to content

Commit

Permalink
Feat(main): Display ending report
Browse files Browse the repository at this point in the history
Main part of #12.

Signed-off-by: Paul Mabileau <paulmabileau@hotmail.fr>
  • Loading branch information
PaulDance committed May 31, 2024
1 parent a5d94c5 commit e23ec9f
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 22 deletions.
102 changes: 80 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tempfile as _;
use trycmd as _;

mod cargo;
use cargo::InstallStatus;
mod cli;
use cli::{LinerArgs, LinerCommands};
mod config;
Expand Down Expand Up @@ -153,35 +154,47 @@ fn try_main(args: &LinerArgs) -> Result<()> {
.update_others(!ship_args.only_self);
}

let inst_res = if skip_check {
let (inst_res, old_vers, new_vers) = if skip_check {
// Don't parse `.crates.toml` here: can be used as a workaround.
cargo::install_all(
&config.packages,
&BTreeSet::new(),
keep_going,
force,
args.color,
cargo_verbosity,
(
cargo::install_all(
&config.packages,
&BTreeSet::new(),
keep_going,
force,
args.color,
cargo_verbosity,
),
BTreeMap::new(),
BTreeMap::new(),
)
} else {
let cct = CargoCratesToml::parse_file()
.wrap_err("Failed to parse Cargo's .crates.toml file.")?;
let vers = cargo::search_exact_all(&config.packages)
let old_vers = cct.clone().into_name_versions();
let new_vers = cargo::search_exact_all(&config.packages)
.wrap_err("Failed to fetch the latest versions of the configured packages.")?;
log_summary(&colorizer, &vers, &cct.clone().into_name_versions());

cargo::install_all(
&needing_install(&config.packages, &vers, &cct.clone().into_name_versions()),
&cct.into_names(),
keep_going,
force,
args.color,
cargo_verbosity,
log_version_check_summary(&colorizer, &new_vers, &old_vers);

(
cargo::install_all(
&needing_install(&config.packages, &new_vers, &old_vers),
&cct.into_names(),
keep_going,
force,
args.color,
cargo_verbosity,
),
old_vers,
new_vers,
)
};

if let Some(err) = match inst_res {
Ok(rep) => rep.error_report,
Ok(rep) => {
log_install_report(&colorizer, &rep.package_statuses, &new_vers, &old_vers);
rep.error_report
}
Err(err) => Some(err),
} {
Err(err).wrap_err_with(|| {
Expand Down Expand Up @@ -273,7 +286,7 @@ struct PackageStatus {
}

/// Displays whether each package needs an update or not.
fn log_summary(
fn log_version_check_summary(
colorizer: &Colorizer,
new_vers: &BTreeMap<String, Version>,
old_vers: &BTreeMap<String, Version>,
Expand Down Expand Up @@ -306,11 +319,46 @@ fn log_summary(
}
}

/// Displays the ending report about each package's installation status.
fn log_install_report(
colorizer: &Colorizer,
install_report: &BTreeMap<String, InstallStatus>,
new_vers: &BTreeMap<String, Version>,
old_vers: &BTreeMap<String, Version>,
) {
if !install_report.is_empty() {
log::info!(
"Installation report:\n{}",
Table::new(install_report.iter().map(|(pkg_name, status)| {
PackageStatus {
name: pkg_name.clone(),
old_ver: old_vers
.get(pkg_name)
.map_or_else(|| colorizer.none_icon().to_string(), ToString::to_string),
new_ver: new_vers
.get(pkg_name)
.map_or_else(|| colorizer.none_icon().to_string(), ToString::to_string),
status: match status {
InstallStatus::Installed => colorizer.new_icon().to_string(),
InstallStatus::Updated => colorizer.ok_icon().to_string(),
InstallStatus::Failed => colorizer.err_icon().to_string(),
},
}
}))
.with(Style::sharp())
);
}
}

/// When nothing to display or needs to be done: already up-to-date.
const NONE_ICON: &str = "ø";
/// When something needs to be performed: fresh installation of a package.
/// When something needs to be performed: installation or update of a package.
const TODO_ICON: &str = "🛈";
/// When things went right: already up-to-date.
/// When something was successfully added: new installation of a package.
const NEW_ICON: &str = "+";
/// When something failed.
const ERR_ICON: &str = "✘";
/// When things went right: already up-to-date or successful update.
const OK_ICON: &str = "✔";

/// Assembles both an output stream's color capacity and a color preference in
Expand Down Expand Up @@ -366,6 +414,16 @@ impl Colorizer {
self.colorize_with(&TODO_ICON, |s| s.bold().blue().to_string())
}

/// Returns the colorized version of [`NEW_ICON`].
pub fn new_icon(&self) -> impl Display {
self.colorize_with(&NEW_ICON, |s| s.bold().green().to_string())
}

/// Returns the colorized version of [`ERR_ICON`].
pub fn err_icon(&self) -> impl Display {
self.colorize_with(&ERR_ICON, <&str>::red)
}

/// Returns the colorized version of [`OK_ICON`].
pub fn ok_icon(&self) -> impl Display {
self.colorize_with(&OK_ICON, <&str>::green)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
Installing /tmp/[..]/bin/pkg
Installed package `pkg v0.0.0` (executable `pkg`)
warning: be sure to add `/tmp/[..]/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ pkg │ ø │ 0.0.0 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_extrargs_list.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
INFO cargo_liner::cargo > Installing `abc`...
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ abc │ ø │ ø │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_features.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
Installing [..]/.cargo/bin/pkg
Installed package `pkg v0.0.0` (executable `pkg`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ pkg │ ø │ 0.0.0 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ error: could not find `abc` in registry `crates-io` with version `*`
Installing [..]/.cargo/bin/def
Installed package `def v0.0.0` (executable `def`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ abc │ ø │ ø │ ✘ │
│ def │ ø │ ø │ + │
└──────┴─────────────┴─────────────┴────────┘
Error:
0: Failed to install or update some of the configured packages.
1: Failed to install "abc".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the ins
Installing [..]/.cargo/bin/def
Installed package `def v0.0.0` (executable `def`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ abc │ ø │ ø │ + │
│ def │ ø │ ø │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
7 changes: 7 additions & 0 deletions tests/fixtures/ship/validate_ship_manynewer_update.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the ins
Replacing [..]/.cargo/bin/def
Replaced package `def v0.0.2 (/a/b/c)` with `def v0.0.3` (executable `def`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ abc │ 0.0.1 │ 0.0.2 │ ✔ │
│ def │ 0.0.2 │ 0.0.3 │ ✔ │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the ins
Installing [..]/.cargo/bin/def
Installed package `def v0.0.2` (executable `def`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ abc │ ø │ 0.0.1 │ + │
│ def │ ø │ 0.0.2 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
Replacing [..]/.cargo/bin/cargo-liner
Replaced package `cargo-liner v0.0.3` with `cargo-liner v0.0.4` (executable `cargo-liner`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌─────────────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├─────────────┼─────────────┼─────────────┼────────┤
│ cargo-liner │ ø │ ø │ + │
└─────────────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_newerself_update.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
Replacing [..]/.cargo/bin/cargo-liner
Replaced package `cargo-liner v0.0.3` with `cargo-liner v0.0.4` (executable `cargo-liner`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌─────────────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├─────────────┼─────────────┼─────────────┼────────┤
│ cargo-liner │ 0.0.3 │ 0.0.4 │ ✔ │
└─────────────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
Replacing [..]/.cargo/bin/cargo-liner
Replaced package `cargo-liner v0.0.3` with `cargo-liner v0.0.3` (executable `cargo-liner`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌─────────────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├─────────────┼─────────────┼─────────────┼────────┤
│ cargo-liner │ ø │ ø │ + │
└─────────────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_skipcheck_noupdate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
Downloaded cargo-liner v0.0.3 (registry `dummy-registry`)
Ignored package `cargo-liner v0.0.3` is already installed, use --force to override
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌─────────────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├─────────────┼─────────────┼─────────────┼────────┤
│ cargo-liner │ ø │ ø │ + │
└─────────────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_verbosity_v.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
Installing [..]/.cargo/bin/pkg
Installed package `pkg v0.0.0` (executable `pkg`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ pkg │ ø │ 0.0.0 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_verbosity_vv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
Installing [..]/.cargo/bin/pkg
Installed package `pkg v0.0.0` (executable `pkg`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ pkg │ ø │ 0.0.0 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
6 changes: 6 additions & 0 deletions tests/fixtures/ship/validate_ship_verbosity_vvv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
Installing [..]/.cargo/bin/pkg
Installed package `pkg v0.0.0` (executable `pkg`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬─────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼─────────────┼────────┤
│ pkg │ ø │ 0.0.0 │ + │
└──────┴─────────────┴─────────────┴────────┘
INFO cargo_liner > Done.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
Installing [..]/.cargo/bin/pkg
Installed package `pkg v1.0.0+meta123` (executable `pkg`)
warning: be sure to add `[..]/.cargo/bin` to your PATH to be able to run the installed binaries
INFO cargo_liner > Installation report:
┌──────┬─────────────┬───────────────┬────────┐
│ Name │ Old version │ New version │ Status │
├──────┼─────────────┼───────────────┼────────┤
│ pkg │ ø │ 1.0.0+meta123 │ + │
└──────┴─────────────┴───────────────┴────────┘
INFO cargo_liner > Done.

0 comments on commit e23ec9f

Please sign in to comment.