为什么 Rake 不能连续调用多个任务? [英] Why is Rake not able to invoke multiple tasks consecutively?

查看:39
本文介绍了为什么 Rake 不能连续调用多个任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rake 任务,我在下面进行了简化.我在 Windows 上使用 Ruby 1.9.

也许您想猜猜下面调用 Rake 任务list_all_levels"的结果?应该是:

你好,级别 1"你好,2级"你好,3级"

但由于我不知道的原因,它只打印Hello level 1"然后停止.

也就是说,它总是只调用第一个任务.如果我更改第一行以传递参数42",它将打印Hello level 42"然后停止.

我想知道为什么它调用任务 3 次并打印所有 3 行?有什么方法可以让它按照我的预期工作吗?

task :list_all_levels =>[] 做Rake::Task[:list].invoke 1Rake::Task[:list].invoke 2Rake::Task[:list].invoke 3结尾任务:列表,[:级别] =>[] 做 |t, args|puts "Hello level #{args.level}"结尾

解决方案

问题在于 invoke 仅在需要时调用该任务.运行 rake --trace 显示:

<前>(在/tmp/ruby 中)** 调用默认值(first_time)** 执行默认** 调用列表(first_time)** 执行列表你好 1 级** 调用列表** 调用列表

所以你可以看到它试图再调用任务 :list 两次.但是您可以做的一件事是将主要任务的正文更改为:

task :default =>[] 做Rake::Task[:list].invoke 1Rake::Task[:list].reenableRake::Task[:list].invoke 2Rake::Task[:list].reenableRake::Task[:list].invoke 3结尾

然后再次需要 :list 任务,它正确打印出所有 3 个语句.

更简洁的方法是使用 execute 而不是 invoke:

task :default =>[] 做Rake::Task[:list].execute 1Rake::Task[:list].execute 2Rake::Task[:list].execute 3结尾任务:列表,[:级别] =>[] 做 |t, args|puts "Hello level #{args}"结尾

出于某种原因,这将您的 puts 语句更改为仅使用 args 而不是 args.level.在上面的链接中描述了使用 execute 而不是 invoke 的其他一些注意事项.

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

解决方案

The issue is that invoke only invokes the task if it is needed. Running rake --trace shows:

(in /tmp/ruby)
** Invoke default (first_time)
** Execute default
** Invoke list (first_time)
** Execute list
Hello level 1
** Invoke list 
** Invoke list 

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:

task :default => [] do
    Rake::Task[:list].invoke 1
    Rake::Task[:list].reenable
    Rake::Task[:list].invoke 2
    Rake::Task[:list].reenable
    Rake::Task[:list].invoke 3
end

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 than invoke:

task :default => [] do
    Rake::Task[:list].execute 1
    Rake::Task[:list].execute 2
    Rake::Task[:list].execute 3
end

task :list, [:level] => [] do |t, args|
    puts "Hello level #{args}"
end

That changes your puts statement to use just args rather than args.level for some reason. There are some other caveats with using execute over invoke described in the link above.

这篇关于为什么 Rake 不能连续调用多个任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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