Skip to content

Instantly share code, notes, and snippets.

@joeljunstrom
Created November 9, 2010 08:00
Show Gist options
  • Save joeljunstrom/668837 to your computer and use it in GitHub Desktop.
Save joeljunstrom/668837 to your computer and use it in GitHub Desktop.
ugly hack for caching out merged assets for heroku deploy
require 'jsmin'
require 'aws/s3'
require 'yui/compressor'
task :deploy => ['deploy:console']
namespace :deploy do
task :console => :environment do
if Rails.env != 'production'
puts "We need to be running in production environment, ie 'rake RAILS_ENV=production deploy'"
exit 0
end
puts "\n\033[0;31mMake sure you don't have any staged Git commits before deploying\033[0m"
avilable_environments = `git remote`
puts "\nPlease select environment for deploy (should be one of #{avilable_environments.split("\n").join(', ')})"
print ": "
environment = STDIN.gets.chomp
if !avilable_environments.include?(environment)
puts "Unknown environment"
exit 0
end
puts 'Caching assets ..'
Rake::Task['deploy:cache_assets'].invoke
Rake::Task['deploy:commit_cached_assets'].invoke
puts 'Deploying site to Heroku ...'
puts `git push -f #{environment} master`
Rake::Task['deploy:notify_campfire'].invoke
end
task :migrations => [:push, :off, :migrate, :restart, :on, :tag]
task :rollback => [:off, :push_previous, :restart, :on]
task :cache_assets => :environment do
AssetCacheWriter.cache_javascript_and_stylesheets
end
task :commit_cached_assets => :environment do
`git add public/stylesheets/merged/application.css`
`git add public/javascripts/merged/application.js`
`git add public/stylesheets/merged/small_screen.css`
`git commit -m 'Updated static cache of CSS and JS for deploy'`
end
task :migrate do
puts 'Running database migrations ...'
puts `heroku rake db:migrate`
end
task :off do
puts 'Putting the app into maintenance mode ...'
puts `heroku maintenance:on`
end
task :on do
puts 'Taking the app out of maintenance mode ...'
puts `heroku maintenance:off`
end
desc 'notifies campfire room of deploy'
task :notify_campfire do
begin
require 'tinder'
rails_env = fetch(:rails_env, "production")
local_user = ENV['USER'] || ENV['USERNAME']
msg = "[*****] deployed by #{local_user} at #{Time.now}"
puts "Notifying Campfire with '#{msg}'"
campfire = Tinder::Campfire.new 'XXXXXX', :token => '************'
room = campfire.find_room_by_name('*******')
room.speak(msg)
rescue
puts "Notifying Campfire failed: #{$!}"
end
end
end
class AssetCacheWriter
def initialize(file_name)
@cached_file_name = file_name
@path = File.join(Rails.root.to_s,'public',@cached_file_name)
@extname = File.extname(@cached_file_name)
end
def mime_type
{
'.js' => 'text/javascript',
'.css' => 'text/css'
}[@extname]
end
def javascript?
mime_type == 'text/javascript'
end
def minify
if javascript?
compressor = YUI::JavaScriptCompressor.new(:munge => true)
else
compressor = YUI::CssCompressor.new
end
File.open(@path, 'r') {|file| @minified_js = compressor.compress(file) }
File.open(@path, 'w') {|file| file.puts @minified_js }
end
class << self
def trigger_cache
`rm public/stylesheets/merged/application.css` if File.exists?('public/stylesheets/merged/application.css')
`rm public/javascripts/merged/application.js` if File.exists?('public/javascripts/merged/application.js')
`rm public/stylesheets/merged/small_screen.css` if File.exists?('public/stylesheets/merged/small_screen.css')
app = ActionDispatch::Integration::Session.new(MyApp::Application)
app.get '/'
# app.follow_redirect!
end
def cache_javascript_and_stylesheets
AssetCacheWriter.trigger_cache
['javascripts/merged/application.js', 'stylesheets/merged/application.css', 'stylesheets/merged/small_screen.css'].each do |cached_file|
puts cached_file
if File.exists?('public/'+cached_file)
cacher = AssetCacheWriter.new(cached_file)
cacher.minify
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment