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-08-02 09:38:59 +00:00
|
|
|
def initialize(options)
|
|
|
|
|
2011-07-31 20:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Methods to run the asset pipeline
|
|
|
|
# Taken from - https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
|
|
|
|
module AssetPipeline
|
|
|
|
extend self
|
|
|
|
extend Rake::DSL
|
|
|
|
|
|
|
|
def clean
|
2011-08-28 20:04:28 +00:00
|
|
|
assets = ::Rails.application.config.assets
|
|
|
|
public_asset_path = File.join(::Rails.public_path, config.assets.prefix)
|
2011-07-31 20:37:09 +00:00
|
|
|
rm_rf public_asset_path, :secure => true
|
|
|
|
end
|
|
|
|
|
|
|
|
def precompile
|
|
|
|
Sprockets::Helpers::RailsHelper
|
2011-08-28 20:04:28 +00:00
|
|
|
::ActionView::Base
|
|
|
|
|
|
|
|
config = ::Rails.application.config
|
|
|
|
env = ::Rails.application.assets
|
|
|
|
target = ::Rails.root.join("public#{config.assets.prefix}")
|
|
|
|
|
|
|
|
config.assets.precompile.each do |path|
|
|
|
|
env.each_logical_path do |logical_path|
|
|
|
|
if path.is_a?(::Regexp)
|
|
|
|
next unless path.match(logical_path)
|
|
|
|
else
|
|
|
|
next unless ::File.fnmatch(path.to_s, logical_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
if asset = env.find_asset(logical_path)
|
|
|
|
filename = target.join(asset.digest_path)
|
|
|
|
mkdir_p filename.dirname
|
|
|
|
asset.write_to(filename)
|
|
|
|
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-31 20:37:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-01 09:47:58 +00:00
|
|
|
def boot_rails
|
2011-08-02 09:38:59 +00:00
|
|
|
@rails_booted ||= begin
|
|
|
|
require "#{Dir.pwd}/config/environment.rb"
|
|
|
|
true
|
|
|
|
end
|
2011-08-01 09:47:58 +00:00
|
|
|
end
|
|
|
|
|
2011-07-31 20:37:09 +00:00
|
|
|
def run_compiler
|
|
|
|
begin
|
|
|
|
@failed = false
|
|
|
|
AssetPipeline.clean
|
|
|
|
AssetPipeline.precompile
|
|
|
|
rescue => e
|
|
|
|
puts "An error occurred compiling assets: #{e}"
|
|
|
|
@failed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Runs the asset pipeline compiler.
|
|
|
|
#
|
|
|
|
# @return [ Boolean ] Whether the compilation was successful or not
|
|
|
|
def compile_assets
|
2011-08-02 09:38:59 +00:00
|
|
|
boot_rails
|
2011-07-31 20:37:09 +00:00
|
|
|
run_compiler
|
|
|
|
|
|
|
|
!failed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed?
|
|
|
|
@failed
|
|
|
|
end
|
|
|
|
end
|
2011-08-02 09:38:59 +00:00
|
|
|
end
|