#cargo-subcommand #graph #graphviz #dot #subcommand #cargo

app cargo-graph

Cargo 子命令,用于构建依赖关系图的 GraphViz DOT 文件

8 个版本

使用旧的 Rust 2015

0.3.1 2017年11月7日
0.3.0 2016年9月10日
0.2.2 2016年3月23日
0.2.0 2015年12月11日
0.1.4 2015年11月15日

#41 in #graphviz

MIT 许可协议

55KB
908

cargo-graph

Linux: 构建状态

一个 cargo 子命令,用于构建依赖关系图的 GraphViz DOT 文件。这个子命令最初是基于并受到 cargo-dot 项目启发,该项目由 Max New 创建

演示

假设我们想要构建 cargo-count 的依赖关系图,但我们希望可选依赖使用红色虚线和黑色方块,而常规(即“构建”)依赖使用橙色线和绿色菱形,我们可以运行以下命令。

注意:要生成从 dot 文件到 .PNG 的图像,需要安装 GraphViz 的 dot

$ cargo graph --optional-line-style dashed --optional-line-color red --optional-shape box --build-shape diamond --build-color green --build-line-color orange > cargo-count.dot
$ dot -Tpng > rainbow-graph.png cargo-count.dot

注意:也可以运行 cargo graph [options] | dot [options] > [file] 而不是单独的命令

第一个命令生成一个类似于下面的 GraphViz DOT 文件

digraph dependencies {
  N0[label="cargo-count",shape=diamond,color=green];
  N1[label="ansi_term",shape=box];
  N2[label="clap",shape=diamond,color=green];
  N3[label="clippy",shape=box];
  N4[label="glob",shape=diamond,color=green];
  N5[label="regex",shape=diamond,color=green];
  N6[label="tabwriter",shape=diamond,color=green];
  N7[label="aho-corasick",shape=diamond,color=green];
  N8[label="memchr",shape=diamond,color=green];
  N9[label="bitflags",shape=diamond,color=green];
  N10[label="strsim",shape=diamond,color=green];
  N11[label="unicode-normalization",shape=diamond,color=green];
  N12[label="libc",shape=diamond,color=green];
  N13[label="regex-syntax",shape=diamond,color=green];
  N14[label="unicode-width",shape=diamond,color=green];
  N0 -> N1[label="",style=dashed,color=red];
  N0 -> N2[label="",color=orange];
  N0 -> N3[label="",style=dashed,color=red];
  N0 -> N4[label="",color=orange];
  N0 -> N5[label="",color=orange];
  N0 -> N6[label="",color=orange];
  N7 -> N8[label="",color=orange];
  N2 -> N1[label="",style=dashed,color=red];
  N2 -> N9[label="",color=orange];
  N2 -> N10[label="",color=orange];
  N3 -> N11[label="",color=orange];
  N8 -> N12[label="",color=orange];
  N5 -> N7[label="",color=orange];
  N5 -> N8[label="",color=orange];
  N5 -> N13[label="",color=orange];
  N6 -> N14[label="",color=orange];
}

第二个命令生成一个类似于下面的图表的 PNG 图像

cargo-count dependencies

现在,为什么有人会这样做一个图表,这是一个不同的故事...但这是可能的 :)

安装

可以使用 cargo-graph 命令来安装

$ cargo install cargo-graph

如果出现有关 install 命令找不到的错误,可能需要 nightly 版本的 cargo。您也可以按照以下说明以传统方式编译和安装。

编译

按照以下说明编译 cargo-count,然后跳转到安装部分。

  1. 请确保已经安装了最新版本的 cargoRust
  2. 克隆项目 $ git clone https://github.com/kbknapp/cargo-graph && cd cargo-graph
  3. 构建项目 $ cargo build --release注意:在不优化编译时性能差异很大,因此我建议始终使用 --release 来启用它们)
  4. 完成之后,二进制文件将位于 target/release/cargo-graph

安装和使用

您只需要将 cargo-graph 放置在您的 $PATH 变量的某个位置。然后在任何项目目录中运行 cargo graph。详细信息请见下方。

