How to run Rake tasks from within Rake tasks? How to run Rake tasks from within Rake tasks? ruby ruby

How to run Rake tasks from within Rake tasks?


If you need the task to behave as a method, how about using an actual method?

task :build => [:some_other_tasks] do  buildendtask :build_all do  [:debug, :release].each { |t| build t }enddef build(type = :debug)  # ...end

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
  • This first resets the task's already_invoked state, allowing the task tothen be executed again, dependencies and all:

    Rake::Task["build"].reenableRake::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)


for example:

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  endend

That should sort you out, just needed the same thing myself.