Skip to content

Instantly share code, notes, and snippets.

@bdmac
Created August 19, 2014 23:44
Show Gist options
  • Save bdmac/c4799e29f6a54cea45cf to your computer and use it in GitHub Desktop.
Save bdmac/c4799e29f6a54cea45cf to your computer and use it in GitHub Desktop.
Modification to assets:clean rake task to cleanup old assets from public/assets.
Rake::Task["assets:clean"].enhance do
return "can't run in dev" if Rails.env.development?
puts 'Running my assets:clean extension.'
manifest = Sprockets::Rails::Task.new(Rails.application).manifest
["#{Dir.pwd}/public/assets/"].each do |dir_path|
records = Dir.glob("#{dir_path}**/*")
records.each do |f|
if f =~ /.*.png$/ or
f =~ /.*.jpg$/ or
f =~ /.*.gif$/ or
f =~ /.*.ico$/ or
f =~ /.*.eot$/ or
f =~ /.*.svg$/ or
f =~ /.*.woff$/ or
f =~ /.*.ttf$/ or
f =~ /.*.otf$/ or
f =~ /.*.css$/ or
f =~ /.*.js$/ or
f =~ /.*.sass$/ or
f =~ /.*.css$/ or
f =~ /.*.scss$/ or
f =~ /.*.coffee$/ or
f =~ /.*.wav$/ or
f =~ /.*.gz$/ then
filename = File.basename(f)
# Apparently sprockets doesn't store gzip filenames in the
# manifest so we need to strip the .gz ending and look for
# that instead.
filename = filename[0...-3] if filename.ends_with?('.gz')
unless manifest.files.key?(filename)
puts "Removing #{filename} from filesystem because it's not in the manifest..."
File.delete(f) if File.file?(f)
end
end
end
puts Dir.glob("#{dir_path}**/*")
end
puts 'Finished assets:clean extension...'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment