Skip to content

Instantly share code, notes, and snippets.

@unflores
Created March 25, 2011 19:25
Show Gist options
  • Save unflores/887447 to your computer and use it in GitHub Desktop.
Save unflores/887447 to your computer and use it in GitHub Desktop.
File utility - removes unused files
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