From 90e9dc188009d86acc142d3ac646a65b7eb79b2e Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Wed, 9 May 2007 05:14:37 +0000 Subject: [PATCH] Added support for start and finish callbacks, which get invoked when any task is called via the command-line git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6708 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- CHANGELOG | 2 ++ lib/capistrano/cli/execute.rb | 5 ++++- lib/capistrano/configuration/execution.rb | 9 +++++++-- test/cli/execute_test.rb | 4 ++-- test/configuration/execution_test.rb | 14 ++++++++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f61136b9..328802e9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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] * Make the server definition itself available to SSH channels, rather than just the host name [Jamis Buck] diff --git a/lib/capistrano/cli/execute.rb b/lib/capistrano/cli/execute.rb index ab07db97..894cc3fd 100644 --- a/lib/capistrano/cli/execute.rb +++ b/lib/capistrano/cli/execute.rb @@ -36,7 +36,10 @@ module Capistrano def execute_requested_actions(config) Array(options[:vars]).each { |name, value| config.set(name, value) } - Array(options[:actions]).each { |action| config.find_and_execute_task(action) } + + Array(options[:actions]).each do |action| + config.find_and_execute_task(action, :before => :start, :after => :finish) + end end def set_pre_vars(config) #:nodoc: diff --git a/lib/capistrano/configuration/execution.rb b/lib/capistrano/configuration/execution.rb index 9a86da2a..6a70491a 100644 --- a/lib/capistrano/configuration/execution.rb +++ b/lib/capistrano/configuration/execution.rb @@ -85,9 +85,14 @@ module Capistrano # Attempts to locate the task at the given fully-qualified path, and # execute it. If no such task exists, a Capistrano::NoSuchTaskError will # be raised. - def find_and_execute_task(path) + def find_and_execute_task(path, hooks={}) task = find_task(path) or raise NoSuchTaskError, "the task `#{path}' does not exist" - execute_task(task) + + trigger(hooks[:before], task) if hooks[:before] + result = execute_task(task) + trigger(hooks[:after], task) if hooks[:after] + + result end protected diff --git a/test/cli/execute_test.rb b/test/cli/execute_test.rb index 1807d318..6bf5e9cd 100644 --- a/test/cli/execute_test.rb +++ b/test/cli/execute_test.rb @@ -78,8 +78,8 @@ class CLIExecuteTest < Test::Unit::TestCase @cli.options[:actions] = %w(first second) @config.expects(:set).with(:foo, "bar") @config.expects(:set).with(:baz, "bang") - @config.expects(:find_and_execute_task).with("first") - @config.expects(:find_and_execute_task).with("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) @cli.execute! end diff --git a/test/configuration/execution_test.rb b/test/configuration/execution_test.rb index 47a245d4..e17db420 100644 --- a/test/configuration/execution_test.rb +++ b/test/configuration/execution_test.rb @@ -127,6 +127,20 @@ class ConfigurationExecutionTest < Test::Unit::TestCase assert_nothing_raised { @config.find_and_execute_task("path:to:task") } end + def test_find_and_execute_task_with_before_option_should_trigger_callback + @config.expects(:find_task).with("path:to:task").returns(:found) + @config.expects(:trigger).with(:incoming, :found) + @config.expects(:execute_task).with(:found) + @config.find_and_execute_task("path:to:task", :before => :incoming) + end + + def test_find_and_execute_task_with_after_option_should_trigger_callback + @config.expects(:find_task).with("path:to:task").returns(:found) + @config.expects(:trigger).with(:outgoing, :found) + @config.expects(:execute_task).with(:found) + @config.find_and_execute_task("path:to:task", :after => :outgoing) + end + private def stack_inspector