Linux / OS X

您有两种选择,将 cargo-graph 放置在已经位于您的 $PATH 变量的目录中(要查看这些目录,打开终端并输入 echo "${PATH//:/\n}",引号很重要),或者您可以为您的 $PATH 添加一个自定义目录。

选项 1 如果您有权访问 $PATH 中的目录,或者您有 root 权限(或者通过 sudo),只需将 cargo-graph 复制到该目录 # sudo cp cargo-graph/usr/local/bin

选项 2 如果您没有 root 权限、sudo 或对 $PATH 中任何目录的写权限,您可以在您的家目录中创建一个目录,并将其添加。许多人使用 $HOME/.bin 来隐藏它(并不要使您的家目录变得杂乱),或者如果您希望它总是可见,可以使用 $HOME/bin。以下是一个示例,用于创建目录,将其添加到 $PATH,并将 cargo-graph 复制到那里。

只需将 bin 改为所需的目录名,将 .bashrc 改为你的shell启动文件(通常是 .bashrc.bash_profile.zshrc

$ mkdir ~/bin
$ echo "export PATH=$PATH:$HOME/bin" >> ~/.bashrc
$ cp cargo-graph~/bin
$ source ~/.bashrc

Windows

在Windows 7/8上,可以通过以管理员身份打开命令行并运行来将目录添加到 PATH 变量

C:\> setx path "%path%;C:\path\to\cargo-graph\binary"

否则,请确保你操作命令行所在的目录中有 cargo-graph 二进制文件,因为Windows会自动将你的当前目录添加到PATH中(即如果你打开命令行到 C:\my_project\ 来使用 cargo-graph,确保 cargo-graph.exe 也在该目录中)。

选项

使用 cargo-graph 有几个选项,应该是相当容易理解的。

USAGE:
    cargo graph [FLAGS] [OPTIONS]

FLAGS:
    -h, --help       Prints help information
    -I, --include-versions    Include the dependency version on nodes
    -V, --version    Prints version information

OPTIONS:
        --build-color <COLOR>            Color for regular deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --build-deps <true|false>        Should build deps be in the graph? (Defaults to 'true')
                                         ex. --build-deps=false OR --build-deps=no
        --build-line-color <COLOR>       Line color for regular deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --build-line-style <STYLE>       Line style for build deps (Defaults to 'solid')
                                          [values: solid dotted dashed]
        --build-shape <SHAPE>            Shape for regular deps (Defaults to 'round')
                                          [values: box round diamond triangle]
        --dev-color <COLOR>              Color for dev deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --dev-deps <true|false>          Should dev deps be included in the graph? (Defaults to 'false')
                                         ex. --dev-deps=true OR --dev-deps=yes
        --dev-line-color <COLOR>         Line color for dev deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --dev-line-style <STYLE>         Line style for dev deps (Defaults to 'solid')
                                          [values: solid dotted dashed]
        --dev-shape <SHAPE>              Shape for dev deps (Defaults to 'round')
                                          [values: box round diamond triangle]
        --dot-file <FILE>                Output file (Default to stdout)
        --lock-file <FILE>               Specify location of .lock file (Default 'Cargo.lock')
        --manifest-file <FILE>           Specify location of manifest file (Default 'Cargo.toml')
        --optional-color <COLOR>         Color for optional deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --optional-deps <true|false>     Should optional deps be in the graph? (Defaults to 'true')
                                         ex. --optional-deps=false OR --optional-deps=no
        --optional-line-color <COLOR>    Line color for optional deps (Defaults to 'black')
                                          [values: blue black yellow purple green red white orange]
        --optional-line-style <STYLE>    Line style for optional deps (Defaults to 'solid')
                                          [values: solid dotted dashed]
        --optional-shape <SHAPE>         Shape for optional deps (Defaults to 'round')
                                          [values: box round diamond triangle]

许可证

cargo-graph 在MIT条款下发布。有关详细信息,请参阅 LICENSE-MIT 文件。

依赖关系树

cargo-graph dependencies

依赖关系

~1.2–2MB
~43K SLoC