Skip to content

Instantly share code, notes, and snippets.

@sl4m
Forked from chuckbjones/deploy.rb
Created August 15, 2013 23:37
Show Gist options
  • Save sl4m/6245968 to your computer and use it in GitHub Desktop.
Save sl4m/6245968 to your computer and use it in GitHub Desktop.
# define a method to run rake tasks
def run_rake(task, options={}, &block)
rake = fetch(:rake, 'rake')
rails_env = fetch(:rails_env, 'production')
command = "cd #{current_path} && #{rake} #{task} RAILS_ENV=#{rails_env}"
run(command, options, &block)
end
# generate static html files
after "deploy:create_symlink", "static:generate"
namespace :static do
desc "Generate static html files and put them in /public/"
task :generate do
run_rake 'static:generate'
end
end
# adapted from http://kill-0.com/duplo/2010/01/29/a-different-take-on-custom-error-pages-in-ruby-on-rails/
namespace :static do
desc "Generate static pages and save them in /public"
task :generate => :environment do
require "rails/console/app"
require "rails/console/helpers"
extend Rails::ConsoleMethods
urls_and_paths.each do |url, path|
r = app.get(url)
if 200 == r
File.open(Rails.public_path + path, "w") do |f|
f.write(app.response.body)
end
else
$stderr.puts "Error generating static file #{path} #{r.inspect}"
end
end
end
end
private
def urls_and_paths
Dir.glob("#{Rails.root}/app/views/static/*.html.erb").map do |file|
file = File.basename(file, '.html.erb')
["/static/#{file}", "/#{file}.html"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment