-
Notifications
You must be signed in to change notification settings - Fork 356
feat(tree): json representation #4261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abkfenris
wants to merge
1
commit into
prefix-dev:main
Choose a base branch
from
abkfenris:tree-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ use pixi_manifest::FeaturesExt; | |
use rattler_conda_types::Platform; | ||
use rattler_lock::LockedPackageRef; | ||
use regex::Regex; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
WorkspaceLocator, cli::cli_config::WorkspaceConfig, lock_file::UpdateLockFileOptions, | ||
|
@@ -36,7 +37,7 @@ use super::cli_config::LockFileUpdateConfig; | |
))] | ||
pub struct Args { | ||
/// List only packages matching a regular expression | ||
#[arg()] | ||
#[arg(conflicts_with = "json")] | ||
pub regex: Option<String>, | ||
|
||
/// The platform to list packages for. Defaults to the current platform. | ||
|
@@ -55,8 +56,12 @@ pub struct Args { | |
pub lock_file_update_config: LockFileUpdateConfig, | ||
|
||
/// Invert tree and show what depends on given package in the regex argument | ||
#[arg(short, long, requires = "regex")] | ||
#[arg(short, long, requires = "regex", conflicts_with = "json")] | ||
pub invert: bool, | ||
|
||
/// Show a JSON representation of the dependency tree | ||
#[arg(long)] | ||
pub json: bool, | ||
} | ||
|
||
struct Symbols { | ||
|
@@ -108,7 +113,9 @@ pub async fn execute(args: Args) -> miette::Result<()> { | |
|
||
let stdout = std::io::stdout(); | ||
let mut handle = stdout.lock(); | ||
if args.invert { | ||
if args.json { | ||
print_json_dependency_tree(&mut handle, &dep_map, &direct_deps)?; | ||
} else if args.invert { | ||
print_inverted_dependency_tree( | ||
&mut handle, | ||
&invert_dep_map(&dep_map), | ||
|
@@ -123,6 +130,60 @@ pub async fn execute(args: Args) -> miette::Result<()> { | |
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct DependencyTreeNode { | ||
name: String, | ||
version: String, | ||
tags: Vec<String>, | ||
dependencies: Vec<DependencyTreeNode>, | ||
} | ||
|
||
fn package_to_tree_node( | ||
package: &Package, | ||
dep_map: &HashMap<String, Package>, | ||
) -> DependencyTreeNode { | ||
let dependencies = package | ||
.dependencies | ||
.iter() | ||
.filter_map(|dep_name| dep_map.get(dep_name)) | ||
.map(|dep| package_to_tree_node(dep, dep_map)) | ||
.collect(); | ||
|
||
DependencyTreeNode { | ||
name: package.name.clone(), | ||
version: package.version.clone(), | ||
tags: Vec::new(), | ||
dependencies, | ||
} | ||
} | ||
|
||
/// Print a JSON representation of the dependency tree | ||
fn print_json_dependency_tree( | ||
handle: &mut StdoutLock, | ||
dep_map: &HashMap<String, Package>, | ||
direct_deps: &HashSet<String>, | ||
) -> miette::Result<()> { | ||
let dependencies: Vec<DependencyTreeNode> = direct_deps | ||
.iter() | ||
.filter_map(|pkg_name| dep_map.get(pkg_name)) | ||
.map(|pkg| package_to_tree_node(pkg, dep_map)) | ||
.collect(); | ||
let json = serde_json::to_string_pretty(&dependencies) | ||
.into_diagnostic() | ||
.wrap_err("Failed to serialize dependency tree to JSON")?; | ||
writeln!(handle, "{}", json) | ||
.map_err(|e| { | ||
if e.kind() == std::io::ErrorKind::BrokenPipe { | ||
// Exit gracefully | ||
std::process::exit(0); | ||
} else { | ||
e | ||
} | ||
}) | ||
Comment on lines
+175
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We dont do this anywhere else, can you explain why this is needed? |
||
.into_diagnostic() | ||
.wrap_err("Failed to serialize dependency tree to JSON") | ||
} | ||
|
||
/// Filter and print an inverted dependency tree | ||
fn print_inverted_dependency_tree( | ||
handle: &mut StdoutLock, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be nice to jam a little more info in here, we've had people ask for the
size
and I would say it misses thetype
(conda or pypi)