Skip to content

Instantly share code, notes, and snippets.

@danielricecodes
Forked from rafaelp/find_unused_images.rake
Last active April 23, 2018 18:59
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 danielricecodes/308df9d14d579aa7eb323a7659d769ae to your computer and use it in GitHub Desktop.
Save danielricecodes/308df9d14d579aa7eb323a7659d769ae to your computer and use it in GitHub Desktop.
Rake task to find unused images on Rails project to deletion.
#With Homebrew, run `brew install ack` to ensure this will work.
task :find_unused_images do
raise('ack not found!!! install it with your favorite package manager!') if `which ack`.empty?
images = Dir.glob('app/assets/images/**/*')
images_to_delete = images.inject([]) do |memo, image|
#Ignore Directories
unless File.directory?(image)
# print "\nChecking #{image}..."
print "."
result = `ack -g -i '(app|public)' | ack -x -w '#{File.basename(image)}'`
memo.push(image) if result.empty?
end
memo
end
puts "\n\nDelete 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