Rake task dependencies question

A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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...
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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 = 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
 
J

Jean-Julien Fleck

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 t= o
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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
R

Ryan Davis

Quick question about rake task dependencies. Suppose I have the = following:
=20
task :prepare_to_fight =3D> :has_weapons do
...
end
=20
task :attack_with_gun =3D> :prepare_to_fight do
...
end
=20
task :attack_with_sword =3D> :prepare_to_fight do
...
end
=20
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.
=20
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top