Skip to content

Instantly share code, notes, and snippets.

@jennifersmith
Last active August 29, 2015 14:10
Show Gist options
  • Save jennifersmith/3f22988c52ee46ce10c1 to your computer and use it in GitHub Desktop.
Save jennifersmith/3f22988c52ee46ce10c1 to your computer and use it in GitHub Desktop.
Convert requires into a graph - this is pretty dreadful please can someone do better?
egrep -e "require\s\"([a-z]|_|/)+\"" -o -r lib | ./sniffrequires.rb | dot -Tsvg > /tmp/foo.svg; open /tmp/foo.svg
#!/usr/bin/ruby
puts "digraph {"
$stdin.each do | line |
/lib\/(?<from>(\w|\/)+)(\.rb)?:require \"(?<to>(\w|\/)+)/i =~ line
puts "\t #{from.gsub("/", "_")} -> #{to.gsub("/", "_")}"
end
puts "}"
@jennifersmith
Copy link
Author

Creates something like

https://www.dropbox.com/s/lr1czff32zdccz1/foo.png?dl=0

Anyone has a better approach let me know. Just hacked this together to see why guard/rspec had so many circular dependency warnings.

@johncarney
Copy link

Looks like it does the job pretty well. Your grep will miss files that have underscores or dashes in them, or use single quotes instead of double. You could make the regular expressions a bit more accurate like so: egrep -e "requires\s*[\"'][^\"']+[\"']" ... and %r{lib/(?<from>.+?)(\.rb)?:require\s*["'](?<to>[^'"]+)["']}

@jennifersmith
Copy link
Author

Thanks - been gradually refining it as I realised I missed stuff! To be honest it's pretty horrible and if I was going to do it again I would probably use a real grown up parser not regex :D

@jennifersmith
Copy link
Author

To be fair, the parser version is possibly worse :D

https://github.com/jennifersmith/rubygraph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment