我有一个Rakefile以两种方式编译项目,根据全局变量$build_type,它可以是:debug或:release(结果在单独的目录中):

task :build => [:some_other_tasks] do
end

我想创建一个任务,编译项目的两个配置依次,像这样:

task :build_all do
  [ :debug, :release ].each do |t|
    $build_type = t
    # call task :build with all the tasks it depends on (?)
  end
end

是否有一种方法可以像调用方法一样调用任务?或者我怎样才能实现类似的目标?


当前回答

task :build_all do
  [ :debug, :release ].each do |t|
    $build_type = t
    Rake::Task["build"].reenable
    Rake::Task["build"].invoke
  end
end

这应该能解决你的问题,我也需要一样的东西。

其他回答

task :build_all do
  [ :debug, :release ].each do |t|
    $build_type = t
    Rake::Task["build"].execute
  end
end

例如:

Rake::Task["db:migrate"].invoke
task :build_all do
  [ :debug, :release ].each do |t|
    $build_type = t
    Rake::Task["build"].reenable
    Rake::Task["build"].invoke
  end
end

这应该能解决你的问题,我也需要一样的东西。

如果您需要将任务作为一个方法来执行,那么使用一个实际的方法如何?

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的习语,下面是你从过去的答案中编译出来的可能性:

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 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 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)

task :invoke_another_task do
  # some code
  Rake::Task["another:task"].invoke
end