|
| 1 | +use std::fmt::Write; |
| 2 | + |
| 3 | +use serde::Serialize; |
| 4 | + |
| 5 | +use uv_cli::PipInstallFormat; |
| 6 | +use uv_configuration::DryRun; |
| 7 | +use uv_distribution_types::Name; |
| 8 | +use uv_normalize::PackageName; |
| 9 | + |
| 10 | +use crate::commands::pip::operations::{ChangedDist, Changelog}; |
| 11 | +use crate::printer::Printer; |
| 12 | + |
| 13 | +/// Write the package changes as JSON when requested. |
| 14 | +pub(crate) fn write_install_report( |
| 15 | + changelog: &Changelog, |
| 16 | + dry_run: DryRun, |
| 17 | + output_format: PipInstallFormat, |
| 18 | + printer: Printer, |
| 19 | +) -> anyhow::Result<()> { |
| 20 | + match output_format { |
| 21 | + PipInstallFormat::Text => {} |
| 22 | + PipInstallFormat::Json => { |
| 23 | + let report = InstallReport { |
| 24 | + schema: SchemaReport::default(), |
| 25 | + changes: PackageChangesReport::from_changelog(changelog), |
| 26 | + dry_run: dry_run.enabled(), |
| 27 | + }; |
| 28 | + writeln!( |
| 29 | + printer.stdout_important(), |
| 30 | + "{}", |
| 31 | + serde_json::to_string_pretty(&report)? |
| 32 | + )?; |
| 33 | + } |
| 34 | + } |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +/// A report of the changes made or planned by `uv pip install` or `uv pip sync`. |
| 39 | +#[derive(Debug, Serialize)] |
| 40 | +struct InstallReport { |
| 41 | + schema: SchemaReport, |
| 42 | + changes: PackageChangesReport, |
| 43 | + dry_run: bool, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Serialize, Debug, Default)] |
| 47 | +#[serde(rename_all = "snake_case")] |
| 48 | +enum SchemaVersion { |
| 49 | + /// An unstable, experimental schema. |
| 50 | + #[default] |
| 51 | + Preview, |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Serialize, Debug, Default)] |
| 55 | +pub(crate) struct SchemaReport { |
| 56 | + /// The version of the schema. |
| 57 | + version: SchemaVersion, |
| 58 | +} |
| 59 | + |
| 60 | +/// A summary of all package changes made or planned during an installation. |
| 61 | +#[derive(Serialize, Debug, Clone, Default)] |
| 62 | +pub(crate) struct PackageChangesReport(Vec<PackageChangeReport>); |
| 63 | + |
| 64 | +impl PackageChangesReport { |
| 65 | + pub(crate) fn from_changelog(changelog: &Changelog) -> Self { |
| 66 | + let mut changes: Vec<_> = |
| 67 | + changelog |
| 68 | + .uninstalled |
| 69 | + .iter() |
| 70 | + .map(|dist| PackageChangeReport::from_dist(dist, PackageChangeAction::Uninstalled)) |
| 71 | + .chain(changelog.installed.iter().map(|dist| { |
| 72 | + PackageChangeReport::from_dist(dist, PackageChangeAction::Installed) |
| 73 | + })) |
| 74 | + .chain(changelog.reinstalled.iter().map(|dist| { |
| 75 | + PackageChangeReport::from_dist(dist, PackageChangeAction::Reinstalled) |
| 76 | + })) |
| 77 | + .collect(); |
| 78 | + |
| 79 | + changes.sort_by(|a, b| { |
| 80 | + a.name |
| 81 | + .cmp(&b.name) |
| 82 | + .then_with(|| a.action.cmp(&b.action)) |
| 83 | + .then_with(|| a.version.cmp(&b.version)) |
| 84 | + }); |
| 85 | + Self(changes) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// A summary of a single package change made or planned during an installation. |
| 90 | +#[derive(Serialize, Debug, Clone)] |
| 91 | +struct PackageChangeReport { |
| 92 | + /// The normalized package name. |
| 93 | + name: PackageName, |
| 94 | + /// The resolved version of the package. |
| 95 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 96 | + version: Option<uv_pep440::Version>, |
| 97 | + /// The action that was taken for the package. |
| 98 | + action: PackageChangeAction, |
| 99 | +} |
| 100 | + |
| 101 | +impl PackageChangeReport { |
| 102 | + fn from_dist(dist: &ChangedDist, action: PackageChangeAction) -> Self { |
| 103 | + Self { |
| 104 | + name: dist.name().clone(), |
| 105 | + version: dist.version().cloned(), |
| 106 | + action, |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// The action taken or planned for an individual package. |
| 112 | +#[derive(Serialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 113 | +#[serde(rename_all = "snake_case")] |
| 114 | +enum PackageChangeAction { |
| 115 | + Uninstalled, |
| 116 | + Installed, |
| 117 | + Reinstalled, |
| 118 | +} |
0 commit comments