Task name in a rake task

Hi,
does anyone know how one can reference the task being executed and get
its name in the Rakefile. For example, I want this:

puts "You chose the task: " + taskname

task :egg do
puts " 1 egg "
end

task :milk do
puts " 1 glass milk "
end

Depending on the task name, I want to do load certain time consuming
things before the task is executed. Any ideas?

Thanks.

On Wed, Apr 13, 2011 at 9:18 AM, Santosh [email protected] wrote:

task :milk do
puts " 1 glass milk "
end

Depending on the task name, I want to do load certain time consuming
things before the task is executed. Any ideas?

Hi Santosh,

If your tasks are inside of a namespace you already know which one is
being
called. It would not be hard to pass the name of the task to private
function outside the namespace that will do some addition work for you.
Here
is a simple example of what I’m talking about.

namespace :grocery do
task :egg do
shop_list(“egg”)
puts " 1 egg "
end

task :milk do
    shop_list("milk")
  puts " 1 glass milk "
end

end

private

def shop_list(item)
puts “You are looking for #{item}”
end

Let me know if this is not what you are looking for.

B.

Hi brian,
Thanks for the reply. Here’s my requirement more specifically:

We have more than 200 tasks split across 10 different .rake files, and
we “load” them all from the Rakefile. However since these are too many
loading them all takes time (about 10 secs) and so it hurts if we have
to run rake repeatedly. So I am looking for a way to load only the
required .rake file based on the namespace:taskname supplied on the
rake command line.

Is there a way to do this? Thanks for any ideas.

S

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs