A cargo subcommand for creating GraphViz DOT files and dependency graphs
A
cargosubcommand for building GraphViz DOT files of dependency graphs. This subcommand was originally based off and inspired by the project cargo-dot by Max New
Let's say we wanted to build a dependency graph of cargo-count but we wanted optional dependencies to use red dashed lines and black boxes, and regular (aka "build") dependencies to use orange lines to green diamonds, one would run the following.
NOTE: GraphViz
dotneeds to be installed to produce the .PNG from the dotfile
$ 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
NOTE: It's also possible to run
cargo graph [options] | dot [options] > [file]instead of individual commands
The first command produces a GraphViz DOT file which looks like this:
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]; }
The second command produces a PNG image of the graph which looks like:
Now, why someone would do that to a graph is a different story...but it's possible :)
cargo-graphcan be installed with
cargo install
$ cargo install cargo-graph
This may require a nightly version of
cargoif you get an error about the
installcommand not being found. You may also compile and install the traditional way by following the instructions below.
Follow these instructions to compile
cargo-count, then skip down to Installation.
cargoand Rust installed
$ git clone https://github.com/kbknapp/cargo-graph && cd cargo-graph
$ cargo build --release(NOTE: There is a large performance difference when compiling without optimizations, so I recommend always using
--releaseto enable to them)
target/release/cargo-graph
All you need to do is place
cargo-graphsomewhere in your
$PATH. Then run
cargo graphanywhere in your project directory. For full details see below.
You have two options, place
cargo-graphinto a directory that is already located in your
$PATHvariable (To see which directories those are, open a terminal and type
echo "${PATH//:/\n}", the quotation marks are important), or you can add a custom directory to your
$PATH
Option 1 If you have write permission to a directory listed in your
$PATHor you have root permission (or via
sudo), simply copy the
cargo-graphto that directory
# sudo cp cargo-graph/usr/local/bin
Option 2 If you do not have root,
sudo, or write permission to any directory already in
$PATHyou can create a directory inside your home directory, and add that. Many people use
$HOME/.binto keep it hidden (and not clutter your home directory), or
$HOME/binif you want it to be always visible. Here is an example to make the directory, add it to
$PATH, and copy
cargo-graphthere.
Simply change
binto whatever you'd like to name the directory, and
.bashrcto whatever your shell startup file is (usually
.bashrc,
.bash_profile, or
.zshrc)
$ mkdir ~/bin $ echo "export PATH=$PATH:$HOME/bin" >> ~/.bashrc $ cp cargo-graph~/bin $ source ~/.bashrc
On Windows 7/8 you can add directory to the
PATHvariable by opening a command line as an administrator and running
C:\> setx path "%path%;C:\path\to\cargo-graph\binary"
Otherwise, ensure you have the
cargo-graphbinary in the directory which you operating in the command line from, because Windows automatically adds your current directory to PATH (i.e. if you open a command line to
C:\my_project\to use
cargo-graphensure
cargo-graph.exeis inside that directory as well).
There are a few options for using
cargo-graphwhich should be somewhat self explanatory.
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 for regular deps (Defaults to 'black') [values: blue black yellow purple green red white orange] --build-deps Should build deps be in the graph? (Defaults to 'true') ex. --build-deps=false OR --build-deps=no --build-line-color Line color for regular deps (Defaults to 'black') [values: blue black yellow purple green red white orange] --build-line-style