Thibault Jouan f0e144cebd Refactor and simplify recipe DSL evaluation usages:
Remove most of recipe evaluation code in Recipe class, and rely on
Recipe::DSL to get evaluated recipes.

* Remove Recipe#evaluate call from CLI, rely on
  Recipe.evaluate_from_file to get the evaluated recipe;
* Implement Recipe.evaluate_from_file(filepath, env);
* Implement Recipe::DSL.evaluate(code, env);
* Remove code and filepath accessor on Recipe;
* Remove Recipe.from_file and Recipe#evaluate methods;
* Move task evaluations in Recipe::DSL#evaluate;
* Modify Recipe constructor so that it accepts tasks as argument.
2013-08-14 19:48:43 +00:00

50 lines
940 B
Ruby

module Producer
module Core
class Recipe
class DSL
attr_reader :tasks
def self.evaluate(code, env)
dsl = new(code).evaluate(env)
Recipe.new(dsl.tasks)
end
def initialize(code = nil, &block)
@code = code
@block = block
@tasks = []
end
def evaluate(env)
@env = env
if @code
instance_eval @code
else
instance_eval &@block
end
@tasks.each { |e| e.evaluate env }
self
end
private
def env
@env
end
def source(filepath)
instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb"
end
def target(hostname)
env.target = hostname
end
def task(name, &block)
@tasks << Task.new(name, &block)
end
end
end
end
end