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