added option to either load Rails or use rake command
This commit is contained in:
parent
b97bb6c144
commit
62ce3735a4
@ -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'
|
|
10
lib/guard/rails-assets/cli_runner.rb
Normal file
10
lib/guard/rails-assets/cli_runner.rb
Normal 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
|
@ -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
|
||||||
require "#{Dir.pwd}/config/environment.rb"
|
@rails_booted ||= begin
|
||||||
|
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?
|
||||||
|
12
spec/guard/rails-assets/cli_runner_spec.rb
Normal file
12
spec/guard/rails-assets/cli_runner_spec.rb
Normal 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
|
@ -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
|
||||||
|
@ -2,61 +2,80 @@ 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) }
|
|
||||||
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
|
context 'with any runner' do
|
||||||
it_behaves_like 'guard command', :command => :start, :run => true
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#reload' do
|
let(:options) { {:runner => :cli} }
|
||||||
it_behaves_like 'guard command', :command => :reload, :run => false
|
let(:runner) { mock('runner') }
|
||||||
end
|
subject { Guard::RailsAssets.new(['watchers'], options) }
|
||||||
|
|
||||||
describe '#run_all' do
|
before do
|
||||||
it_behaves_like 'guard command', :command => :run_all, :run => false
|
Guard::RailsAssets::CliRunner.stub(:new).and_return runner
|
||||||
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
|
end
|
||||||
|
|
||||||
it 'should allow symbol' do
|
describe '#start' do
|
||||||
guard = Guard::RailsAssets.new(['watchers'], :run_on => :start)
|
it_behaves_like 'guard command', :command => :start, :run => true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should notify on success' do
|
describe '#reload' do
|
||||||
stub_system_with true
|
it_behaves_like 'guard command', :command => :reload, :run => false
|
||||||
Guard::Notifier.should_receive(:notify).with('Assets compiled')
|
|
||||||
subject.compile_assets
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should notify on failure' do
|
describe '#run_all' do
|
||||||
stub_system_with false
|
it_behaves_like 'guard command', :command => :run_all, :run => false
|
||||||
subject.should_not_receive(:`) # don't obtain tree
|
end
|
||||||
Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
|
|
||||||
subject.compile_assets
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user