Overriding Task Commands

(Redirected from OverridingTaskCommands)

!Overriding Task Commands Warning: replacing methods can completely bork default recipes if you don't know what you're doing. You should at the very least read the actual code of the method you are replacing. That said here is how, in a module, you can override a task method.

 # begin snip capfile
 module MyModule
   def self.included(base)
     base.send(:alias_method, :run_without_error_handling, :run)
     base.send(:alias_method, :run, :run_with_error_handling)
   end
   
   def run_with_error_handling(...)
     ...
   end
 end
 
 Capistrano::Configuration.send(:include, MyModule) 
 # end snip capfile

Thank you Jamis (from http://groups.google.com/group/capistrano/browse_thread/thread/47d8dd2cfb7a5b70)


A runnable toy example:

 # begin snip capfile
 module ExampleOverrideModule
   def self.included(base)
     base.send(:alias_method, :old_method_run, :run)
     base.send(:alias_method, :run, :new_method_run)
   end
   
   def new_method_run(str)
     puts str
   end
 end
 
 Capistrano::Configuration.send(:include, ExampleOverrideModule)
 
 task :test_run do
   run "hi"
 end
 # end snip capfile
 % cap test_run
   * executing `test_run'
 hi