如何声明依赖于参数化任务的 Rake 任务?
我见过一些任务具有参数和依赖项任务的示例,例如:
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 任务,例如:
task :sendLetter => :name
#do something
end
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
How would you pass parameters to the name task if it was a task dependency like:
task :sendLetter => :name
#do something
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参数通过调用堆栈向下传递。您只需要确保您的顶级任务捕获所有依赖项所需的所有参数。在您的情况下,您需要将
first_name
和last_name
放在send_letter
任务中。下面是一个示例,显示在其他地方定义的命名参数流入依赖项(即使它们没有在依赖项中定义),但与顶级任务参数名称不匹配的参数为零。
运行 rake foo[baz] 会产生
有趣的是,在 foo 任务中使用 args.with_defaults(nom: 'Jaques') 会产生对依赖任务没有影响 -
nom
仍然为零。Rake 版本:
rake,版本 10.0.3
Ruby 版本:
ruby 1.9.3p125(2012-02-16 修订版 34643)[x86_64-darwin11.3.0]
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
andlast_name
on thesend_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.
Running
rake foo[baz]
yieldsIt is interesting to note that using
args.with_defaults(nom: 'Jaques')
in thefoo
task has no effect on the dependent task --nom
is still nil.Rake version:
rake, version 10.0.3
Ruby version:
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
您可能会得到的最接近的结果是
或者
您可以执行类似的操作
,但无论您是否请求
sendLetter
,都会调用name
。Task#invoke
仅在尚未调用的情况下调用该任务,并首先调用任何未调用的先决条件。Task#execute
始终调用任务,但不调用任何先决条件。请注意,这些参数不会影响Task#invoke
的一次调用性质:如果您调用参数化任务两次,则无论参数是否不同,它都只会运行一次。The closest you're probably going to get is either
or
You can do something like
but that will invoke
name
regardless of whether you ask forsendLetter
or not.Task#invoke
only calls the task if it hasn't been called and calls any uncalled prereqs first.Task#execute
always calls the task but does not call any prereqs. Note that the parameters don't affect the call-once nature ofTask#invoke
: if you invoke a parameterized task twice, it will only get run once, whether or not the parameters are different.