task name in a rake task

36 views
Skip to first unread message

Santosh

unread,
Apr 13, 2011, 10:18:56 AM4/13/11
to Ruby on Rails: Talk
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.

Bryan Crossland

unread,
Apr 13, 2011, 12:28:12 PM4/13/11
to rubyonra...@googlegroups.com


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.

Santosh c

unread,
Apr 13, 2011, 11:55:17 PM4/13/11
to rubyonra...@googlegroups.com
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

> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages