This commit is contained in:
Steve Hodgkiss 2011-08-01 10:47:58 +01:00
parent 431daf19a0
commit b97bb6c144
3 changed files with 14 additions and 25 deletions

View File

@ -7,24 +7,17 @@ module Guard
def initialize(watchers=[], options={})
super
@options = options || {}
end
def create_rails_runner
@run_on = @options[:run_on] || [:start, :change]
@run_on = [@run_on] unless @run_on.respond_to?(:include?)
@rails_runner = RailsRunner.new
end
def rails_runner
@rails_runner ||= create_rails_runner
end
def start
create_rails_runner
compile_assets if run_for? :start
end
def reload
rails_runner.restart_rails
@rails_runner.restart_rails
compile_assets if run_for? :reload
end
@ -39,7 +32,7 @@ module Guard
def compile_assets
puts 'Compiling rails assets'
result = rails_runner.compile_assets
result = @rails_runner.compile_assets
if result
Notifier::notify 'Assets compiled'
@ -49,10 +42,7 @@ module Guard
end
def run_for? command
run_on = @options[:run_on]
run_on = [:start, :change] if not run_on or run_on.to_s.empty?
run_on = [run_on] unless run_on.respond_to?(:include?)
run_on.include?(command)
@run_on.include?(command)
end
end
end

View File

@ -1,6 +1,5 @@
module Guard
# Keeps rails loaded in a thread waiting to run the asset pipeline compiler
class RailsAssets::RailsRunner
def initialize
@ -29,6 +28,10 @@ module Guard
end
end
def boot_rails
require "#{Dir.pwd}/config/environment.rb"
end
def run_compiler
begin
@failed = false
@ -40,10 +43,6 @@ module Guard
end
end
def boot_rails
require "#{Dir.pwd}/config/environment.rb"
end
# Runs the asset pipeline compiler.
#
# @return [ Boolean ] Whether the compilation was successful or not

View File

@ -3,12 +3,12 @@ require 'guard/rails-assets'
describe Guard::RailsAssets do
let(:options) { {} }
let(:rails_thread) { mock(Guard::RailsAssets::RailsThread) }
let(:rails_runner) { mock(Guard::RailsAssets::RailsRunner) }
subject { Guard::RailsAssets.new(['watchers'], options) }
before do
Guard::RailsAssets::RailsThread.stub(:new => rails_thread)
rails_thread.stub(:compile_assets => true, :restart_rails => true)
Guard::RailsAssets::RailsRunner.stub(:new => rails_runner)
rails_runner.stub(:compile_assets => true, :restart_rails => true)
end
describe '#start' do
@ -43,7 +43,7 @@ describe Guard::RailsAssets do
describe 'asset compilation using CLI' do
def stub_system_with result
rails_thread.should_receive(:compile_assets).and_return result
rails_runner.should_receive(:compile_assets).and_return result
end
it 'should notify on success' do