Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 152 lines (116 sloc) 6.216 kb
de564de Initial revision
jimweirich authored
1 = Why rake?
2
3 Ok, let me state from the beginning that I never intended to write this
4 code. I'm not convinced it is useful, and I'm not convinced anyone
5 would even be interested in it. All I can say is that Why's onion truck
6 must by been passing through the Ohio valley.
7
8 What am I talking about? ... A Ruby version of Make.
9
10 See, I can sense you cringing already, and I agree. The world certainly
11 doesn't need yet another reworking of the "make" program. I mean, we
12 already have "ant". Isn't that enough?
13
14 It started yesterday. I was helping a coworker fix a problem in one of
15 the Makefiles we use in our project. Not a particularly tough problem,
16 but during the course of the conversation I began lamenting some of the
17 shortcomings of make. In particular, in one of my makefiles I wanted to
18 determine the name of a file dynamically and had to resort to some
19 simple scripting (in Ruby) to make it work. "Wouldn't it be nice if you
20 could just use Ruby inside a Makefile" I said.
21
22 My coworker (a recent convert to Ruby) agreed, but wondered what it
23 would look like. So I sketched the following on the whiteboard...
24
25 "What if you could specify the make tasks in Ruby, like this ..."
26
27 task "build" do
28 java_compile(...args, etc ...)
29 end
30
31 "The task function would register "build" as a target to be made,
32 and the block would be the action executed whenever the build
33 system determined that it was time to do the build target."
34
35 We agreed that would be cool, but writing make from scratch would be WAY
36 too much work. And that was the end of that!
37
38 ... Except I couldn't get the thought out of my head. What exactly
39 would be needed to make the about syntax work as a make file? Hmmm, you
40 would need to register the tasks, you need some way of specifying
af62240 @rtlechow Clean up trailing whitespace.
rtlechow authored
41 dependencies between tasks, and some way of kicking off the process.
de564de Initial revision
jimweirich authored
42 Hey! What if we did ... and fifteen minutes later I had a working
43 prototype of Ruby make, complete with dependencies and actions.
44
45 I showed the code to my coworker and we had a good laugh. It was just
46 about a page worth of code that reproduced an amazing amount of the
53219ca @rtlechow Typos.
rtlechow authored
47 functionality of make. We were both truly stunned with the power of
de564de Initial revision
jimweirich authored
48 Ruby.
49
50 But it didn't do everything make did. In particular, it didn't have
51 timestamp based file dependencies (where a file is rebuilt if any of its
52 prerequisite files have a later timestamp). Obviously THAT would be a
53 pain to add and so Ruby Make would remain an interesting experiment.
54
55 ... Except as I walked back to my desk, I started thinking about what
53219ca @rtlechow Typos.
rtlechow authored
56 file based dependencies would really need. Rats! I was hooked again,
de564de Initial revision
jimweirich authored
57 and by adding a new class and two new methods, file/timestamp
58 dependencies were implemented.
59
60 Ok, now I was really hooked. Last night (during CSI!) I massaged the
61 code and cleaned it up a bit. The result is a bare-bones replacement
62 for make in exactly 100 lines of code.
63
64 For the curious, you can see it at ...
65 * doc/proto_rake.rdoc
66
67 Oh, about the name. When I wrote the example Ruby Make task on my
68 whiteboard, my coworker exclaimed "Oh! I have the perfect name: Rake ...
69 Get it? Ruby-Make. Rake!" He said he envisioned the tasks as leaves
70 and Rake would clean them up ... or something like that. Anyways, the
71 name stuck.
72
73 Some quick examples ...
74
75 A simple task to delete backup files ...
76
77 task :clean do
0a5d4ac updated docs and comments. getting ready for 0.3.0
jimweirich authored
78 Dir['*~'].each {|fn| rm fn rescue nil}
de564de Initial revision
jimweirich authored
79 end
80
0a5d4ac updated docs and comments. getting ready for 0.3.0
jimweirich authored
81 Note that task names are symbols (they are slightly easier to type
82 than quoted strings ... but you may use quoted string if you would
83 rather). Rake makes the methods of the FileUtils module directly
84 available, so we take advantage of the <tt>rm</tt> command. Also note
85 the use of "rescue nil" to trap and ignore errors in the <tt>rm</tt>
86 command.
de564de Initial revision
jimweirich authored
87
88 To run it, just type "rake clean". Rake will automatically find a
89 Rakefile in the current directory (or above!) and will invoke the
90 targets named on the command line. If there are no targets explicitly
91 named, rake will invoke the task "default".
92
93 Here's another task with dependencies ...
94
95 task :clobber => [:clean] do
0a5d4ac updated docs and comments. getting ready for 0.3.0
jimweirich authored
96 rm_r "tempdir"
de564de Initial revision
jimweirich authored
97 end
98
99 Task :clobber depends upon task :clean, so :clean will be run before
af62240 @rtlechow Clean up trailing whitespace.
rtlechow authored
100 :clobber is executed.
de564de Initial revision
jimweirich authored
101
102 Files are specified by using the "file" command. It is similar to the
103 task command, except that the task name represents a file, and the task
104 will be run only if the file doesn't exist, or if its modification time
105 is earlier than any of its prerequisites.
106
107 Here is a file based dependency that will compile "hello.cc" to
108 "hello.o".
109
110 file "hello.cc"
111 file "hello.o" => ["hello.cc"] do |t|
112 srcfile = t.name.sub(/\.o$/, ".cc")
0a5d4ac updated docs and comments. getting ready for 0.3.0
jimweirich authored
113 sh %{g++ #{srcfile} -c -o #{t.name}}
de564de Initial revision
jimweirich authored
114 end
115
116 I normally specify file tasks with string (rather than symbols). Some
117 file names can't be represented by symbols. Plus it makes the
af62240 @rtlechow Clean up trailing whitespace.
rtlechow authored
118 distinction between them more clear to the casual reader.
de564de Initial revision
jimweirich authored
119
120 Currently writing a task for each and every file in the project would be
121 tedious at best. I envision a set of libraries to make this job
122 easier. For instance, perhaps something like this ...
123
124 require 'rake/ctools'
125 Dir['*.c'].each do |fn|
126 c_source_file(fn)
127 end
128
129 where "c_source_file" will create all the tasks need to compile all the
130 C source files in a directory. Any number of useful libraries could be
131 created for rake.
132
133 That's it. There's no documentation (other than whats in this
134 message). Does this sound interesting to anyone? If so, I'll continue
135 to clean it up and write it up and publish it on RAA. Otherwise, I'll
53219ca @rtlechow Typos.
rtlechow authored
136 leave it as an interesting exercise and a tribute to the power of Ruby.
de564de Initial revision
jimweirich authored
137
0a5d4ac updated docs and comments. getting ready for 0.3.0
jimweirich authored
138 Why /might/ rake be interesting to Ruby programmers. I don't know,
de564de Initial revision
jimweirich authored
139 perhaps ...
140
141 * No weird make syntax (only weird Ruby syntax :-)
142 * No need to edit or read XML (a la ant)
143 * Platform independent build scripts.
144 * Will run anywhere Ruby exists, so no need to have "make" installed.
145 If you stay away from the "sys" command and use things like
146 'ftools', you can have a perfectly platform independent
147 build script. Also rake is only 100 lines of code, so it can
148 easily be packaged along with the rest of your code.
149
150 So ... Sorry for the long rambling message. Like I said, I never
151 intended to write this code at all.
Something went wrong with that request. Please try again.