Skip to content

Instantly share code, notes, and snippets.

@myouju
Last active December 24, 2019 04:18
Show Gist options
  • Save myouju/5a15d4c6aef72c67ca09aed8a91d4d1f to your computer and use it in GitHub Desktop.
Save myouju/5a15d4c6aef72c67ca09aed8a91d4d1f to your computer and use it in GitHub Desktop.
A Go dependency graph visualization tool (golangのpackage依存を可視化)

Run

# first party package only
bash run.sh $GOPATH/src/$YOUR_REPO > darge.html

# with third party package
bash run_with_thirdparty.sh $GOPATH/src/$YOUR_REPO > darge.html

View

open darge.html

sample image

sample

with third party

sample_with_thirdparty

<!doctype html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script src="https://dagrejs.github.io/project/dagre-d3/latest/dagre-d3.js"></script>
<style id="css">
.clusters rect {
fill: #00ffd0;
stroke: #999;
stroke-width: 1.5px;
}
text {
font-weight: 300;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
font-size: 14px;
}
.node rect {
stroke: #999;
fill: #fff;
stroke-width: 1.5px;
}
.edgePath path {
stroke: #333;
stroke-width: 1.5px;
}
</style>
<svg id="svg-canvas"></svg>
<script id="js">
// Create the input graph
var g = new dagreD3.graphlib.Graph({compound:true})
.setGraph({rankdir:"LR"})
.setDefaultEdgeLabel(function() { return {}; });
//// Here we're setting the nodes
{{#NODES}}
{{.}}
{{/NODES}}
//// Set up edges, no special attributes.
{{#EDGES}}
{{.}}
{{/EDGES}}
g.nodes().forEach(function(v) {
var node = g.node(v);
// Round the corners of the nodes
node.rx = node.ry = 5;
});
// Create the renderer
var render = new dagreD3.render();
// Set up an SVG group so that we can translate the final graph.
var svg = d3.select("svg"),
svgGroup = svg.append("g");
// Run the renderer. This is what draws the final graph.
render(d3.select("svg g"), g);
document.getElementById("svg-canvas").style.width = g.graph().width + 40;
svg.attr("height", g.graph().height + 80);
// Center the graph
svgGroup.attr("transform", "translate(20, 40)");
</script>
export GO111MODULE=on
BASE_DIR=$(pwd)
cd $1
BASE_HOST=$(basename $(dirname $(dirname $(pwd))))
BASE_ORG=$(basename $(dirname $(pwd)))
BASE_REPO=$(basename $(pwd))
export NODES=()
export EDGES=()
while read package
do
node=$(echo "g.setNode('$package', {label: '$package'});" | sed -e "s:$BASE_HOST/$BASE_ORG/$BASE_REPO/::g")
NODES+=( $node )
cd $GOPATH/src/$package
while read p
do
edge=$(echo "g.setEdge('$package','$p');" | sed -e "s:$BASE_HOST/$BASE_ORG/$BASE_REPO/::g")
EDGES+=($edge)
done < <(go list -f '{{join .Imports "\n"}}' | grep $BASE_REPO | grep -v vendor)
done < <(go list ./...)
cd $BASE_DIR
if [ ! -f $BASE_DIR/mo ]; then
curl https://raw.githubusercontent.com/tests-always-included/mo/master/mo > $BASE_DIR/mo
fi
. $BASE_DIR/mo
mo darge.html.mo
export GO111MODULE=on
BASE_DIR=$(pwd)
cd $1
BASE_HOST=$(basename $(dirname $(dirname $(pwd))))
BASE_ORG=$(basename $(dirname $(pwd)))
BASE_REPO=$(basename $(pwd))
export NODES=()
export EDGES=()
while read package
do
node=$(echo "g.setNode('$package', {label: '$package'});")
NODES+=( $node )
cd $GOPATH/src/$package
while read p
do
NODES+=( $(echo "g.setNode('$p', {label: '$p'});"))
edge=$(echo "g.setEdge('$package','$p');")
EDGES+=($edge)
done < <(go list -f '{{join .Imports "\n"}}' | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}')
done < <(go list ./...)
cd $BASE_DIR
if [ ! -f $BASE_DIR/mo ]; then
curl https://raw.githubusercontent.com/tests-always-included/mo/master/mo > $BASE_DIR/mo
fi
. $BASE_DIR/mo
mo darge.html.mo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment