2011-08-02 10:53:26 +00:00
|
|
|
require 'rake/dsl_definition'
|
2011-07-31 20:37:09 +00:00
|
|
|
module Guard
|
2011-08-01 09:47:58 +00:00
|
|
|
|
2011-07-31 20:37:09 +00:00
|
|
|
class RailsAssets::RailsRunner
|
2011-10-03 06:31:37 +00:00
|
|
|
include Rake::DSL
|
|
|
|
|
2011-09-02 05:11:26 +00:00
|
|
|
@@rails_booted = false # Only one rails app is allowed, so make it a class var
|
2011-09-04 07:53:43 +00:00
|
|
|
@@rails_env = nil
|
2011-10-10 22:28:23 +00:00
|
|
|
@@digest = nil
|
2011-07-31 20:37:09 +00:00
|
|
|
|
2011-09-04 07:53:43 +00:00
|
|
|
def initialize(options={})
|
|
|
|
@@rails_env = (options[:rails_env] || 'test').to_s unless @@rails_booted
|
2011-10-10 22:28:23 +00:00
|
|
|
@@digest = options[:digest]
|
2011-09-04 07:53:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.apply_hacks
|
|
|
|
# TODO: Hack due to Rails 3.1 issue: https://github.com/rails/rails/issues/2663#issuecomment-1990121
|
|
|
|
ENV["RAILS_GROUPS"] ||= "assets"
|
|
|
|
ENV["RAILS_ENV"] ||= @@rails_env
|
2011-07-31 20:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Methods to run the asset pipeline
|
2011-09-02 05:11:26 +00:00
|
|
|
# See as a reference https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
|
2011-09-04 07:53:43 +00:00
|
|
|
def self.boot_rails
|
2011-09-02 05:11:26 +00:00
|
|
|
return if @@rails_booted
|
2011-09-04 07:53:43 +00:00
|
|
|
puts "Booting Rails for #{@@rails_env} environment."
|
2011-10-03 06:31:37 +00:00
|
|
|
require "fileutils"
|
|
|
|
|
2011-09-04 07:53:43 +00:00
|
|
|
apply_hacks
|
2011-09-02 08:10:02 +00:00
|
|
|
require 'rake'
|
2011-09-02 05:11:26 +00:00
|
|
|
require "#{Dir.pwd}/config/environment.rb"
|
|
|
|
app = ::Rails.application
|
2011-09-04 07:53:43 +00:00
|
|
|
|
|
|
|
app.assets.cache = nil # don't touch my FS pls. (can we use `app.config.assets.cache_store = false` instead)?
|
2011-09-02 05:11:26 +00:00
|
|
|
app.load_tasks
|
|
|
|
@@rails_booted = true
|
2011-08-01 09:47:58 +00:00
|
|
|
end
|
|
|
|
|
2011-10-10 23:06:36 +00:00
|
|
|
def clean
|
|
|
|
Rake::Task["tmp:cache:clear"].execute
|
|
|
|
# copy from the "assets:clean" Rake task
|
|
|
|
config = ::Rails.application.config
|
|
|
|
public_asset_path = File.join(Rails.public_path, config.assets.prefix)
|
|
|
|
rm_rf public_asset_path, :secure => true
|
|
|
|
end
|
2011-07-31 20:37:09 +00:00
|
|
|
|
2011-10-03 06:31:37 +00:00
|
|
|
def precompile
|
2011-10-10 22:28:23 +00:00
|
|
|
config = Rails.application.config
|
|
|
|
unless config.assets.enabled
|
|
|
|
warn "Cannot precompile assets if sprockets is disabled. Enabling it."
|
|
|
|
config.assets.enabled = true
|
|
|
|
end
|
2011-10-03 06:31:37 +00:00
|
|
|
|
2011-10-10 22:28:23 +00:00
|
|
|
# Ensure that action view is loaded and the appropriate
|
|
|
|
# sprockets hooks get executed
|
|
|
|
_ = ActionView::Base
|
2011-10-03 06:31:37 +00:00
|
|
|
|
2011-10-10 22:28:23 +00:00
|
|
|
digest = @@digest.nil? ? config.assets.digest : @@digest
|
2011-10-03 06:31:37 +00:00
|
|
|
|
|
|
|
config.assets.compile = true
|
2011-10-10 22:28:23 +00:00
|
|
|
config.assets.digest = digest
|
2011-10-03 06:31:37 +00:00
|
|
|
config.assets.digests = {}
|
|
|
|
|
2011-10-10 22:28:23 +00:00
|
|
|
env = Rails.application.assets
|
|
|
|
target = File.join(Rails.public_path, config.assets.prefix)
|
|
|
|
compiler = Sprockets::StaticCompiler.new(env,
|
|
|
|
target,
|
|
|
|
config.assets.precompile,
|
|
|
|
:manifest_path => config.assets.manifest,
|
|
|
|
:digest => config.assets.digest,
|
|
|
|
:manifest => config.assets.digest.nil?)
|
|
|
|
compiler.compile
|
2011-10-03 06:31:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-31 20:37:09 +00:00
|
|
|
# Runs the asset pipeline compiler.
|
|
|
|
#
|
|
|
|
# @return [ Boolean ] Whether the compilation was successful or not
|
|
|
|
def compile_assets
|
2011-09-04 07:53:43 +00:00
|
|
|
self.class.boot_rails
|
2011-09-02 05:11:26 +00:00
|
|
|
return false unless @@rails_booted
|
|
|
|
begin
|
2011-10-10 23:06:36 +00:00
|
|
|
clean
|
2011-10-03 06:31:37 +00:00
|
|
|
precompile
|
2011-09-02 05:11:26 +00:00
|
|
|
true
|
|
|
|
rescue => e
|
|
|
|
puts "An error occurred compiling assets: #{e}"
|
|
|
|
false
|
2011-10-03 06:31:37 +00:00
|
|
|
ensure
|
|
|
|
::Rails.application.assets.instance_eval { expire_index! }
|
2011-09-02 05:11:26 +00:00
|
|
|
end
|
2011-07-31 20:37:09 +00:00
|
|
|
end
|
|
|
|
end
|
2011-08-02 09:38:59 +00:00
|
|
|
end
|