Last active
August 29, 2015 14:10
-
-
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?
This file contains 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
egrep -e "require\s\"([a-z]|_|/)+\"" -o -r lib | ./sniffrequires.rb | dot -Tsvg > /tmp/foo.svg; open /tmp/foo.svg |
This file contains 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/ruby | |
puts "digraph {" | |
$stdin.each do | line | | |
/lib\/(?<from>(\w|\/)+)(\.rb)?:require \"(?<to>(\w|\/)+)/i =~ line | |
puts "\t #{from.gsub("/", "_")} -> #{to.gsub("/", "_")}" | |
end | |
puts "}" |
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>[^'"]+)["']}
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
To be fair, the parser version is possibly worse :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.