module Ratch module Taskable #def main # @main #end def tasks @tasks ||= {} end # Define a main task. def main(name, &block) @main = Task.register(name, &block) tasks[@main.name] = @main end # Define a task. def task(name, &block) tsk = Task.register(name, &block) tasks[tsk.name] = tsk end # Define a file target. def file(name, &block) tasks[name.to_s] = FileTask.register(name, &block) end end # # # class Task def self.tasks @@tasks ||= {} end def self.register(name_deps, &block) if Hash===name_deps name = name_deps.keys[0] deps = name_deps.values[0] else name = name_deps deps = [] end tasks[name.to_s] = new(name, *deps, &block) end attr :name attr :preqs attr :action # def initialize(name, *preqs, &action) @name = name.to_s @preqs = preqs @action = action end def call #p plan plan.each{ |name| @@tasks[name].action_call } #action_call end # def action_call @action.call if @action end # TODO Check for circular dependencies. def plan(list=[]) if list.include?(name) raise "Circular dependency #{name}." end @preqs.each do |need| need = need.to_s next if list.include?(need) @@tasks[need].plan(list) end list << name return list end end # # # class FileTask < Task end end