1

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

从命名空间任务调用时,名为的自定义命令capturecmd会中断 Capistrano 的示例跟踪file:replace。适用于任何非命名空间任务:

#⚡ 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 的适当结构,以便我的自定义方法可以与 , 等的现有方法处于同一run级别capture

4

1 回答 1

3

我从来不需要它,但这是交易。当您在 中时Capfileself是对 的实例的引用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
于 2013-02-13T21:34:19.417 回答