rational.rdoc 6.1 KB

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