You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@buildr.apache.org by Diggory Hardy <li...@dhardy.name> on 2013/10/09 11:29:00 UTC

adding custom hooks to tasks

Hello list,

I'm trying to add some extra operations to tasks. What I'm doing at the moment 
is:

define 'myproject' do
	...
	package(:jar).tap to |jar|
		# do stuff here
		...
	end
end

but I notice that these operations happen even when not running the 'package' 
task — even when I run 'buildr clean'! On the contrary, if I remove the '.tap' 
bit my code never gets run.

Actually, that appears to be the case for the 'package' task. Same if I remove 
the '(:jar)' part. But for the 'compile' task, this works fine (only run when 
compiling):

compile do ||
	# extra stuff...
end

So what gives with the 'package' task?

Re: adding custom hooks to tasks

Posted by Rhett Sutphin <rh...@detailedbalance.net>.
Hi,

On Oct 9, 2013, at 4:29 AM, Diggory Hardy <li...@dhardy.name> wrote:

> Hello list,
> 
> I'm trying to add some extra operations to tasks. What I'm doing at the moment 
> is:
> 
> define 'myproject' do
> 	...
> 	package(:jar).tap to |jar|
> 		# do stuff here
> 		...
> 	end
> end

This:

package(:jar).tap do |jar|
  jar.stuff
end

Is roughly equivalent to:

jar = package(:jar)
jar.stuff

i.e., the contents of a block provided to `tap` are evaluated immediately, as the buildfile is evaluated. 

If you want to add stuff to run after a task executes, use enhance:

package(:jar).enhance do |jar|
  jar.stuff
end

Rhett

> 
> but I notice that these operations happen even when not running the 'package' 
> task — even when I run 'buildr clean'! On the contrary, if I remove the '.tap' 
> bit my code never gets run.
> 
> Actually, that appears to be the case for the 'package' task. Same if I remove 
> the '(:jar)' part. But for the 'compile' task, this works fine (only run when 
> compiling):
> 
> compile do ||
> 	# extra stuff...
> end
> 
> So what gives with the 'package' task?


Re: adding custom hooks to tasks

Posted by Peter Donald <pe...@realityforge.org>.
Hi,

On Wed, Oct 9, 2013 at 8:29 PM, Diggory Hardy <li...@dhardy.name> wrote:
> define 'myproject' do
>         ...
>         package(:jar).tap to |jar|
>                 # do stuff here
>                 ...
>         end
> end
>
> but I notice that these operations happen even when not running the 'package'
> task — even when I run 'buildr clean'! On the contrary, if I remove the '.tap'
> bit my code never gets run.
>
> Actually, that appears to be the case for the 'package' task. Same if I remove
> the '(:jar)' part. But for the 'compile' task, this works fine (only run when
> compiling):
>
> compile do ||
>         # extra stuff...
> end
>
> So what gives with the 'package' task?

I am not sure to the reasoning behind it originally but the package
task behaves differently than most other tasks. Essentially what you
have to do is nest an enhance. ie. The following snippet will out "hi"
after the package is built.

 package(:jar).tap do |jar|
    jar.enhance do
      puts "hi"
    end
  end


-- 
Cheers,

Peter Donald