proto_rake.rdoc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. = Original Prototype Rake
  2. This is the original 100 line prototype rake program.
  3. ---
  4. #!/usr/bin/env ruby
  5. require 'ftools'
  6. class Task
  7. TASKS = Hash.new
  8. attr_reader :prerequisites
  9. def initialize(task_name)
  10. @name = task_name
  11. @prerequisites = []
  12. @actions = []
  13. end
  14. def enhance(deps=nil, &block)
  15. @prerequisites |= deps if deps
  16. @actions << block if block_given?
  17. self
  18. end
  19. def name
  20. @name.to_s
  21. end
  22. def invoke
  23. @prerequisites.each { |n| Task[n].invoke }
  24. execute if needed?
  25. end
  26. def execute
  27. return if @triggered
  28. @triggered = true
  29. @actions.collect { |act| result = act.call(self) }.last
  30. end
  31. def needed?
  32. true
  33. end
  34. def timestamp
  35. Time.now
  36. end
  37. class << self
  38. def [](task_name)
  39. TASKS[intern(task_name)] or fail "Don't know how to rake #{task_name}"
  40. end
  41. def define_task(args, &block)
  42. case args
  43. when Hash
  44. fail "Too Many Target Names: #{args.keys.join(' ')}" if args.size > 1
  45. fail "No Task Name Given" if args.size < 1
  46. task_name = args.keys[0]
  47. deps = args[task_name]
  48. else
  49. task_name = args
  50. deps = []
  51. end
  52. deps = deps.collect {|d| intern(d) }
  53. get(task_name).enhance(deps, &block)
  54. end
  55. def get(task_name)
  56. name = intern(task_name)
  57. TASKS[name] ||= self.new(name)
  58. end
  59. def intern(task_name)
  60. (Symbol === task_name) ? task_name : task_name.intern
  61. end
  62. end
  63. end
  64. class FileTask < Task
  65. def needed?
  66. return true unless File.exist?(name)
  67. latest_prereq = @prerequisites.collect{|n| Task[n].timestamp}.max
  68. return false if latest_prereq.nil?
  69. timestamp < latest_prereq
  70. end
  71. def timestamp
  72. File.new(name.to_s).mtime
  73. end
  74. end
  75. def task(args, &block)
  76. Task.define_task(args, &block)
  77. end
  78. def file(args, &block)
  79. FileTask.define_task(args, &block)
  80. end
  81. def sys(cmd)
  82. puts cmd
  83. system(cmd) or fail "Command Failed: [#{cmd}]"
  84. end
  85. def rake
  86. begin
  87. here = Dir.pwd
  88. while ! File.exist?("Rakefile")
  89. Dir.chdir("..")
  90. fail "No Rakefile found" if Dir.pwd == here
  91. here = Dir.pwd
  92. end
  93. puts "(in #{Dir.pwd})"
  94. load "./Rakefile"
  95. ARGV.push("default") if ARGV.size == 0
  96. ARGV.each { |task_name| Task[task_name].invoke }
  97. rescue Exception => ex
  98. puts "rake aborted ... #{ex.message}"
  99. puts ex.backtrace.find {|str| str =~ /Rakefile/ } || ""
  100. end
  101. end
  102. if __FILE__ == $0 then
  103. rake
  104. end