Rake newbie needs help

D

DaZoner

I've read the rake docs online and I've got a very simple rakefile to work.
Now I'd like to make it more complex and I can't figure out how to do it.
Can anyone let me know if what I want to do is possible and how to do it?

Why doesn't the following rakefile print "Making ..."?

task :default => [:build]

task :build do
print "In build\n"
file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end
end

Also I'd like to give the filetask prereq's saying only do this step if the
file is out of date with *.rb and data/*.dat. Can I do this? Does it look
like this?

file "zipfile.zip" => ["*.rb","data/*.dat"] do |t|
end

Thanks for your help.
 
T

Tilman Sauerbeck

DaZoner said:
I've read the rake docs online and I've got a very simple rakefile to work.
Now I'd like to make it more complex and I can't figure out how to do it.
Can anyone let me know if what I want to do is possible and how to do it?

Why doesn't the following rakefile print "Making ..."?

task :default => [:build]

task :build do
print "In build\n"
file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end
end

Because rake doesn't see the need to invoke the "newzip.zip" task.
Your build task does *not* depend on the "newzip.zip" task! :)

The following line would add that dependency:
task :build => ["newzip.zip"]
Also I'd like to give the filetask prereq's saying only do this step if the
file is out of date with *.rb and data/*.dat. Can I do this? Does it look
like this?

file "zipfile.zip" => ["*.rb","data/*.dat"] do |t|
end

Yep. This looks like Rake's PackageTask would also help :]
 
J

Jim Weirich

DaZoner said:
I've read the rake docs online and I've got a very simple rakefile to
work.
Now I'd like to make it more complex and I can't figure out how to do it.
Can anyone let me know if what I want to do is possible and how to do it?

Why doesn't the following rakefile print "Making ..."?

task :default => [:build]

task :build do
print "In build\n"
file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end
end

Hi DaZoner ... welcome to Rake!

Let me rewrite your Rakefile and then talk about the changes ... I think
you were wanting something along these lines ...

task :default => [:build]

task :build => ["newzip.zip"] do
puts "Done building"
end

file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end

In your version, you nested the newzip.zip file task inside the build
task. That meant that the task wasn't defined until you invoked build.
That's a perfectly fine thing to do and is great for the dynamic building
of tasks, but its a more advanced concept. I suspect you only want the
build task to invoke the newzip.zip task whenever necessary. So, put both
tasks at the top level and list newzip.zip as a dependency of build.

The other subtle thing is that the message "In Build" prints *after* the
newzip.zip task is complete. The body of a task only executes after all
the prerequisites are finished. I changed the message to read "Build
Done" to emphasize that.
Also I'd like to give the filetask prereq's saying only do this step if
the
file is out of date with *.rb and data/*.dat. Can I do this? Does it look
like this?

file "zipfile.zip" => ["*.rb","data/*.dat"] do |t|
end

Close. "*.rb" is not a file name, but a pattern for a file name. It must
be expanded to a list of files to be useful. You can use a FileList
object to do that ...

file "zipfile.zip" => FileList['*.rb', 'data/*.dat'] do |t| ... end
 
S

Stefan Lang

I've read the rake docs online and I've got a very simple rakefile to work.
Now I'd like to make it more complex and I can't figure out how to do it.
Can anyone let me know if what I want to do is possible and how to do it?

Why doesn't the following rakefile print "Making ..."?

task :default => [:build]

task :build do
print "In build\n"
file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end
end

Also I'd like to give the filetask prereq's saying only do this step if the
file is out of date with *.rb and data/*.dat. Can I do this? Does it look
like this?

file "zipfile.zip" => ["*.rb","data/*.dat"] do |t|
end

Thanks for your help.

I think you really want this:

task :default => ["newzip.zip"]

file "newzip.zip" => ["*.rb", "data/*.dat"] do |t|
puts "Making #{t.name}"
end

The "file" function already defines a task for you.
You don't need to wrap a task around you "file" definition.

When your "build" task was run, it *defined* the "newzip.zip" task.

Stefan
 
S

Stefan Lang

I've read the rake docs online and I've got a very simple rakefile to
work. Now I'd like to make it more complex and I can't figure out how to
do it. Can anyone let me know if what I want to do is possible and how to
do it?

Why doesn't the following rakefile print "Making ..."?

task :default => [:build]

task :build do
print "In build\n"
file "newzip.zip" do |t|
print "Making ",t.name,"\n"
end
end

Also I'd like to give the filetask prereq's saying only do this step if
the file is out of date with *.rb and data/*.dat. Can I do this? Does it
look like this?

file "zipfile.zip" => ["*.rb","data/*.dat"] do |t|
end

Thanks for your help.

I think you really want this:

task :default => ["newzip.zip"]

file "newzip.zip" => ["*.rb", "data/*.dat"] do |t|
^^^^^^^^^^^^^^^^^^^^^^
Sorry. Should be: FileList["*.rb", "data/*.dat"]
 
D

DaZoner

Thanks Jim (and thanks for the tool). I've made changes to my rakefile and
it almost works flawlessly. The only problem is that if I change "*.rb" my
filetask fires but if I change a "reports/*.rb" it does not. (Note how I'm
using "reports/*.rb" instead of the old "data/*.dat" purposely here). Any
idea what I've done wrong? Here is my mini-rakefile that shows the problem:


MyZip = "newzip.zip"

task :default => [:build]

task :build => [MyZip] do
print "Build done\n"
end

dependencies = FileList['*.rb','reports/*.rb']

file MyZip => dependencies do |t|

print "Making ",t.name,"\n"

# zipping code is here

print t.name," made!\n"
end
 
D

DaZoner

Found the problem. My bad. I assumed *.rb would catch *.RB and of course it
doesn't.
 

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
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top