为Capistrano定义自定义方法?

时间:2013-02-13 18:36:28

标签: ruby capistrano

我正在尝试为Capistrano定义自定义方法,以便在我的上限任务中使用。 我可以定义方法,然后在任务中使用这些方法就好了,直到我尝试从命名空间的任务调用这样的方法,因为Capistrano然后抛出异常。

示例跟踪,其中名为capturecmd的自定义命令在从命名空间任务file:replace调用时中断了Capistrano。对于任何非命名空间的任务都可以正常工作:

#⚡ cap local file:replace -s dir=./jel-ids/ -s f=deploy -s r=deployed -s ext=.bak
  * 2013-02-13 13:34:08 executing `local'
  * 2013-02-13 13:34:08 executing `file:replace'
Capfile:129:in `capturecmd': uninitialized constant Capistrano::Configuration::Open3 (NameError)
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/namespaces.rb:191:in `method_missing'
    from Capfile:41:in `block (2 levels) in load'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/execution.rb:138:in `instance_eval'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/execution.rb:138:in `invoke_task_directly'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/callbacks.rb:25:in `invoke_task_directly_with_callbacks'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/execution.rb:89:in `execute_task'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/configuration/execution.rb:101:in `find_and_execute_task'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/execute.rb:46:in `block in execute_requested_actions'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/execute.rb:45:in `each'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/execute.rb:45:in `execute_requested_actions'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/help.rb:19:in `execute_requested_actions_with_help'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/execute.rb:34:in `execute!'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/cli/execute.rb:14:in `execute'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/capistrano-2.14.2/bin/cap:4:in `<top (required)>'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/bin/cap:23:in `load'
    from /usr/local/opt/rbenv/versions/1.9.3-p385/bin/cap:23:in `<main>'

编辑:此外,如果任何人都可以建议一个适当的结构来扩展Capistrano,那么我的自定义方法可以与runcapture等的现有方法保持同一级别。

1 个答案:

答案 0 :(得分:3)

我从来不需要它,但这是交易。当您在Capfile时,self是对Capistrano::Configuration实例的引用。您可以使用Capfile中的以下代码来查看它:

p self.class

self的范围内,您可以访问run方法:p self.methods.grep :run将返回:

[:run]

所以,如果你想要一个与run相同范围的方法,你可以在self中进行定义:

def self.my_method
  puts "let's go!"
end

就像我说的,我不知道这是否是最好的方法,但这会奏效。现在您可以使用这样的命名空间任务:

namespace :borba do
  task :teste do
    my_method
  end
end