如何声明依赖于参数化任务的 Rake 任务? [英] How do you declare a Rake task that depends on a parameterized task?

查看:49
本文介绍了如何声明依赖于参数化任务的 Rake 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些例子,其中一个任务有参数和一个依赖任务,比如:

I have seen examples where a task has parameters and a dependency task like:

task :name, [:first_name, :last_name] => [:pre_name] do |t, args|
  args.with_defaults(:first_name => "John", :last_name => "Dough")
  puts "First name is #{args.first_name}"
  puts "Last name is #{args.last_name}"
end

如果 name 任务是一个任务依赖项,你将如何将参数传递给它:

How would you pass parameters to the name task if it was a task dependency like:

task :sendLetter => :name
  #do something
end

推荐答案

Args 通过调用堆栈向下传递.您只需要确保您的顶级任务捕获所有依赖项所需的所有参数.在您的情况下,您需要将 first_namelast_name 放在 send_letter 任务上.

Args are passed down through the call stack. You just need to make sure your top-level task captures all the arguments all of the dependencies require. In your case you'll want to put first_name and last_name on the send_letter task.

这是一个示例,显示在其他地方定义的命名参数流入依赖项(即使它们未在依赖项中定义),但与顶级任务参数的名称不匹配的参数为零.

Here is an example that shows named arguments defined elsewhere flowing into the dependency (even if they aren't defined in the dependency), but the argument that doesn't match the name of the top-level task argument is nil.

desc 'Bar'
task :bar, :nom do |task, args|
  puts "BAR NOM: #{args[:nom]}"
  puts "BAR NAME: #{args[:name]}"
end

desc 'Foo'
task :foo, [:name] => :bar do |task, args|
  puts "FOO NAME: #{args[:name]}"
end

运行 rake foo[baz] 收益

BAR NOM: 
BAR NAME: baz
FOO NAME: baz

有趣的是,在 foo 任务中使用 args.with_defaults(nom: 'Jaques') 对依赖任务没有影响——nom 仍然为零.

It is interesting to note that using args.with_defaults(nom: 'Jaques') in the foo task has no effect on the dependent task -- nom is still nil.

Rake 版本:rake,版本 10.0.3

Ruby 版本:ruby 1.9.3p125(2012-02-16 修订版 34643)[x86_64-darwin11.3.0]

这篇关于如何声明依赖于参数化任务的 Rake 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