Skip to content

Commit b5a1197

Browse files
committed
feat: add no-proc-macro for --edges option
1 parent 44596fa commit b5a1197

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub fn cli() -> App {
4343
"edges",
4444
"KINDS",
4545
"The kinds of dependencies to display \
46-
(features, normal, build, dev, all, no-dev, no-build, no-normal)",
46+
(features, normal, build, dev, all, \
47+
no-normal, no-build, no-dev, no-proc-macro)",
4748
)
4849
.short("e"),
4950
)
@@ -147,7 +148,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
147148
};
148149
let target = tree::Target::from_cli(targets);
149150

150-
let edge_kinds = parse_edge_kinds(config, args)?;
151+
let (edge_kinds, no_proc_macro) = parse_edge_kinds(config, args)?;
151152
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
152153

153154
let packages = args.packages_from_flags()?;
@@ -201,25 +202,47 @@ subtree of the package given to -p.\n\
201202
charset,
202203
format: args.value_of("format").unwrap().to_string(),
203204
graph_features,
205+
no_proc_macro,
204206
};
205207

206208
tree::build_and_print(&ws, &opts)?;
207209
Ok(())
208210
}
209211

210-
fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashSet<EdgeKind>> {
211-
let mut kinds: Vec<&str> = args
212-
.values_of("edges")
213-
.map_or_else(|| Vec::new(), |es| es.flat_map(|e| e.split(',')).collect());
214-
if args.is_present("no-dev-dependencies") {
215-
config
216-
.shell()
217-
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
218-
kinds.push("no-dev");
219-
}
220-
if kinds.is_empty() {
221-
kinds.extend(&["normal", "build", "dev"]);
222-
}
212+
/// Parses `--edges` option.
213+
///
214+
/// Returns a tuple of `EdgeKind` map and `no_proc_marco` flag.
215+
fn parse_edge_kinds(
216+
config: &Config,
217+
args: &ArgMatches<'_>,
218+
) -> CargoResult<(HashSet<EdgeKind>, bool)> {
219+
let (kinds, no_proc_macro) = {
220+
let mut no_proc_macro = false;
221+
let mut kinds = args.values_of("edges").map_or_else(
222+
|| Vec::new(),
223+
|es| {
224+
es.flat_map(|e| e.split(','))
225+
.filter(|e| {
226+
no_proc_macro = *e == "no-proc-macro";
227+
!no_proc_macro
228+
})
229+
.collect()
230+
},
231+
);
232+
233+
if args.is_present("no-dev-dependencies") {
234+
config
235+
.shell()
236+
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
237+
kinds.push("no-dev");
238+
}
239+
240+
if kinds.is_empty() {
241+
kinds.extend(&["normal", "build", "dev"]);
242+
}
243+
244+
(kinds, no_proc_macro)
245+
};
223246

224247
let mut result = HashSet::new();
225248
let insert_defaults = |result: &mut HashSet<EdgeKind>| {
@@ -231,7 +254,7 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
231254
bail!(
232255
"unknown edge kind `{}`, valid values are \
233256
\"normal\", \"build\", \"dev\", \
234-
\"no-normal\", \"no-build\", \"no-dev\", \
257+
\"no-normal\", \"no-build\", \"no-dev\", \"no-proc-macro\", \
235258
\"features\", or \"all\"",
236259
k
237260
)
@@ -244,13 +267,18 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
244267
"no-build" => result.remove(&EdgeKind::Dep(DepKind::Build)),
245268
"no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)),
246269
"features" => result.insert(EdgeKind::Feature),
247-
"normal" | "build" | "dev" | "all" => {
248-
bail!("`no-` dependency kinds cannot be mixed with other dependency kinds")
270+
k @ "normal" | k @ "build" | k @ "dev" | k @ "all" => {
271+
bail!(
272+
"`{}` dependency kind cannot be mixed with \
273+
\"no-normal\", \"no-build\", or \"no-dev\" \
274+
dependency kinds",
275+
k
276+
)
249277
}
250278
k => return unknown(k),
251279
};
252280
}
253-
return Ok(result);
281+
return Ok((result, no_proc_macro));
254282
}
255283
for kind in &kinds {
256284
match *kind {
@@ -276,5 +304,5 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
276304
if kinds.len() == 1 && kinds[0] == "features" {
277305
insert_defaults(&mut result);
278306
}
279-
Ok(result)
307+
Ok((result, no_proc_macro))
280308
}

src/cargo/ops/tree/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct TreeOptions {
4343
pub format: String,
4444
/// Includes features in the tree as separate nodes.
4545
pub graph_features: bool,
46+
/// Exculdes proc-macro dependencies.
47+
pub no_proc_macro: bool,
4648
}
4749

4850
#[derive(PartialEq)]
@@ -241,6 +243,7 @@ fn print(
241243
symbols,
242244
opts.prefix,
243245
opts.no_dedupe,
246+
opts.no_proc_macro,
244247
&mut visited_deps,
245248
&mut levels_continue,
246249
&mut print_stack,
@@ -259,6 +262,7 @@ fn print_node<'a>(
259262
symbols: &Symbols,
260263
prefix: Prefix,
261264
no_dedupe: bool,
265+
no_proc_macro: bool,
262266
visited_deps: &mut HashSet<usize>,
263267
levels_continue: &mut Vec<bool>,
264268
print_stack: &mut Vec<usize>,
@@ -316,6 +320,7 @@ fn print_node<'a>(
316320
symbols,
317321
prefix,
318322
no_dedupe,
323+
no_proc_macro,
319324
visited_deps,
320325
levels_continue,
321326
print_stack,
@@ -334,6 +339,7 @@ fn print_dependencies<'a>(
334339
symbols: &Symbols,
335340
prefix: Prefix,
336341
no_dedupe: bool,
342+
no_proc_macro: bool,
337343
visited_deps: &mut HashSet<usize>,
338344
levels_continue: &mut Vec<bool>,
339345
print_stack: &mut Vec<usize>,
@@ -362,7 +368,23 @@ fn print_dependencies<'a>(
362368
}
363369
}
364370

365-
let mut it = deps.iter().peekable();
371+
let mut it = deps
372+
.iter()
373+
.filter(|dep| {
374+
// Filter out proc-macro dependencies.
375+
if no_proc_macro {
376+
match graph.node(**dep) {
377+
&Node::Package { package_id, .. } => {
378+
!graph.package_for_id(package_id).proc_macro()
379+
}
380+
_ => true,
381+
}
382+
} else {
383+
true
384+
}
385+
})
386+
.peekable();
387+
366388
while let Some(dependency) = it.next() {
367389
levels_continue.push(it.peek().is_some());
368390
print_node(
@@ -373,6 +395,7 @@ fn print_dependencies<'a>(
373395
symbols,
374396
prefix,
375397
no_dedupe,
398+
no_proc_macro,
376399
visited_deps,
377400
levels_continue,
378401
print_stack,

0 commit comments

Comments
 (0)