Skip to content

Instantly share code, notes, and snippets.

@eiwi1101
Created November 30, 2015 18:07
Show Gist options
  • Save eiwi1101/e4996dd5b52b9ba2e64a to your computer and use it in GitHub Desktop.
Save eiwi1101/e4996dd5b52b9ba2e64a to your computer and use it in GitHub Desktop.
Enumerates the SASS variables in your Rails app, and searches for places they can be used.
#!/usr/bin/ruby
#
# Needs work, feel free to contribute.
# Dependencies: ANSI color escapes, spits out vim commands.
#
# USAGE
# ./sassify.rb list
# ./sassify.rb [files...]
#
# list - Command which lists all loaded SASSY variables.
# Of course, pretty prints in color and stuff.
#
# files... - Files to search for REPLACEMENTS.
# If empty, we'll look in all your .scss files
# We still look in all your .scss files for variables...
# ... because reasons.
#
begin
puts "Enumerating application stylesheets..."
@app_scss = Dir['app/**/*.scss']
@scss_var = Hash.new
puts "Found #{@app_scss.count} files."
puts
puts "Loading variables..."
@app_scss.each do |f|
File.readlines(f).each do |l|
next unless l =~ /\$([\w\-]+):\s*(.*?);/
next if $2[0] == '$'
@scss_var[$1] = [%r{[\w\-]+:.*?[\(,\s]#{Regexp.escape($2)}\b}, Regexp.escape($2), $2]
end
end
puts "Found #{@scss_var.count} variables."
puts
if ARGV[0].downcase == "list"
@scss_var.each do |k,v|
puts "\x1b[36m$%s\x1b[0m:\t\x1b[35m%s\x1b[0m;" % [
k, v[2]
]
end
exit 0
end
# List Search Files
@search = ARGV.collect { |f| Dir[f] }.flatten
@search.empty? and @search = @app_scss
puts "Searching for replacements in #{@search.count} files..."
puts
@search.each do |f|
File.readlines(f).each_with_index do |l, n|
l.strip!
print "\x1b[33m%25s [%04d] >\x1b[35m%s\x1b[0m\r" % [
f, n + 1,
("." * (n%25)) + (" " * (25-(n%25)))
]
@scss_var.each do |k, v|
next if /\$#{k}:/.match(l)
next unless v[0].match(l)
puts "\x1b[37;1m%30s\x1b[0m:%04d - \x1b[32m%s\x1b[31m => \x1b[36m$%s\x1b[0m: \x1b[35m%s\x1b[0m" % [
f, n + 1, l, k, v[2]
]
puts "\tvim %s +:%s +:s/%s/\\$%s/gi +:wq" % [
f, n + 1, v[1], k
]
puts
end
end
end
puts "\x1b[K\x1b[0mGood bye, I'm finished."
rescue Interrupt
puts
puts "\x1b[K\x1b[0mGood bye, you killed me."
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment