|
| 1 | +use crate::command_prelude::*; |
| 2 | +use anyhow::{bail, format_err}; |
| 3 | +use cargo::core::dependency::DepKind; |
| 4 | +use cargo::ops::tree::{self, EdgeKind}; |
| 5 | +use cargo::ops::Packages; |
| 6 | +use cargo::util::CargoResult; |
| 7 | +use std::collections::HashSet; |
| 8 | +use std::str::FromStr; |
| 9 | + |
| 10 | +pub fn cli() -> App { |
| 11 | + subcommand("tree") |
| 12 | + .about("Display a tree visualization of a dependency graph") |
| 13 | + .arg(opt("quiet", "Suppress status messages").short("q")) |
| 14 | + .arg_manifest_path() |
| 15 | + .arg_package_spec_no_all( |
| 16 | + "Package to be used as the root of the tree", |
| 17 | + "Display the tree for all packages in the workspace", |
| 18 | + "Exclude specific workspace members", |
| 19 | + ) |
| 20 | + .arg(Arg::with_name("all").long("all").short("a").hidden(true)) |
| 21 | + .arg( |
| 22 | + Arg::with_name("all-targets") |
| 23 | + .long("all-targets") |
| 24 | + .hidden(true), |
| 25 | + ) |
| 26 | + .arg_features() |
| 27 | + .arg_target_triple( |
| 28 | + "Filter dependencies matching the given target-triple (default host platform)", |
| 29 | + ) |
| 30 | + .arg( |
| 31 | + Arg::with_name("no-dev-dependencies") |
| 32 | + .long("no-dev-dependencies") |
| 33 | + .hidden(true), |
| 34 | + ) |
| 35 | + .arg( |
| 36 | + multi_opt( |
| 37 | + "edges", |
| 38 | + "KINDS", |
| 39 | + "The kinds of dependencies to display \ |
| 40 | + (features, normal, build, dev, all, no-dev, no-build, no-normal)", |
| 41 | + ) |
| 42 | + .short("e"), |
| 43 | + ) |
| 44 | + .arg( |
| 45 | + optional_multi_opt( |
| 46 | + "invert", |
| 47 | + "SPEC", |
| 48 | + "Invert the tree direction and focus on the given package", |
| 49 | + ) |
| 50 | + .short("i"), |
| 51 | + ) |
| 52 | + .arg(Arg::with_name("no-indent").long("no-indent").hidden(true)) |
| 53 | + .arg( |
| 54 | + Arg::with_name("prefix-depth") |
| 55 | + .long("prefix-depth") |
| 56 | + .hidden(true), |
| 57 | + ) |
| 58 | + .arg( |
| 59 | + opt( |
| 60 | + "prefix", |
| 61 | + "Change the prefix (indentation) of how each entry is displayed", |
| 62 | + ) |
| 63 | + .value_name("PREFIX") |
| 64 | + .possible_values(&["depth", "indent", "none"]) |
| 65 | + .default_value("indent"), |
| 66 | + ) |
| 67 | + .arg(opt( |
| 68 | + "no-dedupe", |
| 69 | + "Do not de-duplicate (repeats all shared dependencies)", |
| 70 | + )) |
| 71 | + .arg( |
| 72 | + opt( |
| 73 | + "duplicates", |
| 74 | + "Show only dependencies which come in multiple versions (implies -i)", |
| 75 | + ) |
| 76 | + .short("d") |
| 77 | + .alias("duplicate"), |
| 78 | + ) |
| 79 | + .arg( |
| 80 | + opt("charset", "Character set to use in output: utf8, ascii") |
| 81 | + .value_name("CHARSET") |
| 82 | + .possible_values(&["utf8", "ascii"]) |
| 83 | + .default_value("utf8"), |
| 84 | + ) |
| 85 | + .arg( |
| 86 | + opt("format", "Format string used for printing dependencies") |
| 87 | + .value_name("FORMAT") |
| 88 | + .short("f") |
| 89 | + .default_value("{p}"), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { |
| 94 | + let prefix = if args.is_present("no-indent") { |
| 95 | + config |
| 96 | + .shell() |
| 97 | + .warn("the --no-indent flag has been changed to --prefix=none")?; |
| 98 | + "none" |
| 99 | + } else if args.is_present("prefix-depth") { |
| 100 | + config |
| 101 | + .shell() |
| 102 | + .warn("the --prefix-depth flag has been changed to --prefix=depth")?; |
| 103 | + "depth" |
| 104 | + } else { |
| 105 | + args.value_of("prefix").unwrap() |
| 106 | + }; |
| 107 | + let prefix = tree::Prefix::from_str(prefix).map_err(|e| anyhow::anyhow!("{}", e))?; |
| 108 | + |
| 109 | + if args.is_present("all") { |
| 110 | + return Err(format_err!( |
| 111 | + "The `cargo tree` --all flag has been changed to --no-dedupe.\n\ |
| 112 | + If you are looking to display all workspace members, use the --workspace flag." |
| 113 | + ) |
| 114 | + .into()); |
| 115 | + } |
| 116 | + |
| 117 | + let target = if args.is_present("all-targets") { |
| 118 | + config |
| 119 | + .shell() |
| 120 | + .warn("the --all-targets flag has been changed to --target=all")?; |
| 121 | + Some("all") |
| 122 | + } else { |
| 123 | + args.value_of("target") |
| 124 | + }; |
| 125 | + let target = tree::Target::from_cli(target); |
| 126 | + |
| 127 | + let edge_kinds = parse_edge_kinds(config, args)?; |
| 128 | + let graph_features = edge_kinds.contains(&EdgeKind::Feature); |
| 129 | + |
| 130 | + let packages = args.packages_from_flags()?; |
| 131 | + let mut invert = args |
| 132 | + .values_of("invert") |
| 133 | + .map_or_else(|| Vec::new(), |is| is.map(|s| s.to_string()).collect()); |
| 134 | + if args.is_present_with_zero_values("invert") { |
| 135 | + match &packages { |
| 136 | + Packages::Packages(ps) => { |
| 137 | + // Backwards compatibility with old syntax of `cargo tree -i -p foo`. |
| 138 | + invert.extend(ps.clone()); |
| 139 | + } |
| 140 | + _ => { |
| 141 | + return Err(format_err!( |
| 142 | + "The `-i` flag requires a package name.\n\ |
| 143 | +\n\ |
| 144 | +The `-i` flag is used to inspect the reverse dependencies of a specific\n\ |
| 145 | +package. It will invert the tree and display the packages that depend on the\n\ |
| 146 | +given package.\n\ |
| 147 | +\n\ |
| 148 | +Note that in a workspace, by default it will only display the package's\n\ |
| 149 | +reverse dependencies inside the tree of the workspace member in the current\n\ |
| 150 | +directory. The --workspace flag can be used to extend it so that it will show\n\ |
| 151 | +the package's reverse dependencies across the entire workspace. The -p flag\n\ |
| 152 | +can be used to display the package's reverse dependencies only with the\n\ |
| 153 | +subtree of the package given to -p.\n\ |
| 154 | +" |
| 155 | + ) |
| 156 | + .into()); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + let ws = args.workspace(config)?; |
| 162 | + let charset = tree::Charset::from_str(args.value_of("charset").unwrap()) |
| 163 | + .map_err(|e| anyhow::anyhow!("{}", e))?; |
| 164 | + let opts = tree::TreeOptions { |
| 165 | + features: values(args, "features"), |
| 166 | + all_features: args.is_present("all-features"), |
| 167 | + no_default_features: args.is_present("no-default-features"), |
| 168 | + packages, |
| 169 | + target, |
| 170 | + edge_kinds, |
| 171 | + invert, |
| 172 | + prefix, |
| 173 | + no_dedupe: args.is_present("no-dedupe"), |
| 174 | + duplicates: args.is_present("duplicates"), |
| 175 | + charset, |
| 176 | + format: args.value_of("format").unwrap().to_string(), |
| 177 | + graph_features, |
| 178 | + }; |
| 179 | + |
| 180 | + tree::build_and_print(&ws, &opts)?; |
| 181 | + Ok(()) |
| 182 | +} |
| 183 | + |
| 184 | +fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashSet<EdgeKind>> { |
| 185 | + let mut kinds: Vec<&str> = args |
| 186 | + .values_of("edges") |
| 187 | + .map_or_else(|| Vec::new(), |es| es.flat_map(|e| e.split(',')).collect()); |
| 188 | + if args.is_present("no-dev-dependencies") { |
| 189 | + config |
| 190 | + .shell() |
| 191 | + .warn("the --no-dev-dependencies flag has changed to -e=no-dev")?; |
| 192 | + kinds.push("no-dev"); |
| 193 | + } |
| 194 | + if kinds.len() == 0 { |
| 195 | + kinds.extend(&["normal", "build", "dev"]); |
| 196 | + } |
| 197 | + |
| 198 | + let mut result = HashSet::new(); |
| 199 | + let insert_defaults = |result: &mut HashSet<EdgeKind>| { |
| 200 | + result.insert(EdgeKind::Dep(DepKind::Normal)); |
| 201 | + result.insert(EdgeKind::Dep(DepKind::Build)); |
| 202 | + result.insert(EdgeKind::Dep(DepKind::Development)); |
| 203 | + }; |
| 204 | + let unknown = |k| { |
| 205 | + bail!( |
| 206 | + "unknown edge kind `{}`, valid values are \ |
| 207 | + \"normal\", \"build\", \"dev\", \ |
| 208 | + \"no-normal\", \"no-build\", \"no-dev\", \ |
| 209 | + \"features\", or \"all\"", |
| 210 | + k |
| 211 | + ) |
| 212 | + }; |
| 213 | + if kinds.iter().any(|k| k.starts_with("no-")) { |
| 214 | + insert_defaults(&mut result); |
| 215 | + for kind in &kinds { |
| 216 | + match *kind { |
| 217 | + "no-normal" => result.remove(&EdgeKind::Dep(DepKind::Normal)), |
| 218 | + "no-build" => result.remove(&EdgeKind::Dep(DepKind::Build)), |
| 219 | + "no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)), |
| 220 | + "features" => result.insert(EdgeKind::Feature), |
| 221 | + "normal" | "build" | "dev" | "all" => { |
| 222 | + bail!("`no-` dependency kinds cannot be mixed with other dependency kinds") |
| 223 | + } |
| 224 | + k => return unknown(k), |
| 225 | + }; |
| 226 | + } |
| 227 | + return Ok(result); |
| 228 | + } |
| 229 | + for kind in &kinds { |
| 230 | + match *kind { |
| 231 | + "all" => { |
| 232 | + insert_defaults(&mut result); |
| 233 | + result.insert(EdgeKind::Feature); |
| 234 | + } |
| 235 | + "features" => { |
| 236 | + result.insert(EdgeKind::Feature); |
| 237 | + } |
| 238 | + "normal" => { |
| 239 | + result.insert(EdgeKind::Dep(DepKind::Normal)); |
| 240 | + } |
| 241 | + "build" => { |
| 242 | + result.insert(EdgeKind::Dep(DepKind::Build)); |
| 243 | + } |
| 244 | + "dev" => { |
| 245 | + result.insert(EdgeKind::Dep(DepKind::Development)); |
| 246 | + } |
| 247 | + k => return unknown(k), |
| 248 | + } |
| 249 | + } |
| 250 | + if kinds.len() == 1 && kinds[0] == "features" { |
| 251 | + insert_defaults(&mut result); |
| 252 | + } |
| 253 | + Ok(result) |
| 254 | +} |
0 commit comments