Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake: how to output a list of tasks from inside a task?

Tags:

ruby

rake

I'd like my :default task in a Rakefile to be a helpful message that also includes the list of available tasks (the output of rake -T) for people who are not as familiar with rake.

How do you invoke rake -T from inside a task?

like image 907
Lorin Avatar asked Apr 30 '11 19:04

Lorin


2 Answers

Invoking rake -T from within tasks is a bit more complicated in newer versions of rake. The options that need to be set may be derived from rake/lib/application.rb in method standard_rake_options. Basically this boils down to

Rake::TaskManager.record_task_metadata = true

task :default do
  Rake::application.options.show_tasks = :tasks  # this solves sidewaysmilk problem
  Rake::application.options.show_task_pattern = //
  Rake::application.display_tasks_and_comments
end

Note that record_task_metadata cannot be set from within the default task, as it will already be too late when the task is executed (descriptions won’t have been collected, thus those are nil and therefore no task matches the pattern). Trying to reload the Rakefile from within a task will lead to a closed loop. I assume there are performance trade ofs when always collecting metadata. If that’s an issue

task :default do
  system("rake -sT")  # s for silent
end

might be more suitable.

Both work for me using rake 0.9.2.2.

like image 192
Stefan Avatar answered Oct 02 '22 12:10

Stefan


Nevermind. I found the answer once I found the right method.

In addition to calling display_tasks_and_comments you also have to set the regexp to filter the tasks you want to show or by default it will filter them all out.

To make your default task the output of rake -T use the following:

task :default do
  Rake.application.options.show_task_pattern = //
  Rake.application.display_tasks_and_comments()
end
like image 27
Lorin Avatar answered Oct 02 '22 12:10

Lorin