Created
October 11, 2013 04:27
-
-
Save jeffrafter/6929594 to your computer and use it in GitHub Desktop.
Asset precompile and upload task
This file contains hidden or 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
Rake::Task['assets:precompile:all'].clear | |
namespace :assets do | |
namespace :precompile do | |
task :all do | |
Rake::Task['assets:precompile:primary'].invoke | |
# ruby_rake_task("assets:precompile:nondigest", false) if Rails.application.config.assets.digest | |
end | |
end | |
end | |
namespace :assets do | |
task :sync do | |
ENV["DEPLOY"] ||= 'production' | |
ENV["BRANCH"] ||= 'master' | |
require 'settings' | |
Settings.load! | |
connection = Fog::Storage.new({ | |
:aws_access_key_id => Settings.amazon["access_key_id"], | |
:aws_secret_access_key => Settings.amazon["secret_access_key"], | |
:provider => "AWS" | |
}) | |
bucket = connection.directories.get("assets.wantful.com") | |
manifest_file = bucket.files.get("assets/manifest-#{ENV["DEPLOY"]}-#{ENV["BRANCH"]}.yml") | |
manifest_file ||= bucket.files.get("assets/manifest-production-master.yml") | |
manifest_sources_file = bucket.files.get("assets/sources_manifest-#{ENV["DEPLOY"]}-#{ENV["BRANCH"]}.yml") | |
manifest_sources_file ||= bucket.files.get("assets/sources_manifest-production-master.yml") | |
`mkdir -p public/assets` | |
File.open(Rails.root.join("tmp/manifest.yml"), "w") { |f| f.write(manifest_file.body) } if manifest_file | |
File.open(Rails.root.join("tmp/sources_manifest.yml"), "w") { |f| f.write(manifest_sources_file.body) } if manifest_sources_file | |
manifest = YAML.load(manifest_file.body) rescue {} | |
manifest_entries = manifest.values | |
local_path = "#{Rails.root.to_s}/tmp/assets" | |
s3_path = "assets/" | |
Rake::Task['assets:precompile'].invoke | |
`rm -rf tmp/assets` | |
`mv -f public/assets tmp/` | |
Dir[local_path + "/**/**"].each do |file| | |
file_name = file.match(/assets\/(.*)/)[1] | |
next if File.directory?(file) | |
# Don't want to upload the manifest.yml file just yet... | |
next if file_name == "manifest.yml" || file_name == "sources_manifest.yml" | |
# maybe we don't want this, but these files don't show up in the manifest | |
next if File.extname(file_name) == ".gz" | |
# let's not upload the file if it's already in the manifest | |
# that means it hasn't changed | |
next if manifest_entries.include?(file_name) || manifest.keys.include?(file_name) | |
content_type = "" | |
content_type = "text/css" if File.extname(file_name) == ".css" | |
content_type = "text/javascript" if File.extname(file_name) == ".js" | |
content_type = "image/png" if File.extname(file_name) == ".png" | |
content_type = "image/jpeg" if File.extname(file_name) == ".jpeg" | |
content_type = "image/jpeg" if File.extname(file_name) == ".jpg" | |
content_type = "image/gif" if File.extname(file_name) == ".gif" | |
content_type = "image/x-icon" if File.extname(file_name) == ".ico" | |
content_type = "text/xml" if File.extname(file_name) == ".xml" | |
content_type = "text/plain" if File.extname(file_name) == ".txt" | |
content_type = "audio/mpeg" if File.extname(file_name) == ".mp3" | |
content_type = "application/pdf" if File.extname(file_name) == ".pdf" | |
content_type = "application/x-shockwave-flash" if File.extname(file_name) == ".swf" | |
@changed ||= true | |
logger.info "\e[1;40mUploading: #{file_name}\e[0m" | |
upload_to = File.join(s3_path, file_name) | |
bucket.files.create({ | |
:key => upload_to, | |
:body => File.read(file), | |
:metadata => {'Cache-Control' => 'max-age=31536000, public', 'Content-Type' => content_type}, | |
:public => true | |
}) | |
non_digest_name = file_name.gsub(/-[0-9a-f]{32}/, "") | |
logger.info "\e[1;30mUploading without digest: #{non_digest_name}\e[0m" | |
logger.info "" | |
upload_to = File.join(s3_path, non_digest_name) | |
bucket.files.create({ | |
:key => upload_to, | |
:body => File.read(file), | |
:metadata => {'Cache-Control' => 'max-age=31536000, public', 'Content-Type' => content_type}, | |
:public => true | |
}) | |
end | |
if @changed | |
logger.info "Uploading manifest.yml" | |
bucket.files.create({ | |
:key => "assets/manifest-#{ENV["DEPLOY"]}-#{ENV["BRANCH"]}.yml", | |
:body => File.read(Rails.root.join("tmp/manifest.yml")), | |
:public => true | |
}) | |
logger.info "Uploading sources_manifest.yml" | |
bucket.files.create({ | |
:key => "assets/sources_manifest-#{ENV["DEPLOY"]}-#{ENV["BRANCH"]}.yml", | |
:body => File.read(Rails.root.join("tmp/sources_manifest.yml")), | |
:public => true | |
}) | |
else | |
logger.info "Nothing changed in this precompile" | |
end | |
end | |
def logger | |
Rails.env.test? ? Rails.logger : Logger.new(STDOUT) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment