Skip to content

Instantly share code, notes, and snippets.

@harukizaemon
Created August 7, 2009 03:19
Show Gist options
  • Save harukizaemon/163689 to your computer and use it in GitHub Desktop.
Save harukizaemon/163689 to your computer and use it in GitHub Desktop.
#########################################
# Run the following to generate some input:
# find . -iname '*.java' -type f -exec cat {} \; | grep -E '^(package|import)'
# Then run this script to generate a .dot file for importing into graphiz
require 'set'
@dependencies = Hash.new { |hash, key| hash[key] = Set.new }
def add_dependency?(from, to)
@dependencies[from].add?(to)
end
def print_dependency(from, to)
if add_dependency?(from, to)
puts %( "#{from}" -> "#{to}";)
end
end
def process_package(name)
@package_name = name
end
def package_name_for(class_name)
class_name.split(".")[0..-2].join(".")
end
def process_import(name)
print_dependency(@package_name, package_name_for(name))
end
def dispatch(line)
case line
when /^package\s+(.*);$/ then process_package($1)
when /^import\s+(.*);$/ then process_import($1)
else raise "Invalid line: '#{line}'"
end
end
def print_header
puts "digraph G {"
end
def process(stream)
stream.each_line { |line| dispatch(line.chomp) }
end
def print_footer
puts "}"
end
print_header
process($<)
print_footer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment