2

How can I run all rake tasks?

task :a do
    # stuff
end
task :b do
    # stuff
end
task :c do
    # stuff
end

task :all do
    # Run all other tasks?
end

I know I can just do

task :all => [:a, :b, :c] do
end

but if I add new task, I also need to add it to :all dependencies. I would like to avoid the need to do it manually since it seems like easy thing to forget.

graywolf
  • 7,092
  • 7
  • 53
  • 77

1 Answers1

2

Here's one way:

namespace :hot_tasks do |hot_tasks_namespace|
  task :task1 do
    puts 1
  end
  task :task2 do
    puts 2
  end
  task :all do
    hot_tasks_namespace.tasks.each do |task|
      Rake::Task[task].invoke
    end
  end
end

running it:

# bundle exec rake hot_tasks:all
1
2

More (not necessarily better) ideas at this question, especially if you're in a rails app.

Community
  • 1
  • 1
burnettk
  • 13,557
  • 4
  • 51
  • 52
  • 1
    1) won't that recursively invoke `:all` task as well? If not why not? 2) If I don't need namespaces, `tasks.each ...` would suffice? – graywolf Apr 10 '17 at 10:25
  • 1
    1) nah, rake de-duplicates tasks pulled in as dependencies, so if you invoke :all, invoking it again programmatically will not run it again. 2) if you don't want namespaces, Rake.application.tasks.each can work. – burnettk Apr 11 '17 at 04:08