|
| 1 | +use crate::shared::tree::{ |
| 2 | + Package, PackageSource, build_reverse_dependency_map, print_dependency_tree, |
| 3 | + print_inverted_dependency_tree, |
| 4 | +}; |
| 5 | +use ahash::HashSet; |
| 6 | +use clap::Parser; |
| 7 | +use console::Color; |
| 8 | +use itertools::Itertools; |
| 9 | +use miette::Context; |
| 10 | +use pixi_consts::consts; |
| 11 | +use pixi_global::common::find_package_records; |
| 12 | +use pixi_global::{EnvRoot, EnvironmentName, Project}; |
| 13 | +use std::collections::HashMap; |
| 14 | +use std::str::FromStr; |
| 15 | + |
| 16 | +/// Show a tree of dependencies for a specific global environment. |
| 17 | +#[derive(Debug, Parser)] |
| 18 | +#[clap(arg_required_else_help = false, long_about = format!( |
| 19 | + "\ |
| 20 | + Show a tree of a global environment dependencies\n\ |
| 21 | + \n\ |
| 22 | + Dependency names highlighted in {} are directly specified in the manifest. |
| 23 | + ", |
| 24 | + console::style("green").fg(Color::Green).bold(), |
| 25 | +))] |
| 26 | +pub struct Args { |
| 27 | + /// The environment to list packages for. |
| 28 | + #[arg(short, long)] |
| 29 | + pub environment: String, |
| 30 | + |
| 31 | + /// List only packages matching a regular expression |
| 32 | + #[arg()] |
| 33 | + pub regex: Option<String>, |
| 34 | + |
| 35 | + /// Invert tree and show what depends on a given package in the regex argument |
| 36 | + #[arg(short, long, requires = "regex")] |
| 37 | + pub invert: bool, |
| 38 | +} |
| 39 | + |
| 40 | +pub async fn execute(args: Args) -> miette::Result<()> { |
| 41 | + let project = Project::discover_or_create().await?; |
| 42 | + let stdout = std::io::stdout(); |
| 43 | + let mut handle = stdout.lock(); |
| 44 | + let env_name = EnvironmentName::from_str(args.environment.as_str())?; |
| 45 | + let environment = project |
| 46 | + .environment(&env_name) |
| 47 | + .wrap_err("Environment not found")?; |
| 48 | + // Contains all the dependencies under conda-meta |
| 49 | + let records = find_package_records( |
| 50 | + &EnvRoot::from_env() |
| 51 | + .await? |
| 52 | + .path() |
| 53 | + .join(env_name.as_str()) |
| 54 | + .join(consts::CONDA_META_DIR), |
| 55 | + ) |
| 56 | + .await?; |
| 57 | + |
| 58 | + let packages: HashMap<String, Package> = records |
| 59 | + .iter() |
| 60 | + .map(|record| { |
| 61 | + let name = record |
| 62 | + .repodata_record |
| 63 | + .package_record |
| 64 | + .name |
| 65 | + .as_normalized() |
| 66 | + .to_string(); |
| 67 | + let package = Package { |
| 68 | + name: name.clone(), |
| 69 | + version: record |
| 70 | + .repodata_record |
| 71 | + .package_record |
| 72 | + .version |
| 73 | + .version() |
| 74 | + .to_string(), |
| 75 | + dependencies: record |
| 76 | + .repodata_record |
| 77 | + .package_record |
| 78 | + .as_ref() |
| 79 | + .depends |
| 80 | + .iter() |
| 81 | + .filter_map(|dep| { |
| 82 | + dep.split([' ', '=']) |
| 83 | + .next() |
| 84 | + .map(|dep_name| dep_name.to_string()) |
| 85 | + }) |
| 86 | + .filter(|dep_name| !dep_name.starts_with("__")) // Filter virtual packages |
| 87 | + .unique() // A package may be listed with multiple constraints |
| 88 | + .collect(), |
| 89 | + needed_by: Vec::new(), |
| 90 | + source: PackageSource::Conda, // Global environments can only manage Conda packages |
| 91 | + }; |
| 92 | + (name, package) |
| 93 | + }) |
| 94 | + .collect(); |
| 95 | + |
| 96 | + let direct_deps = HashSet::from_iter( |
| 97 | + environment |
| 98 | + .dependencies |
| 99 | + .specs |
| 100 | + .iter() |
| 101 | + .map(|(name, _)| name.as_normalized().to_string()), |
| 102 | + ); |
| 103 | + if args.invert { |
| 104 | + print_inverted_dependency_tree( |
| 105 | + &mut handle, |
| 106 | + &build_reverse_dependency_map(&packages), |
| 107 | + &direct_deps, |
| 108 | + &args.regex, |
| 109 | + ) |
| 110 | + .wrap_err("Couldn't print the inverted dependency tree")?; |
| 111 | + } else { |
| 112 | + print_dependency_tree(&mut handle, &packages, &direct_deps, &args.regex) |
| 113 | + .wrap_err("Couldn't print the dependency tree")?; |
| 114 | + } |
| 115 | + Ok(()) |
| 116 | +} |
0 commit comments