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

4

54

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.

Folder answered 6/5, 2009 at 16:18 Comment(0)
M
51

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.

Messing answered 12/4, 2012 at 18:50 Comment(3)
Just FYI: you have to make sure that rake is loaded before you use this method. if it is not, simply do a "require 'rake'".Messing
much obliged, kind sir!Kagu
Nice man! You remember how did you find it out? I was getting stuck inside rake itself for task assets:environment, which thanks to your solution now I know resides in railties-5.0.0.1/lib/rails/application.rb:445. The rake -W was a dead end.Bicipital
L
96

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.

Locket answered 27/4, 2013 at 21:41 Comment(2)
Thanks for updating this, always nice to get a more current answer!Veroniqueverras
Wow, why I didn't know it earlier! Thank you!Klemens
M
51

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.

Messing answered 12/4, 2012 at 18:50 Comment(3)
Just FYI: you have to make sure that rake is loaded before you use this method. if it is not, simply do a "require 'rake'".Messing
much obliged, kind sir!Kagu
Nice man! You remember how did you find it out? I was getting stuck inside rake itself for task assets:environment, which thanks to your solution now I know resides in railties-5.0.0.1/lib/rails/application.rb:445. The rake -W was a dead end.Bicipital
P
6

There is no programmatic way to do this unfortunately. Rake tasks can be loaded either from rails itself, lib/tasks, or from any plugin with a tasks directory.

This should nab most everything not within Rails itself:

find . -name "*.rake" | xargs grep "whatever"

As for db:schema:dump, here's the source:

desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
  require 'active_record/schema_dumper'
  File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

It can be found on line 242 of lib/tasks/database.rake in the rails 2.2.2 gem. If you've got a different version of Rails, just search for "namespace :schema".

You probably actually want the source of the ActiveRecord::SchemaDumper, but I think you should have no trouble figuring out where that is. :-)

Preconcerted answered 6/5, 2009 at 17:58 Comment(0)
P
0

For most rake tasks in Rails, look in the Rails gem directory, in lib/tasks.

If you've vendored Rails into your app directory structure then look in vendor/rails/railties/lib/tasks instead

Either way, db:schema:dump is in databases.rake.

Polyphagia answered 6/5, 2009 at 16:37 Comment(1)
But is there a programmatic way to do this? I ended up doing a find in file search.Folder

© 2022 - 2024 — McMap. All rights reserved.