Created
March 23, 2012 23:03
-
-
Save jparker/2176147 to your computer and use it in GitHub Desktop.
Rake task for compiling assets and uploading them to S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RAILS_ROOT/lib/tasks/assets.rake | |
namespace :assets do | |
desc 'Precompile assets and upload to S3' | |
task :upload, [:noop] => ['assets:clean', 'assets:precompile'] do |_, args| | |
args.with_defaults(noop: false) | |
Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml" | |
Dir.chdir("#{Rails.root}/public") do | |
assets = FileList['assets',"assets/**/*"].inject({}) do |hsh, path| | |
if File.directory? path | |
hsh.update("#{path}/" => :directory) | |
else | |
hsh.update(path => OpenSSL::Digest::MD5.hexdigest(File.read(path))) | |
end | |
end | |
raise 'public/assets is empty: aborting' if assets.size <= 1 | |
fog = Fog::Storage.new(provider: 'AWS') | |
# Replace ASSETS_BUCKET with the name of the S3 bucket for storing assets | |
bucket = fog.directories.get(ASSETS_BUCKET) | |
assets.each do |file, etag| | |
case etag | |
when :directory | |
puts "Directory #{file}" | |
bucket.files.create(key: file, public: true) unless args[:noop] | |
when bucket.files.get(file).try(:etag) | |
puts "Skipping #{file} (identical)" | |
else | |
puts "Uploading #{file}" | |
bucket.files.create(key: file, public: true, body: File.open(file), cache_control: "max-age=#{1.month.to_i}") unless args[:noop] | |
end | |
end | |
bucket.files.each do |object| | |
unless assets.has_key? object.key | |
puts "Removing #{object.key} (no longer exists)" | |
object.destroy unless args[:noop] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment