Skip to content

Commit 66a6737

Browse files
committed
Auto merge of #9663 - nebkor:master, r=alexcrichton
Add format option to `cargo tree` to print the lib_name Adds a way to have `cargo tree` display the name of the library inside a package dependency, which can differ from the name of the package. Updates the `tree::format` test to test for its proper behavior. Closes #9659
2 parents f8ba1cb + af355f0 commit 66a6737

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

src/cargo/ops/tree/format/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use std::fmt;
2+
3+
use anyhow::{bail, Error};
4+
15
use self::parse::{Parser, RawChunk};
26
use super::{Graph, Node};
3-
use anyhow::{bail, Error};
4-
use std::fmt;
57

68
mod parse;
79

@@ -11,6 +13,7 @@ enum Chunk {
1113
License,
1214
Repository,
1315
Features,
16+
LibName,
1417
}
1518

1619
pub struct Pattern(Vec<Chunk>);
@@ -26,6 +29,7 @@ impl Pattern {
2629
RawChunk::Argument("l") => Chunk::License,
2730
RawChunk::Argument("r") => Chunk::Repository,
2831
RawChunk::Argument("f") => Chunk::Features,
32+
RawChunk::Argument("lib") => Chunk::LibName,
2933
RawChunk::Argument(a) => {
3034
bail!("unsupported pattern `{}`", a);
3135
}
@@ -97,6 +101,16 @@ impl<'a> fmt::Display for Display<'a> {
97101
Chunk::Features => {
98102
write!(fmt, "{}", features.join(","))?;
99103
}
104+
Chunk::LibName => {
105+
if let Some(target) = package
106+
.manifest()
107+
.targets()
108+
.iter()
109+
.find(|target| target.is_lib())
110+
{
111+
write!(fmt, "{}", target.crate_name())?;
112+
}
113+
}
100114
}
101115
}
102116
}

src/doc/man/cargo-tree.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ strings will be replaced with the corresponding value:
145145
- `{l}` — The package license.
146146
- `{r}` — The package repository URL.
147147
- `{f}` — Comma-separated list of package features that are enabled.
148+
- `{lib}` — The name, as used in a `use` statement, of the package's library.
148149
{{/option}}
149150

150151
{{#option "`--prefix` _prefix_" }}

src/doc/man/generated_txt/cargo-tree.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ OPTIONS
138138
o {f} — Comma-separated list of package features that are
139139
enabled.
140140

141+
o {lib} — The name, as used in a use statement, of the package's
142+
library.
143+
141144
--prefix prefix
142145
Sets how each line is displayed. The prefix value can be one of:
143146

src/doc/src/commands/cargo-tree.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ strings will be replaced with the corresponding value:</p>
145145
<li><code>{l}</code> — The package license.</li>
146146
<li><code>{r}</code> — The package repository URL.</li>
147147
<li><code>{f}</code> — Comma-separated list of package features that are enabled.</li>
148+
<li><code>{lib}</code> — The name, as used in a <code>use</code> statement, of the package's library.</li>
148149
</ul></dd>
149150

150151

src/etc/man/cargo-tree.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ strings will be replaced with the corresponding value:
186186
.RS 4
187187
\h'-04'\(bu\h'+02'\fB{f}\fR \[em] Comma\-separated list of package features that are enabled.
188188
.RE
189+
.sp
190+
.RS 4
191+
\h'-04'\(bu\h'+02'\fB{lib}\fR \[em] The name, as used in a \fBuse\fR statement, of the package's library.
192+
.RE
189193
.RE
190194
.sp
191195
\fB\-\-prefix\fR \fIprefix\fR

tests/testsuite/tree.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,23 @@ foo v0.1.0 ([..]/foo)
10091009
#[cargo_test]
10101010
fn format() {
10111011
Package::new("dep", "1.0.0").publish();
1012+
Package::new("other-dep", "1.0.0").publish();
1013+
1014+
Package::new("dep_that_is_awesome", "1.0.0")
1015+
.file(
1016+
"Cargo.toml",
1017+
r#"
1018+
[package]
1019+
name = "dep_that_is_awesome"
1020+
version = "1.0.0"
1021+
1022+
[lib]
1023+
name = "awesome_dep"
1024+
"#,
1025+
)
1026+
.file("src/lib.rs", "pub struct Straw;")
1027+
.publish();
1028+
10121029
let p = project()
10131030
.file(
10141031
"Cargo.toml",
@@ -1021,14 +1038,17 @@ fn format() {
10211038
10221039
[dependencies]
10231040
dep = {version="1.0", optional=true}
1041+
other-dep = {version="1.0", optional=true}
1042+
dep_that_is_awesome = {version="1.0", optional=true}
1043+
10241044
10251045
[features]
10261046
default = ["foo"]
10271047
foo = ["bar"]
10281048
bar = []
10291049
"#,
10301050
)
1031-
.file("src/lib.rs", "")
1051+
.file("src/main.rs", "")
10321052
.build();
10331053

10341054
p.cargo("tree --format <<<{p}>>>")
@@ -1065,8 +1085,21 @@ Caused by:
10651085
.arg("{p} [{f}]")
10661086
.with_stdout(
10671087
"\
1068-
foo v0.1.0 ([..]/foo) [bar,default,dep,foo]
1069-
└── dep v1.0.0 []
1088+
foo v0.1.0 ([..]/foo) [bar,default,dep,dep_that_is_awesome,foo,other-dep]
1089+
├── dep v1.0.0 []
1090+
├── dep_that_is_awesome v1.0.0 []
1091+
└── other-dep v1.0.0 []
1092+
",
1093+
)
1094+
.run();
1095+
1096+
p.cargo("tree")
1097+
.arg("--features=other-dep,dep_that_is_awesome")
1098+
.arg("--format={lib}")
1099+
.with_stdout(
1100+
"
1101+
├── awesome_dep
1102+
└── other_dep
10701103
",
10711104
)
10721105
.run();

0 commit comments

Comments
 (0)