From 3109bdf4b116233b1deca9399f0d61af86dffbb8 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Sun, 4 Oct 2015 13:03:16 +0200 Subject: [PATCH] change deploy behaviour --- config/deploy.rb | 6 ++++-- lib/tasks/dedigest_assets.rake | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index bdb3c4ee..c7e5f3dc 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -96,6 +96,7 @@ namespace :deploy do with rails_env: fetch(:rails_env) do execute 'bin/rake assets:precompile' execute 'bin/rake assets:dedigest' + execute 'bin/rake assets:gzip' end end @@ -105,10 +106,11 @@ namespace :deploy do #end #end rsync_host = host - run_locally { execute "rsync -av --delete ./public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/" } + #run_locally { execute "rsync -av --delete ./public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/" } + run_locally { execute "rsync -av ./public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/" } execute :chown, "-R www-data:www-data", shared_path.join('public/assets/') - #run_locally { execute 'rm -rf public/assets' } + run_locally { execute 'rm -rf public/assets' } end end diff --git a/lib/tasks/dedigest_assets.rake b/lib/tasks/dedigest_assets.rake index 4e57795c..dcc92150 100644 --- a/lib/tasks/dedigest_assets.rake +++ b/lib/tasks/dedigest_assets.rake @@ -1,3 +1,4 @@ +require 'pry' namespace :assets do task dedigest: :environment do extensions = %w[jpg png js css gif eot svg woff2 woff ttf] @@ -7,4 +8,27 @@ namespace :assets do `cp '#{asset_path}' '#{dedigest_path}' 2>/dev/null` end end + desc "Create .gz versions of assets" + task gzip: :environment do + zip_types = /\.(?:css|html|js|otf|svg|txt|xml)$/ + + public_assets = File.join(Rails.root, "public", Rails.application.config.assets.prefix) + + Dir.glob("#{public_assets}/**/*").each do |f| + next unless f =~ zip_types + + mtime = File.mtime(f) + gz_file = "#{f}.gz" + next if File.exist?(gz_file) && File.mtime(gz_file) >= mtime + + File.open(gz_file, "wb") do |dest| + gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION) + gz.mtime = mtime.to_i + IO.copy_stream(open(f), gz) + gz.close + end + + File.utime(mtime, mtime, gz_file) + end + end end