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.



45
46
47
48
49
50
# File 'lib/itamae/recipe.rb', line 45

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.



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

def children
  @children
end

#delayed_notificationsObject (readonly)

Returns the value of attribute delayed_notifications.



10
11
12
# File 'lib/itamae/recipe.rb', line 10

def delayed_notifications
  @delayed_notifications
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#runnerObject (readonly)

Returns the value of attribute runner.



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

def runner
  @runner
end

Class Method Details

.find_recipe_in_gem(recipe) ⇒ Object



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
41
42
# File 'lib/itamae/recipe.rb', line 13

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



52
53
54
# File 'lib/itamae/recipe.rb', line 52

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

#load(vars = {}) ⇒ Object



56
57
58
59
# File 'lib/itamae/recipe.rb', line 56

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

#runObject



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

def run
  show_banner

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