File Dependency in "Graphviz dot"

karoyakani2007-05-11


The visualization tool "dot" turns file dependency in a diagram. Given root file(s) , a ruby script below visits dependency files to put directed edges in a "dot" form:

out = Array.new
while path = ARGV.shift
  File::open(path) {|file|
    while file.gets
      if /^#include\s+"(\w+\.\w+)/ =~ $_
        out.push "\"#{path}\" -> \"#{$1}\""
        ARGV.unshift $1
      end
    end
  }
end

out.uniq!

puts "digraph G {"
out.each {|s| puts s}
puts "}"

Then pipe it to "dot" to output a graphic file (jpg in this example):

% ruby mkdep.rb a.c | dot -Tjpg -o out.jpg