refactor(resolve): Give printing access to the workspace

This commit is contained in:
Ed Page 2024-04-12 17:05:56 -05:00
parent 29189d9908
commit 17a11e493c
2 changed files with 37 additions and 26 deletions

View File

@ -37,7 +37,7 @@ pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
true, true,
)?; )?;
ops::write_pkg_lockfile(ws, &mut resolve)?; ops::write_pkg_lockfile(ws, &mut resolve)?;
print_lockfile_changes(ws.gctx(), previous_resolve, &resolve, &mut registry)?; print_lockfile_changes(ws, previous_resolve, &resolve, &mut registry)?;
Ok(()) Ok(())
} }
@ -170,7 +170,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
true, true,
)?; )?;
print_lockfile_updates(opts.gctx, &previous_resolve, &resolve, &mut registry)?; print_lockfile_updates(ws, &previous_resolve, &resolve, &mut registry)?;
if opts.dry_run { if opts.dry_run {
opts.gctx opts.gctx
.shell() .shell()
@ -186,21 +186,23 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
/// This would acquire the package-cache lock, as it may update the index to /// This would acquire the package-cache lock, as it may update the index to
/// show users latest available versions. /// show users latest available versions.
pub fn print_lockfile_changes( pub fn print_lockfile_changes(
gctx: &GlobalContext, ws: &Workspace<'_>,
previous_resolve: Option<&Resolve>, previous_resolve: Option<&Resolve>,
resolve: &Resolve, resolve: &Resolve,
registry: &mut PackageRegistry<'_>, registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> { ) -> CargoResult<()> {
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?; let _lock = ws
.gctx()
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
if let Some(previous_resolve) = previous_resolve { if let Some(previous_resolve) = previous_resolve {
print_lockfile_sync(gctx, previous_resolve, resolve, registry) print_lockfile_sync(ws, previous_resolve, resolve, registry)
} else { } else {
print_lockfile_generation(gctx, resolve, registry) print_lockfile_generation(ws, resolve, registry)
} }
} }
fn print_lockfile_generation( fn print_lockfile_generation(
gctx: &GlobalContext, ws: &Workspace<'_>,
resolve: &Resolve, resolve: &Resolve,
registry: &mut PackageRegistry<'_>, registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> { ) -> CargoResult<()> {
@ -210,7 +212,8 @@ fn print_lockfile_generation(
// just ourself, nothing worth reporting // just ourself, nothing worth reporting
return Ok(()); return Ok(());
} }
gctx.shell() ws.gctx()
.shell()
.status("Locking", format!("{num_pkgs} packages"))?; .status("Locking", format!("{num_pkgs} packages"))?;
for diff in diff { for diff in diff {
@ -245,7 +248,7 @@ fn print_lockfile_generation(
}; };
if let Some(latest) = latest { if let Some(latest) = latest {
gctx.shell().status_with_color( ws.gctx().shell().status_with_color(
"Adding", "Adding",
format!("{package}{latest}"), format!("{package}{latest}"),
&style::NOTE, &style::NOTE,
@ -258,7 +261,7 @@ fn print_lockfile_generation(
} }
fn print_lockfile_sync( fn print_lockfile_sync(
gctx: &GlobalContext, ws: &Workspace<'_>,
previous_resolve: &Resolve, previous_resolve: &Resolve,
resolve: &Resolve, resolve: &Resolve,
registry: &mut PackageRegistry<'_>, registry: &mut PackageRegistry<'_>,
@ -269,7 +272,8 @@ fn print_lockfile_sync(
return Ok(()); return Ok(());
} }
let plural = if num_pkgs == 1 { "" } else { "s" }; let plural = if num_pkgs == 1 { "" } else { "s" };
gctx.shell() ws.gctx()
.shell()
.status("Locking", format!("{num_pkgs} package{plural}"))?; .status("Locking", format!("{num_pkgs} package{plural}"))?;
for diff in diff { for diff in diff {
@ -318,10 +322,12 @@ fn print_lockfile_sync(
// This metadata is often stuff like git commit hashes, which are // This metadata is often stuff like git commit hashes, which are
// not meaningfully ordered. // not meaningfully ordered.
if removed.version().cmp_precedence(added.version()) == Ordering::Greater { if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
gctx.shell() ws.gctx()
.shell()
.status_with_color("Downgrading", msg, &style::WARN)?; .status_with_color("Downgrading", msg, &style::WARN)?;
} else { } else {
gctx.shell() ws.gctx()
.shell()
.status_with_color("Updating", msg, &style::GOOD)?; .status_with_color("Updating", msg, &style::GOOD)?;
} }
} else { } else {
@ -339,7 +345,7 @@ fn print_lockfile_sync(
} }
.unwrap_or_default(); .unwrap_or_default();
gctx.shell().status_with_color( ws.gctx().shell().status_with_color(
"Adding", "Adding",
format!("{package}{latest}"), format!("{package}{latest}"),
&style::NOTE, &style::NOTE,
@ -352,7 +358,7 @@ fn print_lockfile_sync(
} }
fn print_lockfile_updates( fn print_lockfile_updates(
gctx: &GlobalContext, ws: &Workspace<'_>,
previous_resolve: &Resolve, previous_resolve: &Resolve,
resolve: &Resolve, resolve: &Resolve,
registry: &mut PackageRegistry<'_>, registry: &mut PackageRegistry<'_>,
@ -404,16 +410,21 @@ fn print_lockfile_updates(
// This metadata is often stuff like git commit hashes, which are // This metadata is often stuff like git commit hashes, which are
// not meaningfully ordered. // not meaningfully ordered.
if removed.version().cmp_precedence(added.version()) == Ordering::Greater { if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
gctx.shell() ws.gctx()
.shell()
.status_with_color("Downgrading", msg, &style::WARN)?; .status_with_color("Downgrading", msg, &style::WARN)?;
} else { } else {
gctx.shell() ws.gctx()
.shell()
.status_with_color("Updating", msg, &style::GOOD)?; .status_with_color("Updating", msg, &style::GOOD)?;
} }
} else { } else {
for package in diff.removed.iter() { for package in diff.removed.iter() {
gctx.shell() ws.gctx().shell().status_with_color(
.status_with_color("Removing", format!("{package}"), &style::ERROR)?; "Removing",
format!("{package}"),
&style::ERROR,
)?;
} }
for package in diff.added.iter() { for package in diff.added.iter() {
let latest = if !possibilities.is_empty() { let latest = if !possibilities.is_empty() {
@ -429,7 +440,7 @@ fn print_lockfile_updates(
} }
.unwrap_or_default(); .unwrap_or_default();
gctx.shell().status_with_color( ws.gctx().shell().status_with_color(
"Adding", "Adding",
format!("{package}{latest}"), format!("{package}{latest}"),
&style::NOTE, &style::NOTE,
@ -451,8 +462,8 @@ fn print_lockfile_updates(
if let Some(latest) = latest { if let Some(latest) = latest {
unchanged_behind += 1; unchanged_behind += 1;
if gctx.shell().verbosity() == Verbosity::Verbose { if ws.gctx().shell().verbosity() == Verbosity::Verbose {
gctx.shell().status_with_color( ws.gctx().shell().status_with_color(
"Unchanged", "Unchanged",
format!("{package}{latest}"), format!("{package}{latest}"),
&anstyle::Style::new().bold(), &anstyle::Style::new().bold(),
@ -462,13 +473,13 @@ fn print_lockfile_updates(
} }
} }
if gctx.shell().verbosity() == Verbosity::Verbose { if ws.gctx().shell().verbosity() == Verbosity::Verbose {
gctx.shell().note( ws.gctx().shell().note(
"to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`", "to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`",
)?; )?;
} else { } else {
if 0 < unchanged_behind { if 0 < unchanged_behind {
gctx.shell().note(format!( ws.gctx().shell().note(format!(
"pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest" "pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest"
))?; ))?;
} }

View File

@ -255,7 +255,7 @@ fn resolve_with_registry<'gctx>(
false false
}; };
if print { if print {
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?; ops::print_lockfile_changes(ws, prev.as_ref(), &resolve, registry)?;
} }
Ok(resolve) Ok(resolve)
} }