
Untitled
By: a guest on
Apr 30th, 2012 | syntax:
None | size: 1.31 KB | hits: 15 | expires: Never
desc "cache assets"
task :cache_assets => :environment do
BUCKET = 'my-bucket-name'
PATHS = ['public/javascripts/all.js']
s3_yml = "#{Rails.root}/config/s3.yml"
s3_config = YAML.load_file(s3_yml)
puts "-----> caching assets..."
PATHS.each do |path|
FileUtils.rm(path) if File.exist?(path)
end
ActionController::Base.perform_caching = true
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get('/')
session.follow_redirect!
AWS::S3::Base.establish_connection!(
:access_key_id => s3_config['production']['access_key_id'],
:secret_access_key => s3_config['production']['secret_access_key']
)
PATHS.each do |f|
next if File.directory?(f)
key = f.gsub(/\.\/public/, '')
puts " -> %s" % key
puts " -> uglify..."
ugly_file = Uglifier.new.compile(File.read(f))
puts " -> gzip..."
gzip_file = StringIO.open('', 'w')
gz = Zlib::GzipWriter.new(gzip_file)
gz.write(ugly_file)
gz.close
puts " -> upload to S3..."
AWS::S3::S3Object.store(
key, gzip_file.string, BUCKET,
:access => :public_read, 'Cache-Control' => 'max-age=315360000', 'Content-Encoding' => 'gzip'
)
puts " -> add config var"
system("heroku config:add all_js_cache_id=#{ugly_file.hash.abs}")
end
puts "-----> done"
end