Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump task.

like image 406
Tilendor Avatar asked May 06 '09 16:05

Tilendor


People also ask

Where are Rake tasks located?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.

What is a rake file?

What is rake? Rake is a native tool for Ruby, similar to Unix's “make”. Written by Jim Weirich, It is used to handle administrative commands or tasks, which are stored either in a Rakefile or in a . rake file. One can write their own rake tasks, specific to their application.

How do I run a rake file?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

How do I create a rake file?

How do I create a rakefile? Well, you create it. Same way as any other file. You open your text editor, write the code, and save it as rakefile .


2 Answers

I know this is an old question, but in any case:

rake -W

This was introduced in rake 0.9.0.

http://rake.rubyforge.org/doc/release_notes/rake-0_9_0_rdoc.html

Support for the –where (-W) flag for showing where a task is defined.

like image 183
Magne Land Avatar answered Oct 11 '22 11:10

Magne Land


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.

like image 49
Tom Lubitz Avatar answered Oct 11 '22 10:10

Tom Lubitz