将多个 rake 任务合并为一个 rake 任务 [英] Combining many rake tasks into one rake task
问题描述
而不是像这样单独运行每个 rake 任务:
Instead of running each rake task individually like this:
rake db:drop
rake db:create
rake db:migrate
rake db:load
我想运行一个可以完成所有任务的 rake 任务.
I want to run one rake task that does all for.
这是我的 rakefile 文件:
This is what I have for my rakefile:
desc 'This rebuilds development db'
namespace :rebuild_dev do
Rake::Task["db:drop"].execute
Rake::Task["db:create"].execute
Rake::Task["db:migrate"].execute
Rake::Task["db:load"].execute
end
以上在我运行时不起作用.
The above doesn't work when I run it.
推荐答案
你想要 invoke
而不是 execute
.摘自我自己的代码,展示了如何传递变量:
You want invoke
not execute
. A little excerpt from my own code showing how to pass variables:
namespace :clients do
task :create, [:client] => ["clients:creation:checks"] do |t, args|
Rake::Task["clients:creation:git"].invoke(client, password)
Rake::Task["server:virtualhost:create"].invoke(client)
Rake::Task["server:virtualhost:enable"].invoke(client)
Rake::Task["server:reload"].invoke
Rake::Task["db:roles:create"].invoke(client, password)
Rake::Task["db:create"].invoke(client, client)
Rake::Task["db:migrate"].invoke(client)
end
end
或者,您可以使任务依赖于另一个任务,就像我上面使用 :create
所做的那样,取决于 clients:creation:checks
.
Alternatively, you can make the task depend upon another task as I have done above with :create
depending upon clients:creation:checks
.
澄清一下,命名空间用于对任务进行分组,因此您必须像上面一样在命名空间中实际定义任务.您不能简单地从命名空间内调用任务.
Just to clarify, a namespace is for grouping tasks, so you must actually define the tasks within the namespace as I have above. You can't simply call tasks from within a namespace.
所以你上面的代码应该是:
So your code above should be:
desc 'This rebuilds development db'
task :rebuild_dev do
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:load"].invoke
end
这篇关于将多个 rake 任务合并为一个 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!