-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add cargo tree command.
#8062
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
Merged
Merged
Add cargo tree command.
#8062
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
96a3937
Add `cargo tree` command.
ehuss feaa312
Update `cargo tree` from review comments.
ehuss b1fd8e9
Try to clarify how node edges works.
ehuss 9dc56c6
Deduplicate nodes across roots.
ehuss 6868215
Don't show (*) deduplicate if a node doesn't have any dependencies to…
ehuss e1c95e2
Fix tree format parsing for bare `}`
ehuss a9ff02e
Change --no-indent and --prefix-depth to a single --prefix option.
ehuss ec5e297
Change --no-filter-targets to --target=all.
ehuss afdcab7
Catch and display error about the --all flag.
ehuss dd10b59
Add hint about removal of --all-targets
ehuss 3b1df3a
Change --no-dev-dependencies to --dep-kinds.
ehuss 5ccc5e0
Change old flags to warnings instead of errors.
ehuss 96ff434
Rename --dep-kinds to --edges and fold in --graph-features.
ehuss 7c40344
Switch --invert to take the package name as an argument.
ehuss 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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| use crate::command_prelude::*; | ||
| use cargo::ops::tree; | ||
| use std::str::FromStr; | ||
|
|
||
| pub fn cli() -> App { | ||
| subcommand("tree") | ||
| .about("Display a tree visualization of a dependency graph") | ||
| .arg(opt("quiet", "Suppress status messages").short("q")) | ||
| .arg_manifest_path() | ||
| .arg_package_spec_no_all( | ||
| "Package to be used as the root of the tree", | ||
| "Display the tree for all packages in the workspace", | ||
| "Exclude specific workspace members", | ||
| ) | ||
| .arg_features() | ||
| .arg_target_triple( | ||
| "Filter dependencies matching the given target-triple (default host platform)", | ||
| ) | ||
| .arg(opt( | ||
| "no-filter-targets", | ||
| "Return dependencies for all targets", | ||
| )) | ||
| .arg(opt("no-dev-dependencies", "Skip dev dependencies")) | ||
| .arg(opt("invert", "Invert the tree direction").short("i")) | ||
| .arg(opt( | ||
| "no-indent", | ||
| "Display the dependencies as a list (rather than a tree)", | ||
| )) | ||
| .arg(opt( | ||
| "prefix-depth", | ||
| "Display the dependencies as a list (rather than a tree), but prefixed with the depth", | ||
| )) | ||
| .arg(opt( | ||
| "no-dedupe", | ||
| "Do not de-duplicate (repeats all shared dependencies)", | ||
| )) | ||
| .arg( | ||
| opt( | ||
| "duplicates", | ||
| "Show only dependencies which come in multiple versions (implies -i)", | ||
| ) | ||
| .short("d") | ||
| .alias("duplicate"), | ||
| ) | ||
| .arg( | ||
| opt("charset", "Character set to use in output: utf8, ascii") | ||
| .value_name("CHARSET") | ||
| .possible_values(&["utf8", "ascii"]) | ||
| .default_value("utf8"), | ||
| ) | ||
| .arg( | ||
| opt("format", "Format string used for printing dependencies") | ||
| .value_name("FORMAT") | ||
| .short("f") | ||
| .default_value("{p}"), | ||
| ) | ||
| .arg(opt("graph-features", "Include features in the tree")) | ||
| } | ||
|
|
||
| pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { | ||
| let ws = args.workspace(config)?; | ||
| let charset = tree::Charset::from_str(args.value_of("charset").unwrap()) | ||
| .map_err(|e| anyhow::anyhow!("{}", e))?; | ||
| let opts = tree::TreeOptions { | ||
| features: values(args, "features"), | ||
| all_features: args.is_present("all-features"), | ||
| no_default_features: args.is_present("no-default-features"), | ||
| packages: args.packages_from_flags()?, | ||
| target: args.target(), | ||
| no_filter_targets: args.is_present("no-filter-targets"), | ||
| no_dev_dependencies: args.is_present("no-dev-dependencies"), | ||
| invert: args.is_present("invert"), | ||
| no_indent: args.is_present("no-indent"), | ||
| prefix_depth: args.is_present("prefix-depth"), | ||
| no_dedupe: args.is_present("no-dedupe"), | ||
| duplicates: args.is_present("duplicates"), | ||
| charset, | ||
| format: args.value_of("format").unwrap().to_string(), | ||
| graph_features: args.is_present("graph-features"), | ||
| }; | ||
|
|
||
| tree::build_and_print(&ws, &opts)?; | ||
| Ok(()) | ||
| } |
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
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
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
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 |
|---|---|---|
|
|
@@ -48,4 +48,5 @@ mod fix; | |
| mod lockfile; | ||
| mod registry; | ||
| mod resolve; | ||
| pub mod tree; | ||
| mod vendor; | ||
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 |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| use self::parse::{Parser, RawChunk}; | ||
| use super::{Graph, Node}; | ||
| use anyhow::{anyhow, Error}; | ||
| use std::fmt; | ||
|
|
||
| mod parse; | ||
|
|
||
| enum Chunk { | ||
| Raw(String), | ||
| Package, | ||
| License, | ||
| Repository, | ||
| Features, | ||
| } | ||
|
|
||
| pub struct Pattern(Vec<Chunk>); | ||
|
|
||
| impl Pattern { | ||
| pub fn new(format: &str) -> Result<Pattern, Error> { | ||
| let mut chunks = vec![]; | ||
|
|
||
| for raw in Parser::new(format) { | ||
| let chunk = match raw { | ||
| RawChunk::Text(text) => Chunk::Raw(text.to_owned()), | ||
| RawChunk::Argument("p") => Chunk::Package, | ||
| RawChunk::Argument("l") => Chunk::License, | ||
| RawChunk::Argument("r") => Chunk::Repository, | ||
| RawChunk::Argument("f") => Chunk::Features, | ||
| RawChunk::Argument(a) => { | ||
| return Err(anyhow!("unsupported pattern `{}`", a)); | ||
| } | ||
| RawChunk::Error(err) => return Err(anyhow!("{}", err)), | ||
| }; | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| Ok(Pattern(chunks)) | ||
| } | ||
|
|
||
| pub fn display<'a>(&'a self, graph: &'a Graph<'a>, node_index: usize) -> Display<'a> { | ||
| Display { | ||
| pattern: self, | ||
| graph, | ||
| node_index, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct Display<'a> { | ||
| pattern: &'a Pattern, | ||
| graph: &'a Graph<'a>, | ||
| node_index: usize, | ||
| } | ||
|
|
||
| impl<'a> fmt::Display for Display<'a> { | ||
| fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let node = self.graph.node(self.node_index); | ||
| match node { | ||
| Node::Package { | ||
| package_id, | ||
| features, | ||
| .. | ||
| } => { | ||
| let package = self.graph.package_for_id(*package_id); | ||
| for chunk in &self.pattern.0 { | ||
| match *chunk { | ||
| Chunk::Raw(ref s) => fmt.write_str(s)?, | ||
|
alexcrichton marked this conversation as resolved.
Outdated
|
||
| Chunk::Package => { | ||
| write!(fmt, "{} v{}", package.name(), package.version())?; | ||
|
|
||
| let source_id = package.package_id().source_id(); | ||
| if !source_id.is_default_registry() { | ||
| write!(fmt, " ({})", source_id)?; | ||
| } | ||
| } | ||
| Chunk::License => { | ||
| if let Some(ref license) = package.manifest().metadata().license { | ||
| write!(fmt, "{}", license)?; | ||
| } | ||
| } | ||
| Chunk::Repository => { | ||
| if let Some(ref repository) = package.manifest().metadata().repository { | ||
| write!(fmt, "{}", repository)?; | ||
| } | ||
| } | ||
| Chunk::Features => { | ||
| write!(fmt, "{}", features.join(","))?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Node::Feature { name, node_index } => { | ||
| let for_node = self.graph.node(*node_index); | ||
| match for_node { | ||
| Node::Package { package_id, .. } => { | ||
| write!(fmt, "{} feature \"{}\"", package_id.name(), name)?; | ||
| if self.graph.is_cli_feature(self.node_index) { | ||
| write!(fmt, " (command-line)")?; | ||
| } | ||
| } | ||
| _ => panic!("unexpected feature node {:?}", for_node), | ||
|
alexcrichton marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.