How do I find the source file for a rake task?

后端 未结 4 1011
名媛妹妹
名媛妹妹 2020-12-24 00:40

I know you can view all possible rake tasks by typing

rake -T

But I need to know what exactly a task does. From the output, how can I find

4条回答
  •  -上瘾入骨i
    2020-12-24 01:17

    Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. To do this, just run something like the following in your code or from a console:

    # load all the tasks associated with the rails app
    Rails.application.load_tasks
    
    # get the source locations of actions called by a task
    task_name = 'db:schema:load' # fully scoped task name
    Rake.application[task_name].actions.map(&:source_location)
    

    This will return the source locations of any code that gets executed for this task. You can also use #prerequisites instead of #source_location to get a list of prerequisite task names (e.g. 'environment', etc).

    You can also list all tasks loaded using:

    Rake.application.tasks
    

    UPDATE: See Magne's good answer below. For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks.

提交回复
热议问题