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 || {} @options = options || {}
@run_on = @options[:run_on] || [:start, :change] @run_on = @options[:run_on] || [:start, :change]
@run_on = [@run_on] unless @run_on.respond_to?(:include?) @run_on = [@run_on] unless @run_on.respond_to?(:include?)
@rails_runner = RailsRunner.new
end end
def start def start
runner.start if runner.respond_to? :start
compile_assets if run_for? :start compile_assets if run_for? :start
end end
def reload def reload
@rails_runner.restart_rails runner.reload if runner.respond_to? :reload
compile_assets if run_for? :reload compile_assets if run_for? :reload
end end
@ -32,7 +32,7 @@ module Guard
def compile_assets def compile_assets
puts 'Compiling rails assets' puts 'Compiling rails assets'
result = @rails_runner.compile_assets result = runner.compile_assets
if result if result
Notifier::notify 'Assets compiled' Notifier::notify 'Assets compiled'
@ -41,10 +41,18 @@ module Guard
end end
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 def run_for? command
@run_on.include?(command) @run_on.include?(command)
end end
end 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 class RailsAssets::RailsRunner
def initialize def initialize(options)
boot_rails
end end
# Methods to run the asset pipeline # Methods to run the asset pipeline
@ -29,7 +29,10 @@ module Guard
end end
def boot_rails def boot_rails
@rails_booted ||= begin
require "#{Dir.pwd}/config/environment.rb" require "#{Dir.pwd}/config/environment.rb"
true
end
end end
def run_compiler def run_compiler
@ -47,6 +50,7 @@ module Guard
# #
# @return [ Boolean ] Whether the compilation was successful or not # @return [ Boolean ] Whether the compilation was successful or not
def compile_assets def compile_assets
boot_rails
run_compiler run_compiler
!failed? !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 describe Guard::RailsAssets::RailsRunner do
subject { Guard::RailsAssets::RailsRunner.new({}) }
describe ".compile_assets" do describe ".compile_assets" do
let(:asset_pipeline) { Guard::RailsAssets::RailsRunner::AssetPipeline } let(:asset_pipeline) { Guard::RailsAssets::RailsRunner::AssetPipeline }
@ -19,7 +21,7 @@ describe Guard::RailsAssets::RailsRunner do
asset_pipeline.stub(:precompile) asset_pipeline.stub(:precompile)
end end
it "clean's the assets" do it "cleans the assets" do
asset_pipeline.should_receive(:clean) asset_pipeline.should_receive(:clean)
subject.compile_assets subject.compile_assets
end end

View File

@ -2,13 +2,16 @@ require 'spec_helper'
require 'guard/rails-assets' require 'guard/rails-assets'
describe Guard::RailsAssets do describe Guard::RailsAssets do
let(:options) { {} }
let(:rails_runner) { mock(Guard::RailsAssets::RailsRunner) }
context 'with any runner' do
let(:options) { {:runner => :cli} }
let(:runner) { mock('runner') }
subject { Guard::RailsAssets.new(['watchers'], options) } subject { Guard::RailsAssets.new(['watchers'], options) }
before do before do
Guard::RailsAssets::RailsRunner.stub(:new => rails_runner) Guard::RailsAssets::CliRunner.stub(:new).and_return runner
rails_runner.stub(:compile_assets => true, :restart_rails => true)
end end
describe '#start' do describe '#start' do
@ -39,11 +42,12 @@ describe Guard::RailsAssets do
guard.run_for?(:start).should be_true guard.run_for?(:start).should be_true
guard.run_for?(:reload).should be_false guard.run_for?(:reload).should be_false
end end
end end
describe 'asset compilation using CLI' do describe 'notifications' do
def stub_system_with result def stub_system_with result
rails_runner.should_receive(:compile_assets).and_return result runner.should_receive(:compile_assets).and_return result
end end
it 'should notify on success' do it 'should notify on success' do
@ -54,9 +58,24 @@ describe Guard::RailsAssets do
it 'should notify on failure' do it 'should notify on failure' do
stub_system_with false 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) Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
subject.compile_assets subject.compile_assets
end end
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 end

View File

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