done with compiling using CLI

This commit is contained in:
Dmytrii Nagirniak 2011-06-17 22:14:17 +10:00
parent db77c93bad
commit 2983323f7a
3 changed files with 55 additions and 16 deletions

View File

@ -5,29 +5,42 @@ module Guard
class RailsAssets < Guard
def initialize(watchers=[], options={})
super
@options = options || {}
end
def start
# Started
compile_assets if run_for? :start
end
def reload
# Ctrl-Z
compile_assets if run_for? :reload
end
def run_all
# Ctr-\ - restarting stuff
compile_assets if run_for? :all
end
def run_on_change(paths)
def run_on_change(paths=[])
compile_assets if run_for? :change
end
def compile_assets
# clean
# prefix or path
# compile
Notifier::notify '1 directory, 2 files', :title => 'Assets compiled'
result = system "rm -rf public/assets && bundle exec rake assets:precompile"
if result
tree = `tree public/assets`
summary = tree.split("\n").last
Notifier::notify summary, :title => 'Assets compiled'
else
Notifier::notify 'see the details in the terminal', :title => "Can't compile assets", :image => :failed
end
end
private
def run_for? command
run_on = @options[:run_on]
run_on = [:start, :all, :change] if not run_on or run_on.empty?
run_on.include?(command)
end
end
end

View File

@ -28,20 +28,24 @@ describe Guard::RailsAssets do
describe 'asset compilation using CLI' do
def stub_system_with result
Kernel.stub(:system).with("rm -rf public/assets && bundle exec rake assets:precompile").and_return result
subject.should_receive(:system).with("rm -rf public/assets && bundle exec rake assets:precompile").and_return result
end
it 'should notify on success' do
stub_system_with true
Kernel.should_receive(:~).with('tree public/assets').and_return "a\nb\n1 directory, 2 files"
subject.should_receive(:`).with('tree public/assets').and_return "a\nb\n1 directory, 2 files"
Guard::Notifier.should_receive(:notify).with('1 directory, 2 files', :title => 'Assets compiled')
subject.compile_assets
end
it 'should notify on failure' do
stub_system_with false
Kernel.should_not_receive(:~)
Kernel.should_not_receive(:`)
Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
subject.compile_assets
end
end
describe 'custom assets prefix' do
it 'should use given prefix'
end
end

View File

@ -1,9 +1,31 @@
shared_examples_for "guard command" do |options|
shared_examples_for "guard command" do |info|
it "should execute #{options[:command]} when said so"
it "should not execute #{options[:command]} when disabled"
def set_run_on_option info
# run_on_change -> change
# run_all -> all
run_option = info[:command].to_s.match(/(.*_)?(\w+)/)[2].to_sym
options[:run_on] = [run_option]
end
it "should #{options[:run] ? '' : 'not '}execute #{options[:command]} by default"
it "should execute built-in helper"
it "should execute #{info[:command]} when said so" do
set_run_on_option info
subject.should_receive(:compile_assets)
subject.send(info[:command])
end
it "should not execute #{info[:command]} when disabled" do
options[:run_on] = [:something_other]
subject.should_not_receive(:compile_assets)
subject.send(info[:command])
end
it "should #{info[:run] ? '' : 'not '}execute #{info[:command]} by default" do
options[:run_on] = nil
if info[:run]
subject.should_receive(:compile_assets)
else
subject.should_not_receive(:compile_assets)
end
subject.send(info[:command])
end
end