Skip to content

Commit 3b1df3a

Browse files
committed
Change --no-dev-dependencies to --dep-kinds.
1 parent dd10b59 commit 3b1df3a

File tree

7 files changed

+287
-31
lines changed

7 files changed

+287
-31
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use crate::command_prelude::*;
2+
use anyhow::{bail, format_err};
3+
use cargo::core::dependency::DepKind;
24
use cargo::ops::tree;
5+
use cargo::util::CargoResult;
6+
use std::collections::HashSet;
37
use std::str::FromStr;
48

59
pub fn cli() -> App {
@@ -22,7 +26,19 @@ pub fn cli() -> App {
2226
.arg_target_triple(
2327
"Filter dependencies matching the given target-triple (default host platform)",
2428
)
25-
.arg(opt("no-dev-dependencies", "Skip dev dependencies"))
29+
.arg(
30+
Arg::with_name("no-dev-dependencies")
31+
.long("no-dev-dependencies")
32+
.hidden(true),
33+
)
34+
.arg(
35+
opt(
36+
"dep-kinds",
37+
"Dependency kinds to display \
38+
(normal, build, dev, no-dev, no-build, no-normal, all)",
39+
)
40+
.value_name("KINDS"),
41+
)
2642
.arg(opt("invert", "Invert the tree direction").short("i"))
2743
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
2844
.arg(
@@ -68,41 +84,43 @@ pub fn cli() -> App {
6884

6985
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
7086
if args.is_present("no-indent") {
71-
return Err(
72-
anyhow::format_err!("the --no-indent flag has been changed to --prefix=none").into(),
73-
);
87+
return Err(format_err!("the --no-indent flag has been changed to --prefix=none").into());
7488
}
7589
if args.is_present("prefix-depth") {
76-
return Err(anyhow::format_err!(
77-
"the --prefix-depth flag has been changed to --prefix=depth"
78-
)
79-
.into());
90+
return Err(
91+
format_err!("the --prefix-depth flag has been changed to --prefix=depth").into(),
92+
);
8093
}
8194
if args.is_present("all") {
82-
return Err(anyhow::format_err!(
95+
return Err(format_err!(
8396
"The `cargo tree` --all flag has been changed to --no-dedupe.\n\
8497
If you are looking to display all workspace members, use the --workspace flag."
8598
)
8699
.into());
87100
}
88101
if args.is_present("all-targets") {
89-
return Err(
90-
anyhow::format_err!("the --all-targets flag has been changed to --target=all").into(),
91-
);
102+
return Err(format_err!("the --all-targets flag has been changed to --target=all").into());
103+
}
104+
if args.is_present("no-dev-dependencies") {
105+
return Err(format_err!(
106+
"the --no-dev-dependencies flag has changed to --dep-kinds=no-dev"
107+
)
108+
.into());
92109
}
93110
let ws = args.workspace(config)?;
94111
let charset = tree::Charset::from_str(args.value_of("charset").unwrap())
95112
.map_err(|e| anyhow::anyhow!("{}", e))?;
96113
let prefix = tree::Prefix::from_str(args.value_of("prefix").unwrap())
97114
.map_err(|e| anyhow::anyhow!("{}", e))?;
98115
let target = tree::Target::from_cli(args.value_of("target"));
116+
let dep_kinds = parse_dep_kinds(args.value_of("dep-kinds"))?;
99117
let opts = tree::TreeOptions {
100118
features: values(args, "features"),
101119
all_features: args.is_present("all-features"),
102120
no_default_features: args.is_present("no-default-features"),
103121
packages: args.packages_from_flags()?,
104122
target,
105-
no_dev_dependencies: args.is_present("no-dev-dependencies"),
123+
dep_kinds,
106124
invert: args.is_present("invert"),
107125
prefix,
108126
no_dedupe: args.is_present("no-dedupe"),
@@ -115,3 +133,53 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
115133
tree::build_and_print(&ws, &opts)?;
116134
Ok(())
117135
}
136+
137+
fn parse_dep_kinds(kinds: Option<&str>) -> CargoResult<HashSet<DepKind>> {
138+
let kinds: Vec<&str> = kinds.unwrap_or("all").split(',').collect();
139+
let mut result = HashSet::new();
140+
let insert_all = |result: &mut HashSet<DepKind>| {
141+
result.insert(DepKind::Normal);
142+
result.insert(DepKind::Build);
143+
result.insert(DepKind::Development);
144+
};
145+
let unknown = |k| {
146+
bail!(
147+
"unknown dependency kind `{}`, valid values are \
148+
\"normal\", \"build\", \"dev\", \
149+
\"no-normal\", \"no-build\", \"no-dev\", \
150+
or \"all\"",
151+
k
152+
)
153+
};
154+
if kinds.iter().any(|k| k.starts_with("no-")) {
155+
insert_all(&mut result);
156+
for kind in &kinds {
157+
match *kind {
158+
"no-normal" => result.remove(&DepKind::Normal),
159+
"no-build" => result.remove(&DepKind::Build),
160+
"no-dev" => result.remove(&DepKind::Development),
161+
"normal" | "build" | "dev" | "all" => {
162+
bail!("`no-` dependency kinds cannot be mixed with other dependency kinds")
163+
}
164+
k => return unknown(k),
165+
};
166+
}
167+
return Ok(result);
168+
}
169+
for kind in kinds {
170+
match kind {
171+
"all" => insert_all(&mut result),
172+
"normal" => {
173+
result.insert(DepKind::Normal);
174+
}
175+
"build" => {
176+
result.insert(DepKind::Build);
177+
}
178+
"dev" => {
179+
result.insert(DepKind::Development);
180+
}
181+
k => return unknown(k),
182+
}
183+
}
184+
Ok(result)
185+
}

src/cargo/ops/tree/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn add_pkg(
333333
return false;
334334
}
335335
// Filter out dev-dependencies if requested.
336-
if opts.no_dev_dependencies && dep.kind() == DepKind::Development {
336+
if !opts.dep_kinds.contains(&dep.kind()) {
337337
return false;
338338
}
339339
if dep.is_optional() {

src/cargo/ops/tree/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub struct TreeOptions {
2525
pub packages: Packages,
2626
/// The platform to filter for.
2727
pub target: Target,
28-
pub no_dev_dependencies: bool,
28+
/// The dependency kinds to display.
29+
pub dep_kinds: HashSet<DepKind>,
2930
pub invert: bool,
3031
/// The style of prefix for each line.
3132
pub prefix: Prefix,
@@ -140,10 +141,10 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
140141
opts.all_features,
141142
!opts.no_default_features,
142143
);
143-
let has_dev = if opts.no_dev_dependencies {
144-
HasDevUnits::No
145-
} else {
144+
let has_dev = if opts.dep_kinds.contains(&DepKind::Development) {
146145
HasDevUnits::Yes
146+
} else {
147+
HasDevUnits::No
147148
};
148149
let ws_resolve = ops::resolve_ws_with_opts(
149150
ws,

src/doc/man/cargo-tree.adoc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ packages. You can then investigate if the package that depends on the
6363
duplicate with the older version can be updated to the newer version so that
6464
only one instance is built.
6565

66-
*--no-dev-dependencies*::
67-
Do not include dev-dependencies.
66+
*--dep-kinds* KINDS::
67+
The dependency kinds to display. Takes a comma separated list of values:
68+
69+
- `all` (default) — Show all dependency kinds.
70+
- `normal` — Show normal dependencies.
71+
- `build` — Show build dependencies.
72+
- `dev` — Show development dependencies.
73+
- `no-normal` — Do not include normal dependencies.
74+
- `no-build` — Do not include build dependencies.
75+
- `no-dev` — Do not include development dependencies.
76+
+
77+
The `no-` prefixed options cannot be mixed with the other kinds.
6878

6979
*--target* _TRIPLE_::
7080
Filter dependencies matching the given target-triple.

src/doc/man/generated/cargo-tree.html

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,37 @@ <h3 id="cargo_tree_tree_options">Tree Options</h3>
7474
only one instance is built.</p>
7575
</div>
7676
</dd>
77-
<dt class="hdlist1"><strong>--no-dev-dependencies</strong></dt>
77+
<dt class="hdlist1"><strong>--dep-kinds</strong> KINDS</dt>
7878
<dd>
79-
<p>Do not include dev-dependencies.</p>
79+
<p>The dependency kinds to display. Takes a comma separated list of values:</p>
80+
<div class="ulist">
81+
<ul>
82+
<li>
83+
<p><code>all</code> (default) — Show all dependency kinds.</p>
84+
</li>
85+
<li>
86+
<p><code>normal</code> — Show normal dependencies.</p>
87+
</li>
88+
<li>
89+
<p><code>build</code> — Show build dependencies.</p>
90+
</li>
91+
<li>
92+
<p><code>dev</code> — Show development dependencies.</p>
93+
</li>
94+
<li>
95+
<p><code>no-normal</code> — Do not include normal dependencies.</p>
96+
</li>
97+
<li>
98+
<p><code>no-build</code> — Do not include build dependencies.</p>
99+
</li>
100+
<li>
101+
<p><code>no-dev</code> — Do not include development dependencies.</p>
102+
<div class="paragraph">
103+
<p>The <code>no-</code> prefixed options cannot be mixed with the other kinds.</p>
104+
</div>
105+
</li>
106+
</ul>
107+
</div>
80108
</dd>
81109
<dt class="hdlist1"><strong>--target</strong> <em>TRIPLE</em></dt>
82110
<dd>

src/etc/man/cargo-tree.1

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,88 @@ duplicate with the older version can be updated to the newer version so that
8787
only one instance is built.
8888
.RE
8989
.sp
90-
\fB\-\-no\-dev\-dependencies\fP
90+
\fB\-\-dep\-kinds\fP KINDS
9191
.RS 4
92-
Do not include dev\-dependencies.
92+
The dependency kinds to display. Takes a comma separated list of values:
93+
.sp
94+
.RS 4
95+
.ie n \{\
96+
\h'-04'\(bu\h'+03'\c
97+
.\}
98+
.el \{\
99+
. sp -1
100+
. IP \(bu 2.3
101+
.\}
102+
\fBall\fP (default) — Show all dependency kinds.
103+
.RE
104+
.sp
105+
.RS 4
106+
.ie n \{\
107+
\h'-04'\(bu\h'+03'\c
108+
.\}
109+
.el \{\
110+
. sp -1
111+
. IP \(bu 2.3
112+
.\}
113+
\fBnormal\fP — Show normal dependencies.
114+
.RE
115+
.sp
116+
.RS 4
117+
.ie n \{\
118+
\h'-04'\(bu\h'+03'\c
119+
.\}
120+
.el \{\
121+
. sp -1
122+
. IP \(bu 2.3
123+
.\}
124+
\fBbuild\fP — Show build dependencies.
125+
.RE
126+
.sp
127+
.RS 4
128+
.ie n \{\
129+
\h'-04'\(bu\h'+03'\c
130+
.\}
131+
.el \{\
132+
. sp -1
133+
. IP \(bu 2.3
134+
.\}
135+
\fBdev\fP — Show development dependencies.
136+
.RE
137+
.sp
138+
.RS 4
139+
.ie n \{\
140+
\h'-04'\(bu\h'+03'\c
141+
.\}
142+
.el \{\
143+
. sp -1
144+
. IP \(bu 2.3
145+
.\}
146+
\fBno\-normal\fP — Do not include normal dependencies.
147+
.RE
148+
.sp
149+
.RS 4
150+
.ie n \{\
151+
\h'-04'\(bu\h'+03'\c
152+
.\}
153+
.el \{\
154+
. sp -1
155+
. IP \(bu 2.3
156+
.\}
157+
\fBno\-build\fP — Do not include build dependencies.
158+
.RE
159+
.sp
160+
.RS 4
161+
.ie n \{\
162+
\h'-04'\(bu\h'+03'\c
163+
.\}
164+
.el \{\
165+
. sp -1
166+
. IP \(bu 2.3
167+
.\}
168+
\fBno\-dev\fP — Do not include development dependencies.
169+
.sp
170+
The \fBno\-\fP prefixed options cannot be mixed with the other kinds.
171+
.RE
93172
.RE
94173
.sp
95174
\fB\-\-target\fP \fITRIPLE\fP

0 commit comments

Comments
 (0)