Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 14:33
Show Gist options
  • Save anonymous/4276726 to your computer and use it in GitHub Desktop.
Save anonymous/4276726 to your computer and use it in GitHub Desktop.
Speed up rake assets:precompile *without* any sprockets or git hacks.
desc "precompile assets (only if needed)"
task :precompile_assets do
puts "Diffing assets to check if we need to precompile"
precompile = true
# Only do this if we have a previous release
if previous_release && latest_release
puts "Comparing asset sources of #{previous_release} and #{latest_release}."
# Gather all the names of the source files from your local machine.
# NOTE: it would be better to diff the filename lists of the previous and lastest release, but this will function.
files = ['Gemfile.lock', 'config/routes.rb']
['app/assets', 'lib/assets', 'vendor/assets'].map{|d| Dir["#{d}/**/*.*"].each{ |f| files << f }}
begin
diff_commands = []
files.each do |file|
diff_commands << "diff -w -q '#{previous_release}/#{file}' '#{latest_release}/#{file}'"
end
# Diff & copy, raises CommandError if any of the diffs fail
run diff_commands.join(' && ')
run "cp -R -p #{previous_release}/public/assets/ #{latest_release}/public/assets/"
precompile = false
rescue Capistrano::CommandError
puts "Diff detected, so we need to recompile our assets"
precompile = true
end
end
# Do a normal precompile if anything broke or on a force precompile
precompile = true if ENV['FORCE_PRECOMPILE']
run "cd #{latest_release} && bundle exec rake i18n:js:export assets:precompile RAILS_ENV=production" if precompile
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment