Created
February 23, 2015 21:34
-
-
Save mattgillooly/d007a2641b625cc92c6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # | |
| # Graph dependencies from a Drakefile using Graphviz | |
| # | |
| # USAGE: drake --print +... | ruby drakeviz.rb out.png | |
| # | |
| require 'graphviz' | |
| class Step < Struct.new(:inputs, :outputs) | |
| def each_edge(&block) | |
| inputs.product(outputs).each(&block) | |
| end | |
| end | |
| unless TARGET_FILENAME = ARGV[0] | |
| fail "missing TARGET_FILENAME" | |
| end | |
| steps = [] | |
| current_step = nil | |
| # Parse output from `drake --print` | |
| $stdin.each_line do |line| | |
| line_type, path = line.split | |
| case line_type | |
| when 'S' | |
| steps << current_step if current_step | |
| current_step = Step.new([], []) | |
| when 'I' | |
| current_step.inputs << path | |
| when 'O' | |
| current_step.outputs << path | |
| else | |
| $stderr.puts "Unknown line type: #{line_type.inspect}" | |
| end | |
| end | |
| steps << current_step | |
| # Create a new graph | |
| g = GraphViz.new(:G, :type => :digraph) | |
| # Create an edge between each input and output of each step | |
| steps.each do |step| | |
| step.each_edge do |input, output| | |
| g.add_edges(input, output) | |
| end | |
| end | |
| # Generate output image | |
| g.output(:png => TARGET_FILENAME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment