1

I am basically trying to convert a solution to this question into a rake task: How do I find the source file for a rake task?

I want to type, e.g.:

rake meta:where[test]

And get the path where the test task is defined. Here's what I have, with some unnecessary stuff so I can make some relevant comments:

Rake::TaskManager.record_task_metadata=true

namespace :meta do
  desc "meta:where[taskname] - where is taskname defined?"
  task :where, [:taskname] do |t, args|
    tsk = Rake.application.tasks.each.find {|t| t.name == args.taskname}
    puts tsk
    tsk.locations.each {|x| puts x}
  end
end

So, puts tsk actually outputs 'test', which means (I think!) I'm correctly locating the task here. It's no trouble printing the path from the rails console using the same code (as in the above-mentioned SO post). It says something like:

/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/test_unit/testing.rake:49:in `<top (required)>'

Any idea how I can get the location(s) to print?

Community
  • 1
  • 1
Dav Clark
  • 1,430
  • 1
  • 13
  • 26

1 Answers1

2

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!

Dav Clark
  • 1,430
  • 1
  • 13
  • 26