Skip to content

Instantly share code, notes, and snippets.

@Pondidum
Last active September 12, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pondidum/a79bddeeba612f9eb1074e9f5274b047 to your computer and use it in GitHub Desktop.
Save Pondidum/a79bddeeba612f9eb1074e9f5274b047 to your computer and use it in GitHub Desktop.
Creates a nuget package dependency graph in graphviz dot language
# put the output in the box here: http://www.webgraphviz.com/
RESULT_FILE="graph.dot" # the output file
NAME_MATCH='Microsoft\.' # leave this as a blank string if you want no filtering
echo '' > $RESULT_FILE # clear out the file
echo 'digraph Dependencies {' >> $RESULT_FILE
echo ' rankdir=LR;' >> $RESULT_FILE # we want a left to right graph, as it's a little easier to read
# find all packages.config, recursively beaneath the path passed into the script
find $1 -iname packages.config | while read line; do
# find any csproj file next to the packages.config
project_path="$(dirname $line)/*.csproj"
# check it exists (e.g. to not error on a /.nuget/packages.config path)
if [ -f $project_path ]; then
# find the name of the assembly
# (our projects are not named with the company prefix, but the assemblies/packages are)
asm_name=$(grep -oP '<RootNamespace>\K(.*)(?=<)' $project_path)
# Ignore any tests projects (optional)
if [[ ${line} != *"Tests"* ]]; then
# find all lines in the packages.config where the package name has a prefix
grep -Po "package id=\"\K($NAME_MATCH.*?)(?=\")" $line | while read package; do
# write it to the result
echo " \"$asm_name\" -> \"$package\"" >> $RESULT_FILE
done
fi
fi
done
echo '}' >> $RESULT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment