Class: Itamae::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/itamae/recipe.rb

Direct Known Subclasses

RecipeFromDefinition

Defined Under Namespace

Classes: EvalContext, RecipeFromDefinition

Constant Summary collapse

NotFoundError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, path) ⇒ Recipe

Returns a new instance of Recipe.



43
44
45
46
47
48
# File 'lib/itamae/recipe.rb', line 43

def initialize(runner, path)
  @runner = runner
  @path = path
  @delayed_notifications = []
  @children = RecipeChildren.new
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/itamae/recipe.rb', line 7

def children
  @children
end

#delayed_notificationsObject (readonly)

Returns the value of attribute delayed_notifications.



8
9
10
# File 'lib/itamae/recipe.rb', line 8

def delayed_notifications
  @delayed_notifications
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/itamae/recipe.rb', line 5

def path
  @path
end

#runnerObject (readonly)

Returns the value of attribute runner.



6
7
8
# File 'lib/itamae/recipe.rb', line 6

def runner
  @runner
end

Class Method Details

.find_recipe_in_gem(recipe) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/itamae/recipe.rb', line 11

def find_recipe_in_gem(recipe)
  plugin_name, recipe_file = recipe.split('::', 2)
  recipe_file = recipe_file.gsub("::", "/") if recipe_file

  gem_name = "itamae-plugin-recipe-#{plugin_name}"
  begin
    gem gem_name
  rescue LoadError
  end
  spec = Gem.loaded_specs.values.find do |spec|
    spec.name == gem_name
  end

  return nil unless spec

  candidate_files = []
  if recipe_file
    recipe_file += '.rb' unless recipe_file.end_with?('.rb')
    candidate_files << "#{plugin_name}/#{recipe_file}"
  else
    candidate_files << "#{plugin_name}/default.rb"
    candidate_files << "#{plugin_name}.rb"
  end

  candidate_files.map do |file|
    File.join(spec.lib_dirs_glob, 'itamae', 'plugin', 'recipe', file)
  end.find do |path|
    File.exist?(path)
  end
end

Instance Method Details

#dirObject



50
51
52
# File 'lib/itamae/recipe.rb', line 50

def dir
  ::File.dirname(@path)
end

#load(vars = {}) ⇒ Object



54
55
56
57
# File 'lib/itamae/recipe.rb', line 54

def load(vars = {})
  context = EvalContext.new(self, vars)
  InstanceEval.new(File.read(path), path, 1, context: context).call
end

#runObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/itamae/recipe.rb', line 59

def run
  show_banner

  @runner.handler.event(:recipe, path: @path) do
    Itamae.logger.with_indent do
      @children.run
      run_delayed_notifications
    end
  end
end