Skip to content

Commit

Permalink
feat: Make diff module public (#3010)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw authored Jan 26, 2025
1 parent 4cc530c commit 9b7740c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
// Format as json?
if args.json {
let diff = LockFileDiff::from_lock_files(&loaded_lock_file, &updated_lock_file.lock_file);
let json_diff = LockFileJsonDiff::new(&project, diff);
let json_diff = LockFileJsonDiff::new(Some(&project), diff);
let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json");
println!("{}", json);
} else if diff.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
let diff = update_deps.lock_file_diff;
// Format as json?
if args.json {
let json_diff = LockFileJsonDiff::new(&project, diff);
let json_diff = LockFileJsonDiff::new(Some(&project), diff);
let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json");
println!("{}", json);
} else {
Expand Down
21 changes: 13 additions & 8 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct LockFileDiff {

impl LockFileDiff {
/// Determine the difference between two lock-files.
pub(crate) fn from_lock_files(previous: &LockFile, current: &LockFile) -> Self {
pub fn from_lock_files(previous: &LockFile, current: &LockFile) -> Self {
let mut result = Self {
environment: IndexMap::new(),
};
Expand Down Expand Up @@ -170,12 +170,12 @@ impl LockFileDiff {
}

/// Returns true if the diff is empty.
pub(crate) fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.environment.is_empty()
}

// Format the lock-file diff.
pub(crate) fn print(&self) -> std::io::Result<()> {
pub fn print(&self) -> std::io::Result<()> {
let mut writer = TabWriter::new(stderr());
for (idx, (environment_name, environment)) in self
.environment
Expand Down Expand Up @@ -377,21 +377,26 @@ pub struct LockFileJsonDiff {
}

impl LockFileJsonDiff {
pub fn new(project: &Project, value: LockFileDiff) -> Self {
pub fn new(project: Option<&Project>, value: LockFileDiff) -> Self {
let mut environment = IndexMap::new();

for (environment_name, environment_diff) in value.environment {
let mut environment_diff_json = IndexMap::new();

for (platform, packages_diff) in environment_diff {
let conda_dependencies = project
.environment(environment_name.as_str())
.map(|env| env.dependencies(pixi_manifest::SpecType::Run, Some(platform)))
.and_then(|p| {
p.environment(environment_name.as_str()).map(|env| {
env.dependencies(pixi_manifest::SpecType::Run, Some(platform))
})
})
.unwrap_or_default();

let pypi_dependencies = project
.environment(environment_name.as_str())
.map(|env| env.pypi_dependencies(Some(platform)))
.and_then(|p| {
p.environment(environment_name.as_str())
.map(|env| env.pypi_dependencies(Some(platform)))
})
.unwrap_or_default();

let add_diffs = packages_diff.added.into_iter().map(|new| match new {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

pub mod activation;
pub mod cli;
mod diff;
pub mod diff;
pub mod environment;
mod global;
mod install_pypi;
pub mod lock_file;
mod prefix;
mod project;
pub mod project;
mod prompt;
pub(crate) mod repodata;
pub mod task;
Expand Down

0 comments on commit 9b7740c

Please sign in to comment.