Skip to content

Instantly share code, notes, and snippets.

@kharmabum
Forked from soffes/clean_assets.rb
Created December 31, 2012 20:24
Show Gist options
  • Save kharmabum/4422476 to your computer and use it in GitHub Desktop.
Save kharmabum/4422476 to your computer and use it in GitHub Desktop.
desc 'Remove unused images'
task :clean_assets do
require 'set'
all = Set.new
used = Set.new
unused = Set.new
# White list
used.merge %w{Icon Icon-29 Icon-50 Icon-58 Icon-72 Icon-114}
regex = /\[UIImage imageNamed:@"([a-zA-Z0-9\-_]+).png"\]/
Dir.glob('Classes/*.m').each do |path|
used.merge File.open(path).read.scan(regex).flatten
end
Dir.glob('Resources/Images/*.png').each do |path|
next if path.include? '@2x.png'
all << path.gsub(/Resources\/Images\/([a-zA-Z0-9\-_]+).png/, "\\1")
end
unused = all - used
unused.each do |key|
`rm -f Resources/Images/#{key}.png Resources/Images/#{key}@2x.png`
end
puts "#{all.length} total found"
puts "#{used.length} used found"
puts "#{unused.length} deleted"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment