晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/alt/ruby32/share/gems/gems/rake-13.0.6/doc/ |
| Current File : //opt/alt/ruby32/share/gems/gems/rake-13.0.6/doc/rational.rdoc |
= Why rake?
Ok, let me state from the beginning that I never intended to write this
code. I'm not convinced it is useful, and I'm not convinced anyone
would even be interested in it. All I can say is that Why's onion truck
must by been passing through the Ohio valley.
What am I talking about? ... A Ruby version of Make.
See, I can sense you cringing already, and I agree. The world certainly
doesn't need yet another reworking of the "make" program. I mean, we
already have "ant". Isn't that enough?
It started yesterday. I was helping a coworker fix a problem in one of
the Makefiles we use in our project. Not a particularly tough problem,
but during the course of the conversation I began lamenting some of the
shortcomings of make. In particular, in one of my makefiles I wanted to
determine the name of a file dynamically and had to resort to some
simple scripting (in Ruby) to make it work. "Wouldn't it be nice if you
could just use Ruby inside a Makefile" I said.
My coworker (a recent convert to Ruby) agreed, but wondered what it
would look like. So I sketched the following on the whiteboard...
"What if you could specify the make tasks in Ruby, like this ..."
task "build" do
java_compile(...args, etc ...)
end
"The task function would register "build" as a target to be made,
and the block would be the action executed whenever the build
system determined that it was time to do the build target."
We agreed that would be cool, but writing make from scratch would be WAY
too much work. And that was the end of that!
... Except I couldn't get the thought out of my head. What exactly
would be needed to make the about syntax work as a make file? Hmmm, you
would need to register the tasks, you need some way of specifying
dependencies between tasks, and some way of kicking off the process.
Hey! What if we did ... and fifteen minutes later I had a working
prototype of Ruby make, complete with dependencies and actions.
I showed the code to my coworker and we had a good laugh. It was just
about a page worth of code that reproduced an amazing amount of the
functionality of make. We were both truly stunned with the power of
Ruby.
But it didn't do everything make did. In particular, it didn't have
timestamp based file dependencies (where a file is rebuilt if any of its
prerequisite files have a later timestamp). Obviously THAT would be a
pain to add and so Ruby Make would remain an interesting experiment.
... Except as I walked back to my desk, I started thinking about what
file based dependencies would really need. Rats! I was hooked again,
and by adding a new class and two new methods, file/timestamp
dependencies were implemented.
Ok, now I was really hooked. Last night (during CSI!) I massaged the
code and cleaned it up a bit. The result is a bare-bones replacement
for make in exactly 100 lines of code.
For the curious, you can see it at ...
* doc/proto_rake.rdoc
Oh, about the name. When I wrote the example Ruby Make task on my
whiteboard, my coworker exclaimed "Oh! I have the perfect name: Rake ...
Get it? Ruby-Make. Rake!" He said he envisioned the tasks as leaves
and Rake would clean them up ... or something like that. Anyways, the
name stuck.
Some quick examples ...
A simple task to delete backup files ...
task :clean do
Dir['*~'].each {|fn| rm fn rescue nil}
end
Note that task names are symbols (they are slightly easier to type
than quoted strings ... but you may use quoted string if you would
rather). Rake makes the methods of the FileUtils module directly
available, so we take advantage of the <tt>rm</tt> command. Also note
the use of "rescue nil" to trap and ignore errors in the <tt>rm</tt>
command.
To run it, just type "rake clean". Rake will automatically find a
Rakefile in the current directory (or above!) and will invoke the
targets named on the command line. If there are no targets explicitly
named, rake will invoke the task "default".
Here's another task with dependencies ...
task :clobber => [:clean] do
rm_r "tempdir"
end
Task :clobber depends upon task :clean, so :clean will be run before
:clobber is executed.
Files are specified by using the "file" command. It is similar to the
task command, except that the task name represents a file, and the task
will be run only if the file doesn't exist, or if its modification time
is earlier than any of its prerequisites.
Here is a file based dependency that will compile "hello.cc" to
"hello.o".
file "hello.cc"
file "hello.o" => ["hello.cc"] do |t|
srcfile = t.name.sub(/\.o$/, ".cc")
sh %{g++ #{srcfile} -c -o #{t.name}}
end
I normally specify file tasks with string (rather than symbols). Some
file names can't be represented by symbols. Plus it makes the
distinction between them more clear to the casual reader.
Currently writing a task for each and every file in the project would be
tedious at best. I envision a set of libraries to make this job
easier. For instance, perhaps something like this ...
require 'rake/ctools'
Dir['*.c'].each do |fn|
c_source_file(fn)
end
where "c_source_file" will create all the tasks need to compile all the
C source files in a directory. Any number of useful libraries could be
created for rake.
That's it. There's no documentation (other than whats in this
message). Does this sound interesting to anyone? If so, I'll continue
to clean it up and write it up and publish it on RAA. Otherwise, I'll
leave it as an interesting exercise and a tribute to the power of Ruby.
Why /might/ rake be interesting to Ruby programmers. I don't know,
perhaps ...
* No weird make syntax (only weird Ruby syntax :-)
* No need to edit or read XML (a la ant)
* Platform independent build scripts.
* Will run anywhere Ruby exists, so no need to have "make" installed.
If you stay away from the "sys" command and use things like
'ftools', you can have a perfectly platform independent
build script. Also rake is only 100 lines of code, so it can
easily be packaged along with the rest of your code.
So ... Sorry for the long rambling message. Like I said, I never
intended to write this code at all.
|