Permalink
Browse files

Cache public/assets, and clean unreferenced assets after precompile

  • Loading branch information...
1 parent a815917 commit bdf6c8a059028427864e05a03e80d079147f266a @ndbroadbent committed Oct 25, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 lib/language_pack/rails3.rb
@@ -43,6 +43,9 @@ def run_assets_precompile_rake_task
if File.exists?("public/assets/manifest.yml")
puts "Detected manifest.yml, assuming assets were compiled locally"
else
+ FileUtils.mkdir_p('public')
+ cache_load "public/assets"
+
ENV["RAILS_GROUPS"] ||= "assets"
ENV["RAILS_ENV"] ||= "production"
@@ -53,6 +56,13 @@ def run_assets_precompile_rake_task
if $?.success?
log "assets_precompile", :status => "success"
puts "Asset precompilation completed (#{"%.2f" % time}s)"
+
+ if clean_unreferenced_assets
+ cache_store "public/assets"
+ else
+ # If something goes wrong, clear the assets cache before the next run.
+ cache_clear "public/assets"
+ end
else
log "assets_precompile", :status => "failure"
puts "Precompiling assets failed, enabling runtime asset compilation"
@@ -65,6 +75,26 @@ def run_assets_precompile_rake_task
end
end
+ # Removes assets that aren't referenced by manifest.yml
+ def clean_unreferenced_assets
+ return false unless File.exists?("public/assets/manifest.yml")
+ digests = YAML.load_file("public/assets/manifest.yml")
+ known_assets = digests.flatten.flat_map {|a| [a, "#{a}.gz"] }.map {|a| File.join('public/assets', a) }
+
+ puts "Cleaning up the assets cache."
+ Dir.glob('public/assets/**/*').each do |asset|
+ next if File.directory?(asset) || asset.include?('manifest.yml')
+ # Remove asset if not referenced by manifest.yml
+ unless known_assets.include?(asset)
+ puts "Removing unreferenced asset: #{asset.sub(%r{.*/public/assets/}, '')}"
+ FileUtils.rm_f asset
+ end
+ end
+ rescue Exception => ex
+ puts "Something failed while cleaning up old assets! => #{ex.message.inspect}"
+ false
+ end
+
# setup the database url as an environment variable
def setup_database_url_env
ENV["DATABASE_URL"] ||= begin

0 comments on commit bdf6c8a

Please sign in to comment.