【问题标题】:Capistrano task from another task with parametersCapistrano 任务来自另一个带有参数的任务
【发布时间】:2013-07-16 07:57:28
【问题描述】:

我的问题类似于How do I invoke one Capistrano task from another?

我想要的额外的事情是能够在从 foo 调用 bar 时将参数传递给它:

task :foo do
  # this calls bar, I would like to pass params (i.e n = 10)
  # as if I were calling cap bar -s n=10
  # bar does not take arguments
  bar
end

task :bar do
  if exists?(:n)
    puts "n is: #{n}"
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 capistrano


    【解决方案1】:

    在 capistrano 3.x 中

    desc "I accept a parameter"
    task :foo, :foo_param do |t, args|
      foo_param = args[:foo_param]
      puts "I am #{foo_param}"
    end
    
    desc "I call the foo task"
    task :bar do
      invoke("foo", "batman")
      # prints "I am batman"
    end
    

    【讨论】:

      【解决方案2】:

      Capistrano 任务无法真正参数化。你可以定义一个辅助方法,如下:

      task :foo do
        bar(10)
      end
      
      def bar(n=variables[:n])
        puts "N is #{n}"  
      end
      

      如果您对将 :bar 也作为一项任务下定决心,请尝试以下技巧:

      task :foo do
        bar(10)
      end
      
      task :bar { bar }
      
      def bar(n=variables[:n])
        puts "N is #{n}"  
      end
      

      请注意,任务必须在方法之前声明。

      【讨论】:

        猜你喜欢
        • 2014-04-25
        • 1970-01-01
        • 2011-05-19
        • 2018-12-05
        • 2021-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-22
        相关资源
        最近更新 更多