为什么 Rake 无法连续调用多个任务?
我有一个 Rake 任务,我在下面简化了它。 我在 Windows 上使用 Ruby 1.9。
也许您想猜测下面调用 Rake 任务“list_all_levels”的结果?它应该是:
"Hello level 1"
"Hello level 2"
"Hello level 3"
但由于我不知道的原因,它只打印“Hello level 1”,然后停止。
也就是说,它始终只调用第一个任务。 如果我更改第一行以传递参数“42”,它将打印“Hello level 42”,然后停止。
我想知道为什么它不调用任务 3 次并打印所有 3 行? 有什么办法让它按照我的预期工作吗?
task :list_all_levels => [] do
Rake::Task[:list].invoke 1
Rake::Task[:list].invoke 2
Rake::Task[:list].invoke 3
end
task :list, [:level] => [] do |t, args|
puts "Hello level #{args.level}"
end
I have a Rake task which I have simplified below.
I'm using Ruby 1.9 on Windows.
Perhaps you would like to guess the outcome of calling the Rake task "list_all_levels" below? It should be:
"Hello level 1"
"Hello level 2"
"Hello level 3"
But for reasons unknown to me, it prints only "Hello level 1" and then stops.
That is, it always invokes only the first task.
If I change the first line to pass the arg "42", it would print "Hello level 42" and then stop.
I'm wondering why does it not invoke the task 3 times and print all 3 lines?
And is there any way to get it to work how I would expect?
task :list_all_levels => [] do
Rake::Task[:list].invoke 1
Rake::Task[:list].invoke 2
Rake::Task[:list].invoke 3
end
task :list, [:level] => [] do |t, args|
puts "Hello level #{args.level}"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
invoke
仅调用如果需要,则执行任务。运行rake --trace
显示:因此您可以看到它尝试再调用任务
:list
两次。但您可以做的一件事是将主任务的主体更改为:然后再次需要
:list
任务,并且它会正确打印出所有 3 个语句。更简洁的方法是 使用
execute
而不是invoke
:这会将您的
puts
语句更改为仅使用args
而不是args .level
由于某种原因。在上面的链接中描述了使用execute
而不是invoke
时还有一些其他注意事项。The issue is that
invoke
only invokes the task if it is needed. Runningrake --trace
shows:So you can see it's trying to invoke the task
:list
two more times. But one thing you can do is to change the body of the main task to:then the
:list
task is needed again and it correctly prints out all 3 statements.The cleaner way to do it is to use
execute
rather thaninvoke
:That changes your
puts
statement to use justargs
rather thanargs.level
for some reason. There are some other caveats with usingexecute
overinvoke
described in the link above.在新的执行之前,您需要重新启用该任务,为此,您只需执行 reenable 方法即可。就像用户 Mark Rushakoff 提到的那样。
You need to re-enable the task before the new execution, for this, you just need to execute the method reenable. Like the user Mark Rushakoff mentioned.