Skip to content

Commit

Permalink
Auto merge of #9663 - nebkor:master, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 14, 2021
2 parents f8ba1cb + af355f0 commit 66a6737
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/cargo/ops/tree/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt;

use anyhow::{bail, Error};

use self::parse::{Parser, RawChunk};
use super::{Graph, Node};
use anyhow::{bail, Error};
use std::fmt;

mod parse;

Expand All @@ -11,6 +13,7 @@ enum Chunk {
License,
Repository,
Features,
LibName,
}

pub struct Pattern(Vec<Chunk>);
Expand All @@ -26,6 +29,7 @@ impl Pattern {
RawChunk::Argument("l") => Chunk::License,
RawChunk::Argument("r") => Chunk::Repository,
RawChunk::Argument("f") => Chunk::Features,
RawChunk::Argument("lib") => Chunk::LibName,
RawChunk::Argument(a) => {
bail!("unsupported pattern `{}`", a);
}
Expand Down Expand Up @@ -97,6 +101,16 @@ impl<'a> fmt::Display for Display<'a> {
Chunk::Features => {
write!(fmt, "{}", features.join(","))?;
}
Chunk::LibName => {
if let Some(target) = package
.manifest()
.targets()
.iter()
.find(|target| target.is_lib())
{
write!(fmt, "{}", target.crate_name())?;
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/doc/man/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ strings will be replaced with the corresponding value:
- `{l}` — The package license.
- `{r}` — The package repository URL.
- `{f}` — Comma-separated list of package features that are enabled.
- `{lib}` — The name, as used in a `use` statement, of the package's library.
{{/option}}

{{#option "`--prefix` _prefix_" }}
Expand Down
3 changes: 3 additions & 0 deletions src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ OPTIONS
o {f} — Comma-separated list of package features that are
enabled.

o {lib} — The name, as used in a use statement, of the package's
library.

--prefix prefix
Sets how each line is displayed. The prefix value can be one of:

Expand Down
1 change: 1 addition & 0 deletions src/doc/src/commands/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ strings will be replaced with the corresponding value:</p>
<li><code>{l}</code> — The package license.</li>
<li><code>{r}</code> — The package repository URL.</li>
<li><code>{f}</code> — Comma-separated list of package features that are enabled.</li>
<li><code>{lib}</code> — The name, as used in a <code>use</code> statement, of the package's library.</li>
</ul></dd>


Expand Down
4 changes: 4 additions & 0 deletions src/etc/man/cargo-tree.1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ strings will be replaced with the corresponding value:
.RS 4
\h'-04'\(bu\h'+02'\fB{f}\fR \[em] Comma\-separated list of package features that are enabled.
.RE
.sp
.RS 4
\h'-04'\(bu\h'+02'\fB{lib}\fR \[em] The name, as used in a \fBuse\fR statement, of the package's library.
.RE
.RE
.sp
\fB\-\-prefix\fR \fIprefix\fR
Expand Down
39 changes: 36 additions & 3 deletions tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,23 @@ foo v0.1.0 ([..]/foo)
#[cargo_test]
fn format() {
Package::new("dep", "1.0.0").publish();
Package::new("other-dep", "1.0.0").publish();

Package::new("dep_that_is_awesome", "1.0.0")
.file(
"Cargo.toml",
r#"
[package]
name = "dep_that_is_awesome"
version = "1.0.0"
[lib]
name = "awesome_dep"
"#,
)
.file("src/lib.rs", "pub struct Straw;")
.publish();

let p = project()
.file(
"Cargo.toml",
Expand All @@ -1021,14 +1038,17 @@ fn format() {
[dependencies]
dep = {version="1.0", optional=true}
other-dep = {version="1.0", optional=true}
dep_that_is_awesome = {version="1.0", optional=true}
[features]
default = ["foo"]
foo = ["bar"]
bar = []
"#,
)
.file("src/lib.rs", "")
.file("src/main.rs", "")
.build();

p.cargo("tree --format <<<{p}>>>")
Expand Down Expand Up @@ -1065,8 +1085,21 @@ Caused by:
.arg("{p} [{f}]")
.with_stdout(
"\
foo v0.1.0 ([..]/foo) [bar,default,dep,foo]
└── dep v1.0.0 []
foo v0.1.0 ([..]/foo) [bar,default,dep,dep_that_is_awesome,foo,other-dep]
├── dep v1.0.0 []
├── dep_that_is_awesome v1.0.0 []
└── other-dep v1.0.0 []
",
)
.run();

p.cargo("tree")
.arg("--features=other-dep,dep_that_is_awesome")
.arg("--format={lib}")
.with_stdout(
"
├── awesome_dep
└── other_dep
",
)
.run();
Expand Down

0 comments on commit 66a6737

Please sign in to comment.