Skip to content

Instantly share code, notes, and snippets.

@justvitalius
Forked from rafaelp/find_unused_images.rake
Last active May 15, 2016 14:32
Show Gist options
  • Save justvitalius/25318a1d71bcc657ee21 to your computer and use it in GitHub Desktop.
Save justvitalius/25318a1d71bcc657ee21 to your computer and use it in GitHub Desktop.
Rake task for finding unused images in rails app.
It requires 'ack' http://betterthangrep.com/
MacOS: brew install ack
Linux: apt-get install ack-grep
Before lunch a task put .ackrc to home directory like ~/.ackrc
--type-add=css=.sass,.less,.scss
--type-add=ruby=.rake,.rsel,.builder,.thor
--type-add=html=..html,.html.erb,.haml,.html.haml,.slim,.html.slim
--type-add=js=.js.erb,.coffee
--type-set=cucumber=.feature
--ignore-dir=vendor
--ignore-dir=log
--ignore-dir=tmp
--ignore-dir=doc
--ignore-dir=coverage
--ignore-dir=public/assets
--ignore-dir=spec/cassettes
--ignore-dir=features/cassettes
--smart-case
--sort-files
--color
--follow
--group
# It requires ACK - http://betterthangrep.com/
task :find_unused_images do
images = Dir.glob('app/assets/images/**/*')
images_to_delete = []
images.each do |image|
unless File.directory?(image)
# print "\nChecking #{image}..."
print "."
result = `ack -1 --ruby --html --css --js #{File.basename(image)}`
if result.empty?
images_to_delete << image
else
end
end
end
puts "\n\nDelete #{images_to_delete.count} unused files running the command below:"
puts "rm #{images_to_delete.join(" ")}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment