added option to either load Rails or use rake command

This commit is contained in:
Dmytrii Nagirniak 2011-08-02 19:38:59 +10:00
parent b97bb6c144
commit 62ce3735a4
7 changed files with 111 additions and 54 deletions

View File

@ -9,15 +9,15 @@ module Guard
@options = options || {}
@run_on = @options[:run_on] || [:start, :change]
@run_on = [@run_on] unless @run_on.respond_to?(:include?)
@rails_runner = RailsRunner.new
end
def start
runner.start if runner.respond_to? :start
compile_assets if run_for? :start
end
def reload
@rails_runner.restart_rails
runner.reload if runner.respond_to? :reload
compile_assets if run_for? :reload
end
@ -32,7 +32,7 @@ module Guard
def compile_assets
puts 'Compiling rails assets'
result = @rails_runner.compile_assets
result = runner.compile_assets
if result
Notifier::notify 'Assets compiled'
@ -41,10 +41,18 @@ module Guard
end
end
def runner
@runner ||= begin
runner_name = (@options[:runner] || :rails).to_s
require_relative "rails-assets/#{runner_name}_runner"
::Guard::RailsAssets.const_get(runner_name.capitalize + 'Runner').new(@options)
end
end
def run_for? command
@run_on.include?(command)
end
end
end
require 'guard/rails-assets/rails_runner'

View File

@ -0,0 +1,10 @@
module Guard
class RailsAssets::CliRunner
def initialize(options)
end
def compile_assets
system "bundle exec rake assets:clean assets:precompile"
end
end
end

View File

@ -2,8 +2,8 @@ module Guard
class RailsAssets::RailsRunner
def initialize
boot_rails
def initialize(options)
end
# Methods to run the asset pipeline
@ -29,7 +29,10 @@ module Guard
end
def boot_rails
require "#{Dir.pwd}/config/environment.rb"
@rails_booted ||= begin
require "#{Dir.pwd}/config/environment.rb"
true
end
end
def run_compiler
@ -47,6 +50,7 @@ module Guard
#
# @return [ Boolean ] Whether the compilation was successful or not
def compile_assets
boot_rails
run_compiler
!failed?

View File

@ -0,0 +1,12 @@
require 'spec_helper'
describe Guard::RailsAssets::CliRunner do
subject { Guard::RailsAssets::CliRunner.new({}) }
it 'should run the command' do
subject.stub(:system)
subject.should_receive(:system).with("bundle exec rake assets:clean assets:precompile")
subject.compile_assets
end
end

View File

@ -2,6 +2,8 @@ require 'spec_helper'
describe Guard::RailsAssets::RailsRunner do
subject { Guard::RailsAssets::RailsRunner.new({}) }
describe ".compile_assets" do
let(:asset_pipeline) { Guard::RailsAssets::RailsRunner::AssetPipeline }
@ -19,7 +21,7 @@ describe Guard::RailsAssets::RailsRunner do
asset_pipeline.stub(:precompile)
end
it "clean's the assets" do
it "cleans the assets" do
asset_pipeline.should_receive(:clean)
subject.compile_assets
end

View File

@ -2,61 +2,80 @@ require 'spec_helper'
require 'guard/rails-assets'
describe Guard::RailsAssets do
let(:options) { {} }
let(:rails_runner) { mock(Guard::RailsAssets::RailsRunner) }
subject { Guard::RailsAssets.new(['watchers'], options) }
before do
Guard::RailsAssets::RailsRunner.stub(:new => rails_runner)
rails_runner.stub(:compile_assets => true, :restart_rails => true)
end
describe '#start' do
it_behaves_like 'guard command', :command => :start, :run => true
end
context 'with any runner' do
describe '#reload' do
it_behaves_like 'guard command', :command => :reload, :run => false
end
let(:options) { {:runner => :cli} }
let(:runner) { mock('runner') }
subject { Guard::RailsAssets.new(['watchers'], options) }
describe '#run_all' do
it_behaves_like 'guard command', :command => :run_all, :run => false
end
describe '#run_on_change' do
it_behaves_like 'guard command', :command => :run_on_change, :run => true
end
describe 'run options' do
it 'should allow array of symbols' do
guard = Guard::RailsAssets.new(['watchers'], :run_on => [:start, :change])
guard.run_for?(:start).should be_true
guard.run_for?(:reload).should be_false
before do
Guard::RailsAssets::CliRunner.stub(:new).and_return runner
end
it 'should allow symbol' do
guard = Guard::RailsAssets.new(['watchers'], :run_on => :start)
guard.run_for?(:start).should be_true
guard.run_for?(:reload).should be_false
end
end
describe 'asset compilation using CLI' do
def stub_system_with result
rails_runner.should_receive(:compile_assets).and_return result
describe '#start' do
it_behaves_like 'guard command', :command => :start, :run => true
end
it 'should notify on success' do
stub_system_with true
Guard::Notifier.should_receive(:notify).with('Assets compiled')
subject.compile_assets
describe '#reload' do
it_behaves_like 'guard command', :command => :reload, :run => false
end
it 'should notify on failure' do
stub_system_with false
subject.should_not_receive(:`) # don't obtain tree
Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
subject.compile_assets
describe '#run_all' do
it_behaves_like 'guard command', :command => :run_all, :run => false
end
describe '#run_on_change' do
it_behaves_like 'guard command', :command => :run_on_change, :run => true
end
describe 'run options' do
it 'should allow array of symbols' do
guard = Guard::RailsAssets.new(['watchers'], :run_on => [:start, :change])
guard.run_for?(:start).should be_true
guard.run_for?(:reload).should be_false
end
it 'should allow symbol' do
guard = Guard::RailsAssets.new(['watchers'], :run_on => :start)
guard.run_for?(:start).should be_true
guard.run_for?(:reload).should be_false
end
end
describe 'notifications' do
def stub_system_with result
runner.should_receive(:compile_assets).and_return result
end
it 'should notify on success' do
stub_system_with true
Guard::Notifier.should_receive(:notify).with('Assets compiled')
subject.compile_assets
end
it 'should notify on failure' do
stub_system_with false
Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
subject.compile_assets
end
end
end # context with any runner
describe 'picking a runner' do
it 'should use Rails runner by default' do
Guard::RailsAssets.new(['watchers']).runner.class.should == ::Guard::RailsAssets::RailsRunner
end
it 'should use CLI runner' do
Guard::RailsAssets.new(['watchers'], :runner => :cli).runner.class.should == ::Guard::RailsAssets::CliRunner
end
it 'should use RailsRunner' do
Guard::RailsAssets.new(['watchers'], :runner => :rails).runner.class.should == ::Guard::RailsAssets::RailsRunner
end
end
end

View File

@ -2,6 +2,8 @@ require 'rspec'
require 'guard/rails-assets'
require 'support/shared_examples'
require 'support/stdout_helper'
require 'guard/rails-assets/cli_runner'
require 'guard/rails-assets/rails_runner'
RSpec.configure do |config|
config.color_enabled = true