Created
December 9, 2011 10:45
-
-
Save cthiel/1451064 to your computer and use it in GitHub Desktop.
Rails 3.1 stylesheet assets dependency graph
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/ruby | |
| # | |
| # (c) 2011 SUSE Linux Products GmbH, Author: Christoph Thiel <[email protected]> | |
| # | |
| # script that generate dependency graph for Rails 3.1 stylesheet assets | |
| # | |
| # usage: sudo rubygem install ruby-graphviz | |
| # cp assets-graphviz.rb yourapp/app/assets/ | |
| # cd yourapp/app/assets/; chmod +x assets-graphviz.rb; ./assets-graphviz.rb | |
| require 'rubygems' | |
| require 'find' | |
| require "graphviz" | |
| g = GraphViz::new( "G", :type => "graph", :use => "dot" ) | |
| nodes = {} | |
| Find.find('stylesheets') do |f| | |
| next if File.directory?(f) | |
| next unless f.match(/.(css|sass)$/) | |
| lines = File.read(f) | |
| requires = [] | |
| lines.grep(/= require/).each do |r| | |
| if r.match(/'(.*)'/) | |
| req = r.match(/'(.*)'/)[1] | |
| if File.exists?('stylesheets/'+req) | |
| requires << req | |
| elsif File.exists?('stylesheets/'+req+".css") | |
| requires << req + ".css" | |
| elsif File.exists?('stylesheets/'+req+".sass") | |
| requires << req + ".sass" | |
| else | |
| puts "skipping: " + req + " in " + f | |
| end | |
| end | |
| end | |
| depend_on = [] | |
| lines.grep(/= depend_on/).each do |r| | |
| if r.match(/'(.*)'/) | |
| req = r.match(/'(.*)'/)[1] | |
| if File.exists?('stylesheets/'+req) | |
| depend_on << req | |
| elsif File.exists?('stylesheets/'+req+".css") | |
| depend_on << req + ".css" | |
| elsif File.exists?('stylesheets/'+req+".sass") | |
| depend_on << req + ".sass" | |
| else | |
| puts "skipping: " + req + " in " + f | |
| end | |
| end | |
| end | |
| imports = [] | |
| lines.grep(/@import/).each do |r| | |
| import = r.match(/@import\ (.*)/)[1] | |
| import.gsub!(/[";]/,'') | |
| import = import.match(/\((.*)\)/)[1] if import.match(/url/) | |
| # imports are relative, so let's normalize them based on the current directory | |
| imports << f.gsub(/^stylesheets\//,'').gsub(File.basename(f),'')+import.strip | |
| end | |
| node = f.gsub(/^stylesheets\//,'') | |
| nodes.merge!({node => [g.add_node( node ), requires, depend_on, imports]}) | |
| # DEBUG | |
| #puts "current file: " + node | |
| #puts " requires: " + requires.inspect | |
| #puts " depend_on: " + depend_on.inspect | |
| #puts " imports:" + imports.inspect | |
| end | |
| nodes.each do |node,data| | |
| # data: node, requires, depend_on, imports | |
| data[1].each do |req| | |
| if nodes[req] | |
| g.add_edge (data[0], nodes[req][0]) | |
| else | |
| puts "missing node: " + req | |
| end | |
| end | |
| data[2].each do |dep| | |
| if nodes[dep] | |
| g.add_edge (nodes[dep][0], data[0]) | |
| else | |
| puts "missing node: " + dep | |
| end | |
| end | |
| data[3].each do |imp| | |
| if nodes[imp] | |
| g.add_edge (data[0], nodes[imp][0]) | |
| else | |
| puts "missing node: " + imp | |
| end | |
| end | |
| end | |
| g.output( :png => "stylesheets.png" ) | |
| g.output( :dot => "stylesheets.dot" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment