Skip to content

Commit 17a11e4

Browse files
committed
refactor(resolve): Give printing access to the workspace
1 parent 29189d9 commit 17a11e4

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
3737
true,
3838
)?;
3939
ops::write_pkg_lockfile(ws, &mut resolve)?;
40-
print_lockfile_changes(ws.gctx(), previous_resolve, &resolve, &mut registry)?;
40+
print_lockfile_changes(ws, previous_resolve, &resolve, &mut registry)?;
4141
Ok(())
4242
}
4343

@@ -170,7 +170,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
170170
true,
171171
)?;
172172

173-
print_lockfile_updates(opts.gctx, &previous_resolve, &resolve, &mut registry)?;
173+
print_lockfile_updates(ws, &previous_resolve, &resolve, &mut registry)?;
174174
if opts.dry_run {
175175
opts.gctx
176176
.shell()
@@ -186,21 +186,23 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
186186
/// This would acquire the package-cache lock, as it may update the index to
187187
/// show users latest available versions.
188188
pub fn print_lockfile_changes(
189-
gctx: &GlobalContext,
189+
ws: &Workspace<'_>,
190190
previous_resolve: Option<&Resolve>,
191191
resolve: &Resolve,
192192
registry: &mut PackageRegistry<'_>,
193193
) -> CargoResult<()> {
194-
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
194+
let _lock = ws
195+
.gctx()
196+
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
195197
if let Some(previous_resolve) = previous_resolve {
196-
print_lockfile_sync(gctx, previous_resolve, resolve, registry)
198+
print_lockfile_sync(ws, previous_resolve, resolve, registry)
197199
} else {
198-
print_lockfile_generation(gctx, resolve, registry)
200+
print_lockfile_generation(ws, resolve, registry)
199201
}
200202
}
201203

202204
fn print_lockfile_generation(
203-
gctx: &GlobalContext,
205+
ws: &Workspace<'_>,
204206
resolve: &Resolve,
205207
registry: &mut PackageRegistry<'_>,
206208
) -> CargoResult<()> {
@@ -210,7 +212,8 @@ fn print_lockfile_generation(
210212
// just ourself, nothing worth reporting
211213
return Ok(());
212214
}
213-
gctx.shell()
215+
ws.gctx()
216+
.shell()
214217
.status("Locking", format!("{num_pkgs} packages"))?;
215218

216219
for diff in diff {
@@ -245,7 +248,7 @@ fn print_lockfile_generation(
245248
};
246249

247250
if let Some(latest) = latest {
248-
gctx.shell().status_with_color(
251+
ws.gctx().shell().status_with_color(
249252
"Adding",
250253
format!("{package}{latest}"),
251254
&style::NOTE,
@@ -258,7 +261,7 @@ fn print_lockfile_generation(
258261
}
259262

260263
fn print_lockfile_sync(
261-
gctx: &GlobalContext,
264+
ws: &Workspace<'_>,
262265
previous_resolve: &Resolve,
263266
resolve: &Resolve,
264267
registry: &mut PackageRegistry<'_>,
@@ -269,7 +272,8 @@ fn print_lockfile_sync(
269272
return Ok(());
270273
}
271274
let plural = if num_pkgs == 1 { "" } else { "s" };
272-
gctx.shell()
275+
ws.gctx()
276+
.shell()
273277
.status("Locking", format!("{num_pkgs} package{plural}"))?;
274278

275279
for diff in diff {
@@ -318,10 +322,12 @@ fn print_lockfile_sync(
318322
// This metadata is often stuff like git commit hashes, which are
319323
// not meaningfully ordered.
320324
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
321-
gctx.shell()
325+
ws.gctx()
326+
.shell()
322327
.status_with_color("Downgrading", msg, &style::WARN)?;
323328
} else {
324-
gctx.shell()
329+
ws.gctx()
330+
.shell()
325331
.status_with_color("Updating", msg, &style::GOOD)?;
326332
}
327333
} else {
@@ -339,7 +345,7 @@ fn print_lockfile_sync(
339345
}
340346
.unwrap_or_default();
341347

342-
gctx.shell().status_with_color(
348+
ws.gctx().shell().status_with_color(
343349
"Adding",
344350
format!("{package}{latest}"),
345351
&style::NOTE,
@@ -352,7 +358,7 @@ fn print_lockfile_sync(
352358
}
353359

354360
fn print_lockfile_updates(
355-
gctx: &GlobalContext,
361+
ws: &Workspace<'_>,
356362
previous_resolve: &Resolve,
357363
resolve: &Resolve,
358364
registry: &mut PackageRegistry<'_>,
@@ -404,16 +410,21 @@ fn print_lockfile_updates(
404410
// This metadata is often stuff like git commit hashes, which are
405411
// not meaningfully ordered.
406412
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
407-
gctx.shell()
413+
ws.gctx()
414+
.shell()
408415
.status_with_color("Downgrading", msg, &style::WARN)?;
409416
} else {
410-
gctx.shell()
417+
ws.gctx()
418+
.shell()
411419
.status_with_color("Updating", msg, &style::GOOD)?;
412420
}
413421
} else {
414422
for package in diff.removed.iter() {
415-
gctx.shell()
416-
.status_with_color("Removing", format!("{package}"), &style::ERROR)?;
423+
ws.gctx().shell().status_with_color(
424+
"Removing",
425+
format!("{package}"),
426+
&style::ERROR,
427+
)?;
417428
}
418429
for package in diff.added.iter() {
419430
let latest = if !possibilities.is_empty() {
@@ -429,7 +440,7 @@ fn print_lockfile_updates(
429440
}
430441
.unwrap_or_default();
431442

432-
gctx.shell().status_with_color(
443+
ws.gctx().shell().status_with_color(
433444
"Adding",
434445
format!("{package}{latest}"),
435446
&style::NOTE,
@@ -451,8 +462,8 @@ fn print_lockfile_updates(
451462

452463
if let Some(latest) = latest {
453464
unchanged_behind += 1;
454-
if gctx.shell().verbosity() == Verbosity::Verbose {
455-
gctx.shell().status_with_color(
465+
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
466+
ws.gctx().shell().status_with_color(
456467
"Unchanged",
457468
format!("{package}{latest}"),
458469
&anstyle::Style::new().bold(),
@@ -462,13 +473,13 @@ fn print_lockfile_updates(
462473
}
463474
}
464475

465-
if gctx.shell().verbosity() == Verbosity::Verbose {
466-
gctx.shell().note(
476+
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
477+
ws.gctx().shell().note(
467478
"to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`",
468479
)?;
469480
} else {
470481
if 0 < unchanged_behind {
471-
gctx.shell().note(format!(
482+
ws.gctx().shell().note(format!(
472483
"pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest"
473484
))?;
474485
}

src/cargo/ops/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn resolve_with_registry<'gctx>(
255255
false
256256
};
257257
if print {
258-
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?;
258+
ops::print_lockfile_changes(ws, prev.as_ref(), &resolve, registry)?;
259259
}
260260
Ok(resolve)
261261
}

0 commit comments

Comments
 (0)