|
| 1 | +#!/bin/bash -eu |
| 2 | + |
| 3 | +################################################################################ |
| 4 | +# Check deps |
| 5 | +################################################################################ |
| 6 | + |
| 7 | +if ! command -v dot >/dev/null 2>&1; then |
| 8 | + echo "\`dot\` not found. It is usually in the \`graphviz\` package." |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +if ! command -v tred >/dev/null 2>&1; then |
| 13 | + echo "\`tred\` not found. It is usually in the \`graphviz\` package." |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +################################################################################ |
| 18 | +# Argument processing |
| 19 | +################################################################################ |
| 20 | + |
| 21 | +script=$(basename "$0") |
| 22 | +stack_dot_flags="" |
| 23 | +include_test_bench="0" |
| 24 | +use_tred="1" |
| 25 | + |
| 26 | +while getopts "hetf" opt; do |
| 27 | + case $opt in |
| 28 | + h) |
| 29 | + echo "usage: ./${script} OPTS" >&2 |
| 30 | + echo "OPTS:" >&2 |
| 31 | + echo " -h : show help" >&2 |
| 32 | + echo " -e : include external dependencies in graph" >&2 |
| 33 | + echo " -t : include test+bench packages in graph" >&2 |
| 34 | + echo " -f : don't use \`tred\` - render full deg graph" >&2 |
| 35 | + exit 0 |
| 36 | + ;; |
| 37 | + e) |
| 38 | + stack_dot_flags="${stack_dot_flags} --external" |
| 39 | + ;; |
| 40 | + t) |
| 41 | + include_test_bench="1" |
| 42 | + ;; |
| 43 | + f) |
| 44 | + use_tred="0" |
| 45 | + ;; |
| 46 | + ?) |
| 47 | + echo "Invalid option: -$OPTARG" >&2 |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | + esac |
| 51 | +done |
| 52 | + |
| 53 | + |
| 54 | +################################################################################ |
| 55 | +# Generate graph |
| 56 | +################################################################################ |
| 57 | + |
| 58 | +tmpdir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename "$0").XXXXXXXXXXXX") |
| 59 | +outfile="cardano-sl-pkg-deps.png" |
| 60 | + |
| 61 | +# 'tred' and 'dot' are in the 'graphviz' of most Linux distributions. |
| 62 | + |
| 63 | +if [ "${include_test_bench}" = "0" ]; then |
| 64 | + prunefiles=$(find . -name \*.cabal -exec basename {} \; \ |
| 65 | + | grep -v stack-work | grep "test.cabal\\|bench.cabal" \ |
| 66 | + | sed 's/\.cabal//' | tr '\n' ',') |
| 67 | + stack_dot_flags="${stack_dot_flags} --prune ${prunefiles}" |
| 68 | +fi |
| 69 | + |
| 70 | +# This is a weird hack to satisfy shellcheck. While strange, it works, and is |
| 71 | +# technically safer. |
| 72 | +output="yes" |
| 73 | +stack dot ${output:+${stack_dot_flags}} > "${tmpdir}/full-dependencies.dot" |
| 74 | + |
| 75 | +final_dotfile="" |
| 76 | +if [ "${use_tred}" = "1" ]; then |
| 77 | + final_dotfile="${tmpdir}/direct-dependencies.dot" |
| 78 | + tred "${tmpdir}/full-dependencies.dot" > "${final_dotfile}" |
| 79 | +else |
| 80 | + final_dotfile="${tmpdir}/full-dependencies.dot" |
| 81 | +fi |
| 82 | + |
| 83 | +dot -Tpng "${final_dotfile}" -o ${outfile} |
| 84 | + |
| 85 | +rm -rf "${tmpdir:?}/" |
| 86 | + |
| 87 | +echo "Generated ${outfile}" |
0 commit comments