Merge branch 'master' of http://github.com/oliamb/guard into oliamb-master
Conflicts: lib/guard/guard.rb
This commit is contained in:
commit
01bf7505cd
@ -136,15 +136,16 @@ lib/guard/guard-name.rb inherit from guard/guard and should overwrite at least o
|
|||||||
# = Guard method =
|
# = Guard method =
|
||||||
# ================
|
# ================
|
||||||
|
|
||||||
|
# If one of those methods raise an exception, the Guard instance
|
||||||
|
# will be removed from the active guard.
|
||||||
|
|
||||||
# Call once when guard starts
|
# Call once when guard starts
|
||||||
# Please override initialize method to init stuff
|
# Please override initialize method to init stuff
|
||||||
def start
|
def start
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Call with Ctrl-C signal (when Guard quit).
|
# Call with Ctrl-C signal (when Guard quit)
|
||||||
# This method must return a true value
|
|
||||||
# if everything went well or guard will not stop.
|
|
||||||
def stop
|
def stop
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
25
lib/guard.rb
25
lib/guard.rb
@ -12,10 +12,16 @@ module Guard
|
|||||||
class << self
|
class << self
|
||||||
attr_accessor :options, :guards, :listener
|
attr_accessor :options, :guards, :listener
|
||||||
|
|
||||||
def start(options = {})
|
# initialize this singleton
|
||||||
|
def init(options = {})
|
||||||
@options = options
|
@options = options
|
||||||
@listener = Listener.init
|
@listener = Listener.init
|
||||||
@guards = []
|
@guards = []
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
def start(options = {})
|
||||||
|
init options
|
||||||
|
|
||||||
Dsl.evaluate_guardfile
|
Dsl.evaluate_guardfile
|
||||||
if guards.empty?
|
if guards.empty?
|
||||||
@ -27,13 +33,13 @@ module Guard
|
|||||||
run do
|
run do
|
||||||
guards.each do |guard|
|
guards.each do |guard|
|
||||||
paths = Watcher.match_files(guard, files)
|
paths = Watcher.match_files(guard, files)
|
||||||
guard.run_on_change(paths) unless paths.empty?
|
supervised_task(guard, :run_on_change, paths) unless paths.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
||||||
guards.each { |g| g.start }
|
guards.each { |g| supervised_task(g, :start) }
|
||||||
listener.start
|
listener.start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -54,6 +60,19 @@ module Guard
|
|||||||
UI.error "Could not find gem 'guard-#{name}' in the current Gemfile."
|
UI.error "Could not find gem 'guard-#{name}' in the current Gemfile."
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Let a guard execute his task but
|
||||||
|
# fire it if his work lead to system failure
|
||||||
|
def supervised_task(guard, task_to_supervise, *args)
|
||||||
|
begin
|
||||||
|
guard.send(task_to_supervise, *args)
|
||||||
|
rescue Exception
|
||||||
|
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise.to_s}> command: #{$!}")
|
||||||
|
::Guard.guards.delete guard
|
||||||
|
UI.info("Guard #{guard.class.name} has just been fired")
|
||||||
|
return $!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def locate_guard(name)
|
def locate_guard(name)
|
||||||
`gem open guard-#{name} --latest --command echo`.chomp
|
`gem open guard-#{name} --latest --command echo`.chomp
|
||||||
rescue
|
rescue
|
||||||
|
@ -33,7 +33,6 @@ module Guard
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Call once when guard quit
|
# Call once when guard quit
|
||||||
# Retrieve a true value if the instance successfuly stopped
|
|
||||||
def stop
|
def stop
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
@ -5,25 +5,22 @@ module Guard
|
|||||||
# Run all (Ctrl-\)
|
# Run all (Ctrl-\)
|
||||||
Signal.trap('QUIT') do
|
Signal.trap('QUIT') do
|
||||||
::Guard.run do
|
::Guard.run do
|
||||||
::Guard.guards.each { |g| g.run_all }
|
::Guard.guards.each { |g| ::Guard.supervised_task g, :run_all }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Stop (Ctrl-C)
|
# Stop (Ctrl-C)
|
||||||
Signal.trap('INT') do
|
Signal.trap('INT') do
|
||||||
::Guard.listener.stop
|
::Guard.listener.stop
|
||||||
if ::Guard.guards.all? { |g| g.stop }
|
::Guard.guards.each { |g| ::Guard.supervised_task g, :stop }
|
||||||
UI.info "Bye bye...", :reset => true
|
UI.info "Bye bye...", :reset => true
|
||||||
abort("\n")
|
abort("\n")
|
||||||
else
|
|
||||||
::Guard.listener.start
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Reload (Ctrl-Z)
|
# Reload (Ctrl-Z)
|
||||||
Signal.trap('TSTP') do
|
Signal.trap('TSTP') do
|
||||||
::Guard.run do
|
::Guard.run do
|
||||||
::Guard.guards.each { |g| g.reload }
|
::Guard.guards.each { |g| ::Guard.supervised_task g, :reload }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
|
# mute UI
|
||||||
|
module Guard::UI
|
||||||
|
class << self
|
||||||
|
def info(message, options = {})
|
||||||
|
end
|
||||||
|
|
||||||
|
def error(message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def debug(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe Guard do
|
describe Guard do
|
||||||
|
|
||||||
describe "get_guard_class" do
|
describe "get_guard_class" do
|
||||||
@ -20,4 +34,64 @@ describe Guard do
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "init" do
|
||||||
|
subject { ::Guard.init }
|
||||||
|
|
||||||
|
it "Should retrieve itself for chaining" do
|
||||||
|
subject.should be_kind_of Module
|
||||||
|
end
|
||||||
|
|
||||||
|
it "Should init guards array" do
|
||||||
|
::Guard.guards.should be_kind_of Array
|
||||||
|
end
|
||||||
|
|
||||||
|
it "Should init options" do
|
||||||
|
opts = {:my_opts => true}
|
||||||
|
::Guard.init(opts).options.should be_include :my_opts
|
||||||
|
end
|
||||||
|
|
||||||
|
it "Should init listeners" do
|
||||||
|
::Guard.listener.should be_kind_of Guard::Listener
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "supervised_task" do
|
||||||
|
subject {::Guard.init}
|
||||||
|
|
||||||
|
before :each do
|
||||||
|
@g = mock(Guard::Guard)
|
||||||
|
@g.stub!(:regular).and_return { true }
|
||||||
|
@g.stub!(:spy).and_return { raise "I break your system" }
|
||||||
|
@g.stub!(:pirate).and_raise Exception.new("I blow your system up")
|
||||||
|
@g.stub!(:regular_arg).with("given_path").and_return { "given_path" }
|
||||||
|
subject.guards.push @g
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should let it go when nothing special occurs" do
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
subject.supervised_task(@g, :regular).should be_true
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should let it work with some tools" do
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
subject.supervised_task(@g, :regular).should be_true
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fire the guard on spy act discovery" do
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
::Guard.supervised_task(@g, :spy).should be_kind_of Exception
|
||||||
|
subject.guards.should_not be_include @g
|
||||||
|
::Guard.supervised_task(@g, :spy).message.should == 'I break your system'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fire the guard on pirate act discovery" do
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
::Guard.supervised_task(@g, :regular_arg, "given_path").should be_kind_of String
|
||||||
|
subject.guards.should be_include @g
|
||||||
|
::Guard.supervised_task(@g, :regular_arg, "given_path").should == "given_path"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user