lib/ratch/taskable.rb in ratch-0.1 vs lib/ratch/taskable.rb in ratch-0.2.1
- old
+ new
@@ -1,105 +1,115 @@
module Ratch
+ # = Taskable Mixin
+ #
module Taskable
- #def main
- # @main
- #end
-
- def tasks
- @tasks ||= {}
+ # Reference task manager.
+ def task_manager
+ @task_manager ||= TaskManager.new
end
# Define a main task.
-
def main(name, &block)
- @main = Task.register(name, &block)
- tasks[@main.name] = @main
+ name, deps, block = *parse_task_dependencies(name, &block)
+ task_manager.define_main(name, *deps, &block)
end
# Define a task.
-
def task(name, &block)
- tsk = Task.register(name, &block)
- tasks[tsk.name] = tsk
+ name, deps, block = *parse_task_dependencies(name, &block)
+ task_manager.define_task(name, *deps, &block)
end
- # Define a file target.
-
- def file(name, &block)
- tasks[name.to_s] = FileTask.register(name, &block)
+ # Run a task.
+ def run(name)
+ task_manager.call(name)
end
+ private
+
+ #
+ def parse_task_dependencies(name_deps, &block)
+ if Hash===name_deps
+ name = name_deps.keys[0]
+ deps = name_deps.values[0]
+ else
+ name = name_deps
+ deps = []
+ end
+ [name, deps, block]
+ end
end
+ # = TaskManager Class
#
- #
- #
+ class TaskManager
+ attr :main
+ attr :tasks
- class Task
+ def initialize
+ @main = nil
+ @tasks = {}
+ end
- def self.tasks
- @@tasks ||= {}
+ def define_main(name=nil, *depend, &block)
+ @main = Task.new(name, *depend, &block)
+ tasks[@main.name] = @main
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)
+ def define_task(name, *depend, &block)
+ task = Task.new(name, *depend, &block)
+ tasks[task.name] = task
end
- attr :name
- attr :preqs
- attr :action
-
- #
-
- def initialize(name, *preqs, &action)
- @name = name.to_s
- @preqs = preqs
- @action = action
+ def call_main
+ return unless @main
+ call(@main.name)
end
- def call
- #p plan
- plan.each{ |name| @@tasks[name].action_call }
+ # Call task.
+ def call(name)
+ plan(name).each{ |name| @tasks[name].call }
#action_call
end
- #
-
- def action_call
- @action.call if @action
- end
-
- # TODO Check for circular dependencies.
-
- def plan(list=[])
+ # Prepare plan, checking for circular dependencies.
+ def plan(name, list=[])
if list.include?(name)
raise "Circular dependency #{name}."
end
-
- @preqs.each do |need|
+ task = @tasks[name]
+ task.preqs.each do |need|
need = need.to_s
next if list.include?(need)
- @@tasks[need].plan(list)
+ @tasks[need].plan(need, list)
end
- list << name
+ list << task.name
return list
end
end
+ # = Task class
#
- #
- #
+ class Task
- class FileTask < Task
+ attr :name
+ attr :preqs
+ attr :action
+
+ #
+ def initialize(name, *preqs, &action)
+ @name = name.to_s
+ @preqs = preqs
+ @action = action
+ end
+
+ #
+ def call
+ @action.call if @action
+ end
+
end
-end
+end #module Ratch