Skip to content

Instantly share code, notes, and snippets.

@thenoseman
Created February 11, 2015 09:52
Show Gist options
  • Save thenoseman/65cb7ba867b7299e1e84 to your computer and use it in GitHub Desktop.
Save thenoseman/65cb7ba867b7299e1e84 to your computer and use it in GitHub Desktop.
Compile assets (haml -> html) via rake task
require 'fileutils'
require "haml"
require "sprockets"
class HamlTemplate < Tilt::HamlTemplate
def prepare
@options = @options.merge :format => :html5
super
end
end
# Don't move this line!
Sprockets.register_engine '.haml', HamlTemplate
Rake::Task['assets:precompile'].enhance do
Rake::Task['assets:precompile_static_html'].invoke
#Rake::Task['static'].invoke
end
namespace :assets do
desc 'Compile the static 404 and 500 html template with the asset paths.'
task :precompile_static_html do
invoke_or_reboot_rake_task 'assets:precompile_static_html:all'
end
namespace :precompile_static_html do
def internal_precompile_static_html
# Ensure that action view is loaded and the appropriate
# sprockets hooks get executed
_ = Sprockets::Engines
_ = ActionView::Base
config = Rails.application.config
config.assets.compile = true
config.assets.digest = true
env = Rails.application.assets
target = Rails.public_path
static_files = config.assets.digests.keys.grep(/static/).map { |path| path.gsub("static\/", "") }
compiler = Sprockets::StaticCompiler.new(
env,
target,
static_files,
:manifest_path => config.assets.manifest,
:digest => false,
:manifest => false
)
compiler.compile
end
task :all do
ruby_rake_task('assets:precompile_static_html:primary', false)
end
task :primary => ['assets:environment', 'tmp:cache:clear'] do
internal_precompile_static_html
end
end
end
task :static => ['assets:environment', 'tmp:cache:clear', 'assets:precompile'] do
rm_rf("public/static")
mkdir_p("public/static")
#DO THIS AS PRODUCTION!
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get("/thepage")
all_asset_digests = Rails.application.config.assets.digests
File.open("public/static/404.html", "w") do |handle|
handle.write session.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment