require 'pry'
namespace :assets do
  task dedigest: :environment do
    extensions = %w[jpg png js css gif eot svg woff2 woff ttf]
    Dir.glob(Rails.root.join('public/assets/**', "*.{#{extensions.join(',')}}")).each do |asset_path|
     next unless asset_path =~ /-[a-f0-9]{32,64}\.(#{extensions.join('|')})\Z/
     dedigest_path = asset_path.sub(/-[a-f0-9]{32,64}/, '')
     `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
