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.
This commit is contained in:
Thibault Jouan
2013-08-10 15:36:27 +00:00
parent 7c5d5b0417
commit f0e144cebd
6 changed files with 77 additions and 85 deletions

View File

@@ -12,7 +12,6 @@ module Producer
def run!
check_arguments!
recipe.evaluate(env)
worker.process recipe.tasks
end
@@ -25,7 +24,7 @@ module Producer
end
def recipe
@recipe ||= Recipe.from_file(@arguments.first)
@recipe ||= Recipe.evaluate_from_file(@arguments.first, env)
end
def worker

View File

@@ -1,22 +1,14 @@
module Producer
module Core
class Recipe
attr_reader :code, :filepath, :tasks
attr_accessor :tasks
def self.from_file(filepath)
new(File.read(filepath), filepath)
def self.evaluate_from_file(filepath, env)
DSL.evaluate(File.read(filepath), env)
end
def initialize(code, filepath = nil)
@code = code
@filepath = filepath
@tasks = []
end
def evaluate(env)
dsl = DSL.new(@code).evaluate(env)
dsl.tasks.map { |e| e.evaluate env }
@tasks = dsl.tasks
def initialize(tasks = [])
@tasks = tasks
end
end
end

View File

@@ -4,6 +4,11 @@ module Producer
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
@@ -17,6 +22,7 @@ module Producer
else
instance_eval &@block
end
@tasks.each { |e| e.evaluate env }
self
end