如何多次执行带有参数的 Rake 任务? [英] How do I execute Rake tasks with arguments multiple times?
问题描述
不可能在循环内调用
相同的rake任务不止一次.但是,我希望能够调用 rake first
并循环遍历一个数组并在每次迭代时使用不同的参数调用 second
.由于 invoke
只在第一次执行,我尝试使用 execute
,但是 Rake::Task#execute 不使用 splat (*) 运算符,只接受一个参数.
It's not possible to invoke
the same rake task from within a loop more than once. But, I want to be able to call rake first
and loop through an array and invoke second
on each iteration with different arguments. Since invoke
only gets executed the first time around, I tried to use execute
, but Rake::Task#execute doesn't use the splat (*) operator and only takes a single argument.
desc "first task"
task :first do
other_arg = "bar"
[1,2,3,4].each_with_index do |n,i|
if i == 0
Rake::Task["foo:second"].invoke(n,other_arg)
else
# this doesn't work
Rake::Task["foo:second"].execute(n,other_arg)
end
end
end
task :second, [:first_arg, :second_arg] => :prerequisite_task do |t,args|
puts args[:first_arg]
puts args[:second_arg]
# ...
end
围绕它的一个技巧是将 execute
的参数放入一个数组中,然后在 second
中检查 args 的结构,但这似乎是 hackish.是否有另一种(更好的?)方法来完成我想做的事情?
One hack around it is to put the arguments to execute
into an array and in second
examine the structure of args, but that seems, well, hackish. Is there another (better?) way to accomplish what I'd like to do?
推荐答案
您可以使用 Rake::Task#reenable 允许它再次被调用.
You can use Rake::Task#reenable to allow it to be invoked again.
desc "first task"
task :first do
other_arg = "bar"
[1,2,3,4].each_with_index do |n,i|
if i == 0
Rake::Task["second"].invoke(n,other_arg)
else
# this does work
Rake::Task["second"].reenable
Rake::Task["second"].invoke(n,other_arg)
end
end
end
task :second, [:first_arg, :second_arg] do |t,args|
puts args[:first_arg]
puts args[:second_arg]
# ...
end
$ 佣金优先
1
bar
2
bar
3
bar
4
bar
这篇关于如何多次执行带有参数的 Rake 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!