2009-10-02 15:07:55 -04:00
|
|
|
= Original Prototype Rake
|
|
|
|
|
|
|
|
This is the original 100 line prototype rake program.
|
|
|
|
|
|
|
|
---
|
|
|
|
#!/usr/bin/env ruby
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
require 'ftools'
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
class Task
|
|
|
|
TASKS = Hash.new
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
attr_reader :prerequisites
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def initialize(task_name)
|
|
|
|
@name = task_name
|
|
|
|
@prerequisites = []
|
|
|
|
@actions = []
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def enhance(deps=nil, &block)
|
|
|
|
@prerequisites |= deps if deps
|
|
|
|
@actions << block if block_given?
|
|
|
|
self
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def name
|
|
|
|
@name.to_s
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def invoke
|
|
|
|
@prerequisites.each { |n| Task[n].invoke }
|
|
|
|
execute if needed?
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def execute
|
|
|
|
return if @triggered
|
|
|
|
@triggered = true
|
|
|
|
@actions.collect { |act| result = act.call(self) }.last
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def needed?
|
|
|
|
true
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def timestamp
|
|
|
|
Time.now
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
class << self
|
|
|
|
def [](task_name)
|
|
|
|
TASKS[intern(task_name)] or fail "Don't know how to rake #{task_name}"
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def define_task(args, &block)
|
|
|
|
case args
|
|
|
|
when Hash
|
|
|
|
fail "Too Many Target Names: #{args.keys.join(' ')}" if args.size > 1
|
|
|
|
fail "No Task Name Given" if args.size < 1
|
|
|
|
task_name = args.keys[0]
|
|
|
|
deps = args[task_name]
|
|
|
|
else
|
|
|
|
task_name = args
|
|
|
|
deps = []
|
|
|
|
end
|
|
|
|
deps = deps.collect {|d| intern(d) }
|
|
|
|
get(task_name).enhance(deps, &block)
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def get(task_name)
|
|
|
|
name = intern(task_name)
|
|
|
|
TASKS[name] ||= self.new(name)
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def intern(task_name)
|
|
|
|
(Symbol === task_name) ? task_name : task_name.intern
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
class FileTask < Task
|
|
|
|
def needed?
|
|
|
|
return true unless File.exist?(name)
|
|
|
|
latest_prereq = @prerequisites.collect{|n| Task[n].timestamp}.max
|
|
|
|
return false if latest_prereq.nil?
|
|
|
|
timestamp < latest_prereq
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def timestamp
|
|
|
|
File.new(name.to_s).mtime
|
|
|
|
end
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def task(args, &block)
|
|
|
|
Task.define_task(args, &block)
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def file(args, &block)
|
|
|
|
FileTask.define_task(args, &block)
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def sys(cmd)
|
|
|
|
puts cmd
|
|
|
|
system(cmd) or fail "Command Failed: [#{cmd}]"
|
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
def rake
|
|
|
|
begin
|
|
|
|
here = Dir.pwd
|
|
|
|
while ! File.exist?("Rakefile")
|
|
|
|
Dir.chdir("..")
|
|
|
|
fail "No Rakefile found" if Dir.pwd == here
|
|
|
|
here = Dir.pwd
|
|
|
|
end
|
|
|
|
puts "(in #{Dir.pwd})"
|
|
|
|
load "./Rakefile"
|
|
|
|
ARGV.push("default") if ARGV.size == 0
|
|
|
|
ARGV.each { |task_name| Task[task_name].invoke }
|
|
|
|
rescue Exception => ex
|
|
|
|
puts "rake aborted ... #{ex.message}"
|
|
|
|
puts ex.backtrace.find {|str| str =~ /Rakefile/ } || ""
|
2012-12-20 21:34:37 -05:00
|
|
|
end
|
2009-10-02 15:07:55 -04:00
|
|
|
end
|
2012-12-20 21:34:37 -05:00
|
|
|
|
2009-10-02 15:07:55 -04:00
|
|
|
if __FILE__ == $0 then
|
|
|
|
rake
|
|
|
|
end
|