Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/e742663998351cdc8a7d to your computer and use it in GitHub Desktop.
Save pcreux/e742663998351cdc8a7d to your computer and use it in GitHub Desktop.
Generate graph of dependency for scss stylesheets
#!/usr/bin/env ruby
#
# Generate a graph of dependencies for your stylesheets.
#
# Requires graphviz
#
# Run `ruby dependencies.rb | fdp -Tpng > out.png; open out.png`
PATH = "app/assets/stylesheets/"
puts "digraph stylesheets {"
Dir.glob("#{PATH}**/*.scss").each do |file|
human_file = file.gsub('app/assets/stylesheets/', '').gsub('.css.scss', '')
human_dir = human_file.split('/')[0..-2].join('/')
content = File.open(file).each_line do |line|
dependency = nil
if line["@import"]
dependency = line[/["'].+["']/][1..-2]
end
if line["//= require"]
dependency = line.gsub("//= require", "").strip
end
if dependency
puts %|"#{human_file}" -> "#{[human_dir, dependency].reject { |f| f.nil? || f == "" }.join("/")}";|;
end
end
end
puts "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment