Rake task dependencies question

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…

On Fri, Sep 10, 2010 at 9:23 AM, Andrew W.
[email protected]wrote:

task :attack_with_sword => :prepare_to_fight do
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 = false
}
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

Hello,

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?

I think you would like to look at the Rake::Task#reenable method:

~> ri Rake::Task#reenable

---------------------------------------------------- Rake::Task#reenable
reenable()

 Reenable the task, allowing its tasks to be executed if the task is
 invoked again.

Cheers,

On Sep 10, 2010, at 07:23 , Andrew W. wrote:

task :attack_with_sword => :prepare_to_fight do
place, so I can’t just put this in a method.
I think it is safe to say that what you’ve described is NOT a task
dependency anymore, and is just a reusable unit of code. In ruby, that’s
just a method:

def prepare_to_fight

end

task :attack_with_gun do
prepare_to_fight
end

task :attack_with_sword do
prepare_to_fight
end

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