Access Rake Task Description from within Task

前端 未结 2 2127
醉酒成梦
醉酒成梦 2021-02-13 18:46

Within a rake task how does one query the description? Something that would give:

desc \"Populate DB\"
task populate: :environment do
  puts task.desc # \"Popula         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 18:57

    Having a similar problem, that I wanted to show the user a customized help screen. The answer here helped me a lot.

    It is very important that

    Rake::TaskManager.record_task_metadata = true
    

    is done before the first definition of tasks.

    Then you can do

    Rake.application.tasks.each do |t|
        printf("%-}s  # %s\n",
               t.name_with_args,
               t.comment)
      end
    

    Details can be found by investigating https://github.com/jimweirich/rake/blob/master/lib/rake/application.rb#L284

提交回复
热议问题