Remove most of task evaluation code from Task class, rely on Task::DSL.evaluate to get an evaluated task. * Task: remove #evaluate, change constructor prototype to accept actions instead of a block, implement .evaluate(name, env &block); * Implement Task::DSL.evaluate method; * Recipe::DSL: remove tasks evaluation from#evaluate, modify #task to use Task.evaluate to return the new task to be rigstered.
44 lines
917 B
Ruby
44 lines
917 B
Ruby
module Producer
|
|
module Core
|
|
class Task
|
|
class DSL
|
|
class << self
|
|
def evaluate(name, env, &block)
|
|
dsl = new(&block)
|
|
dsl.evaluate(env)
|
|
Task.new(name, dsl.actions)
|
|
end
|
|
|
|
def define_action(keyword, klass)
|
|
define_method(keyword) do |*args|
|
|
@actions << klass.new(@env, *args)
|
|
end
|
|
end
|
|
end
|
|
|
|
define_action :echo, Actions::Echo
|
|
define_action :sh, Actions::ShellCommand
|
|
|
|
attr_accessor :actions
|
|
|
|
def initialize(&block)
|
|
@block = block
|
|
@actions = []
|
|
end
|
|
|
|
def evaluate(env)
|
|
@env = env
|
|
instance_eval &@block
|
|
rescue ConditionNotMetError
|
|
end
|
|
|
|
private
|
|
|
|
def condition(&block)
|
|
fail ConditionNotMetError unless block.call
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|