You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@buildr.apache.org by Andrew Moore <rp...@gmail.com> on 2010/01/29 16:58:58 UTC
Task name which called a method
Hi,
I'm a ruby newbie, and I've been using BuildR off and on for a bit
(currently 1.3.3), but I was wondering if there is a way to access the name
of a task that calls a method without passing an argument to that method.
For instance:
def print_task_name()
# print the name of the task that called this function
puts '['+taskName+']'
end
desc 'my-project'
define 'my-project'
desc 'taskA'
task 'taskA' do
print_task_name
end
desc 'taskB'
task 'taskB'
print_task_name
end
end #my-project
My goal is that if I run:
buildr my-project:taskA
that the print_task_name method will print:
[my-project:taskA]
I've been searching through the various Rake, Buildr, and Ruby API's and the
closest I've come is the core ruby 'caller' method. I've spent enough time
trying to figure it out that it's time to ask the experts!
Is what I'm wanting to do possible without adding an argument to the
print_task_name method?
Thanks in advance for your help!
Regards,
Andrew
--
View this message in context: http://n2.nabble.com/Task-name-which-called-a-method-tp4480710p4480710.html
Sent from the Apache Buildr - User mailing list archive at Nabble.com.
Re: Task name which called a method
Posted by Alex Boisvert <al...@gmail.com>.
On Fri, Jan 29, 2010 at 7:58 AM, Andrew Moore <rp...@gmail.com>wrote:
> I'm a ruby newbie, and I've been using BuildR off and on for a bit
> (currently 1.3.3), but I was wondering if there is a way to access the name
> of a task that calls a method without passing an argument to that method.
> For instance:
>
> def print_task_name()
> # print the name of the task that called this function
> puts '['+taskName+']'
> end
>
> desc 'my-project'
> define 'my-project'
>
> desc 'taskA'
> task 'taskA' do
> print_task_name
> end
>
> desc 'taskB'
> task 'taskB'
> print_task_name
> end
>
> end #my-project
>
>
>
> My goal is that if I run:
>
> buildr my-project:taskA
>
> that the print_task_name method will print:
>
> [my-project:taskA]
>
> I've been searching through the various Rake, Buildr, and Ruby API's and
> the
> closest I've come is the core ruby 'caller' method. I've spent enough time
> trying to figure it out that it's time to ask the experts!
>
> Is what I'm wanting to do possible without adding an argument to the
> print_task_name method?
>
>
You should check out how Rote extends Rake to provide
Rake.application.current_task()
http://rote.rubyforge.org/rdoc/
The crux of the extension is the following,
module Rake
class << self
# Array representing current tasks being executed
def task_stack; @tasks ||= []; end
# Reference to current task being executed
def current_task; task_stack.last; end
end
class Task
old_execute = instance_method(:execute)
# Execute the task, loading cached dependencies if not already
# loaded, and handling the task stack.
define_method(:execute) do
begin
Rake.task_stack << self
# ...
old_execute.bind(self).call({ })
ensure
Rake.task_stack.pop
end
end
end
Hope this helps,
alex
Re: Task name which called a method
Posted by Antoine Toulme <an...@lunar-ocean.com>.
In that case I would define the task and its method before, and then the
task name is available by calling self.to_s
You could use a module to make it simple:
module Stuff
def foo
end
end
...
task('bar') do |t|
t.extend Stuff
end
Thanks,
Antoine
On Fri, Jan 29, 2010 at 08:25, Andrew Moore <rp...@gmail.com>wrote:
>
> Thanks for the quick response...
>
> I'm wanting to try and figure out the task name from within a method called
> from the task as opposed to within the task itself.
>
> I had hoped there would be some sort of callstack or scope kept by buildr
> or
> something I could tap into to get that information while within the
> print_task_name method. In my real-world example I'm doing some other
> system
> calls, testing the result and hoping to fail the build with a message that
> has the task name in it. It sounds like I would have to add the '|t|' to
> each task and pass it into each method call. I was hoping to avoid doing
> that for every task and call to my method (keeping it a bit DRYer).
>
> Any other thoughts?
>
> ~ Andrew
> --
> View this message in context:
> http://n2.nabble.com/Task-name-which-called-a-method-tp4480710p4480843.html
> Sent from the Apache Buildr - User mailing list archive at Nabble.com.
>
Re: Task name which called a method
Posted by Andrew Moore <rp...@gmail.com>.
Thanks for the quick response...
I'm wanting to try and figure out the task name from within a method called
from the task as opposed to within the task itself.
I had hoped there would be some sort of callstack or scope kept by buildr or
something I could tap into to get that information while within the
print_task_name method. In my real-world example I'm doing some other system
calls, testing the result and hoping to fail the build with a message that
has the task name in it. It sounds like I would have to add the '|t|' to
each task and pass it into each method call. I was hoping to avoid doing
that for every task and call to my method (keeping it a bit DRYer).
Any other thoughts?
~ Andrew
--
View this message in context: http://n2.nabble.com/Task-name-which-called-a-method-tp4480710p4480843.html
Sent from the Apache Buildr - User mailing list archive at Nabble.com.
Re: Task name which called a method
Posted by Antoine Toulme <an...@lunar-ocean.com>.
I think you can do this:
task 'someTask' do |t|
puts t.to_s
end
On Fri, Jan 29, 2010 at 07:58, Andrew Moore <rp...@gmail.com>wrote:
>
> Hi,
>
> I'm a ruby newbie, and I've been using BuildR off and on for a bit
> (currently 1.3.3), but I was wondering if there is a way to access the name
> of a task that calls a method without passing an argument to that method.
> For instance:
>
>
>
> def print_task_name()
> # print the name of the task that called this function
> puts '['+taskName+']'
> end
>
> desc 'my-project'
> define 'my-project'
>
> desc 'taskA'
> task 'taskA' do
> print_task_name
> end
>
> desc 'taskB'
> task 'taskB'
> print_task_name
> end
>
> end #my-project
>
>
>
> My goal is that if I run:
>
> buildr my-project:taskA
>
> that the print_task_name method will print:
>
> [my-project:taskA]
>
> I've been searching through the various Rake, Buildr, and Ruby API's and
> the
> closest I've come is the core ruby 'caller' method. I've spent enough time
> trying to figure it out that it's time to ask the experts!
>
> Is what I'm wanting to do possible without adding an argument to the
> print_task_name method?
>
> Thanks in advance for your help!
>
> Regards,
>
> Andrew
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://n2.nabble.com/Task-name-which-called-a-method-tp4480710p4480710.html
> Sent from the Apache Buildr - User mailing list archive at Nabble.com.
>