--001485f42780cf140a048fea16f6
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Sep 10, 2010 at 9:23 AM, Andrew Wagner <wagner.andrew / gmail.com>wrote:

> Quick question about rake task dependencies. Suppose I have the following:
>
> task :prepare_to_fight :has_weapons do
> ...
> end
>
> task :attack_with_gun :prepare_to_fight do
> ...
> end
>
> task :attack_with_sword :prepare_to_fight do
> ...
> end
>
> Now, by default, if I run "rake attack_with_gun attack_with_sword", it
> recognizes that "prepare_to_fight" has already been called when it goes to
> run the second task. So it saves itself the trouble and skips it.
>
> My question is, is there some way to override that? That is, I want to
> prepare_to_fight each time, but only define it in one place. Note also that
> :prepare_to_fight has a dependency, which I only want to mention in one
> place, so I can't just put this in a method.
>
> Nobody was injured in the making of this completely fictitious, and very
> violent-sounding example...
>

I came up with two ways that seem to work. Not sure which is best


First way uses prepare_to_fight as a method, which invokes the task. Since
it is a method, it will be invoked multiple times. Since it invokes the
other task, it has that dependency, on :has_weapons, and :has_weapons
behaves like a regular task. Downside is that it is clunky feeling, because
you move prepare_to_fight from a task to a method executed inside the block.

task :has_weapons do
  puts "has weapons"
end

def prepare_to_fight
  Rake::Task[:has_weapons].invoke
  puts "prepare to fight"
end

task :attack_with_gun do
  prepare_to_fight
  puts "attack with gun"
end

task :attack_with_sword do
  prepare_to_fight
  puts "attack with sword"
end



--------------------

Second way is mark the task as not having been invoked, each time it is
invoked. Downside here is that it uses private API, so this is subject to
change.


task :has_weapons do
  puts "has weapons"
end

task :prepare_to_fight :has_weapons do
  Rake::Task[:prepare_to_fight].instance_eval { @already_invoked  alse }
  puts "prepare to fight"
end

task :attack_with_gun :prepare_to_fight do
  puts "attack with gun"
end

task :attack_with_sword :prepare_to_fight do
  puts "attack with sword"
end

--001485f42780cf140a048fea16f6--