如何从 Rake 任务中运行 Rake 任务? [英] How to run Rake tasks from within Rake tasks?
问题描述
我有一个Rakefile,根据全局变量$build_type
,以两种方式编译项目,可以是:debug
或:release代码>(结果在单独的目录中):
I have a Rakefile that compiles the project in two ways, according to the global variable $build_type
, which can be :debug
or :release
(the results go in separate directories):
task :build => [:some_other_tasks] do
end
我希望创建一个任务,依次使用两种配置编译项目,如下所示:
I wish to create a task that compiles the project with both configurations in turn, something like this:
task :build_all do
[ :debug, :release ].each do |t|
$build_type = t
# call task :build with all the tasks it depends on (?)
end
end
有没有办法像调用方法一样调用任务?或者我怎样才能实现类似的目标?
Is there a way to call a task as if it were a method? Or how can I achieve anything similar?
推荐答案
如果您需要将任务表现得像方法一样,那么使用实际方法如何?
task :build => [:some_other_tasks] do
build
end
task :build_all do
[:debug, :release].each { |t| build t }
end
def build(type = :debug)
# ...
end
如果您更愿意坚持 rake
的习惯用法,以下是您的可能性,根据过去的答案汇总:
这总是执行任务,但不执行其依赖项:
If you'd rather stick to
rake
's idioms, here are your possibilities, compiled from past answers:This always executes the task, but it doesn't execute its dependencies:
Rake::Task["build"].execute
这个执行依赖项,但只有在以下情况下才执行任务它尚未被调用:
This one executes the dependencies, but it only executes the task if it has not already been invoked:
Rake::Task["build"].invoke
这首先重置任务的 already_invoked 状态,允许任务然后再次执行,依赖和所有:
This first resets the task's already_invoked state, allowing the task to then be executed again, dependencies and all:
Rake::Task["build"].reenable Rake::Task["build"].invoke
请注意,除非重新启用,否则不会自动重新执行已调用的依赖项.在 Rake >= 10.3.2 中,您也可以使用以下命令重新启用它们:
Note that dependencies already invoked are not automatically re-executed unless they are re-enabled. In Rake >= 10.3.2, you can use the following to re-enable those as well:
Rake::Task["build"].all_prerequisite_tasks.each(&:reenable)
这篇关于如何从 Rake 任务中运行 Rake 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!