How to Find the Source File for a Rake Task

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

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.

rake default task how find source files?

Rails default rake tasks has all the rails default rake files

rake -P lists all the rake tasks

Where is the source code of Rake tasks in Rails 3?

All rails internal rake tasks are in railties/lib/rails/tasks

Rake: Processing files in a specified source directory

I ended up just using an environment variable, it's much easier to work with in my case than Rake's built-in argument implementation. This way I can work with the variable and build my FileList with the prepended path (in the way Cameron suggested in the other answer) outside a task block:

namespace :foofiles do

REQUIRED_FILES = [...]
source_directory = ENV['SOURCEDIR'] || 'vendor/sources'

source_files = FileList[REQUIRED_FILES.map { |relative_path| File.join source_directory, relative_path }]
# Note: FileList can take an array inside the square brackets without needing the splat (*) operator

task process: source_files do
...
end

end

So now I can call it from the terminal like this:

SOURCEDIR=some/folder rake foofiles:process

How do I find the filesystem location of another rake task from a rake task?

As stated above, code from the linked question works, e.g.

namespace :meta do
desc "meta:where[taskname] - where is taskname defined?"
task :where, [:taskname] do |t, args|
puts "looking for #{args.taskname}"
tsk = Rake.application[args.taskname]
# It is mysterious to me why this would need to be so complicated
puts tsk.actions.map(&:source_location)
end
end

But if anyone can explain why the code in the question above doesn't work, or why the source_locations access has to be so complicated, I'd still love to know!

Is there any way to make rake display the source code of a task from the command line?

As I was typing this, I noticed that rake --help gives two options with identical descriptions:

-D, --describe [PATTERN]         Describe the tasks (matching optional PATTERN), then exit.
-W, --where [PATTERN] Describe the tasks (matching optional PATTERN), then exit.

It turns out that rake -W <task> does output the location of the task source, although it sort of looks like it is culled from a stack trace.

This answers the first question. Does anyone know if it's possible to print the task source directly from the command line?

Where are rake tasks defined?

Rake tasks are automatically loaded from the folder structure lib/tasks/*.rake

When we are talking about the task db:migrate for example, it is located within the rails gem in lib/tasks/databases.rake

So for a specific project, you will always have the tasks within the project folder structure as well as all tasks within the specified gems.

where can I find the rake tasks delivered with rails

Figure out where you installed Rails.

$ gem environment

Look for the INSTALLATION_DIRECTORY.

$ cd [INSTALLATION_DIRECTORY]/gems/rails-[VERSION]/lib/tasks

Look for the .rake files in the tasks directory.



Related Topics



Leave a reply



Submit