1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Added support for load and exit callbacks, which get invoked when all recipes have been loaded and when all requested tasks have been executed

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6709 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2007-05-09 05:24:21 +00:00
parent 90e9dc1880
commit 14378caa67
6 changed files with 35 additions and 4 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Added support for load and exit callbacks, which get invoked when all recipes have been loaded and when all requested tasks have been executed [Jamis Buck]
* Added support for start and finish callbacks, which get invoked when any task is called via the command-line [Jamis Buck]
* Make `capify' understand simple command-line switches [Jamis Buck]

View file

@ -10,9 +10,9 @@ module Capistrano
end
def applies_to?(task)
if only.any?
if task && only.any?
return only.include?(task.fully_qualified_name)
elsif except.any?
elsif task && except.any?
return !except.include?(task.fully_qualified_name)
else
return true

View file

@ -27,7 +27,9 @@ module Capistrano
set_pre_vars(config)
load_recipes(config)
config.trigger(:load)
execute_requested_actions(config)
config.trigger(:exit)
config
rescue Exception => error

View file

@ -115,10 +115,12 @@ module Capistrano
# Trigger the named event for the named task. All associated callbacks
# will be fired, in the order they were defined.
def trigger(event, task)
def trigger(event, task=nil)
pending = Array(callbacks[event]).select { |c| c.applies_to?(task) }
if pending.any?
logger.trace "triggering #{event} callbacks for `#{task.fully_qualified_name}'"
msg = "triggering #{event} callbacks"
msg << " for `#{task.fully_qualified_name}'" if task
logger.trace(msg)
pending.each { |callback| callback.call }
end
end

View file

@ -18,6 +18,7 @@ class CLIExecuteTest < Test::Unit::TestCase
@config = stub(:logger => @logger)
@config.stubs(:set)
@config.stubs(:load)
@config.stubs(:trigger)
@cli.stubs(:instantiate_configuration).returns(@config)
end
@ -83,6 +84,16 @@ class CLIExecuteTest < Test::Unit::TestCase
@cli.execute!
end
def test_execute_should_call_load_and_exit_triggers
@cli.options[:actions] = %w(first second)
@config.expects(:find_and_execute_task).with("first", :before => :start, :after => :finish)
@config.expects(:find_and_execute_task).with("second", :before => :start, :after => :finish)
@config.expects(:trigger).never
@config.expects(:trigger).with(:load)
@config.expects(:trigger).with(:exit)
@cli.execute!
end
def test_execute_should_call_handle_error_when_exceptions_occur
@config.expects(:load).raises(Exception, "boom")
@cli.expects(:handle_error).with { |e,| Exception === e }

View file

@ -136,6 +136,20 @@ class ConfigurationCallbacksTest < Test::Unit::TestCase
@config.trigger(:before, task)
end
def test_trigger_without_task_should_invoke_all_callbacks_for_that_event
task = stub(:fully_qualified_name => "any:old:thing")
@config.on(:before, :first)
@config.on(:before, "second:third", :except => "any:old:thing")
@config.on(:before, "this:too", :except => "any:other:thing")
@config.on(:after, :another, "and:another")
@config.expects(:find_and_execute_task).with(:first)
@config.expects(:find_and_execute_task).with("second:third")
@config.expects(:find_and_execute_task).with("this:too")
@config.expects(:find_and_execute_task).with(:another).never
@config.expects(:find_and_execute_task).with("and:another").never
@config.trigger(:before)
end
def test_execute_task_without_named_hooks_should_just_call_task
ns = stub("namespace", :default_task => nil, :name => "old", :fully_qualified_name => "any:old")
task = stub(:fully_qualified_name => "any:old:thing", :name => "thing", :namespace => ns)