Created
March 25, 2011 19:25
-
-
Save unflores/887447 to your computer and use it in GitHub Desktop.
File utility - removes unused files
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
desc "Checks files in app and public against everything in the images directory" | |
task :delete_unused_images do | |
unused_images.each do |image| | |
puts "#{red('[delete]')} #{image}" | |
`rm #{image}` | |
end | |
end | |
desc "Shows all unused images" | |
task :unused_images do | |
unused_images.each{|image| puts "#{image}" } | |
end | |
def images_not_directly_used | |
FileList.new('public/images/*').map do |abs_file| | |
filename = File.basename(abs_file) | |
grep_results = `grep -r #{filename} "#{search_folders(ENV['PROJECT'])}"` #missing a piece | |
#find each class/id, see if it's referenced | |
abs_file unless grep_results[filename] | |
end.compact | |
end | |
def images_not_indirectly_used | |
=begin | |
Return each line in css file with image name in it | |
Parse line for css selector | |
Break up css selector | |
if most specific is id or class | |
find in app dir or delete | |
if most specific not id or class | |
print as too general | |
=end | |
raise Error | |
end | |
def unused_images | |
images_not_directly_used and images_not_indirectly_used | |
end | |
def search_folders(name) | |
environments = { | |
'rails' => %w(app/views), | |
'nodejs' => %w(public) | |
} | |
(environments[name] || environments['rails']).join(',') | |
end | |
def colorize(text, color_code) | |
"#{color_code}#{text}\033[0m" | |
end | |
def red(text); colorize(text, "\033[31m"); end | |
def green(text); colorize(text, "\033[32m"); end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment