Class: Itamae::RecipeChildren

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

Constant Summary collapse

NotFoundError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#_deps_in_dotObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/itamae/recipe_children.rb', line 78

def _deps_in_dot
  result = ""

  recipes(recursive: false).each do |recipe|
    recipe.children.recipes(recursive: false).each do |child_recipe|
      result << %{  "#{recipe.path}" -> "#{child_recipe.path}";\n}
    end
    result << recipe.children._deps_in_dot
  end

  result
end

#deps_in_dotObject

returns dependencies graph in DOT



68
69
70
71
72
73
74
75
76
# File 'lib/itamae/recipe_children.rb', line 68

def deps_in_dot
  result = ""
  result << "digraph recipes {\n"
  result << "  rankdir=LR;\n"
  result << _deps_in_dot
  result << "}"

  result
end

#find_recipe_by_path(path) ⇒ Object



25
26
27
28
29
# File 'lib/itamae/recipe_children.rb', line 25

def find_recipe_by_path(path)
  recipes.find do |recipe|
    recipe.path == path
  end
end

#find_resource_by_description(desc) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/itamae/recipe_children.rb', line 5

def find_resource_by_description(desc)
  # desc is like 'resource_type[name]'
  resources.find do |resource|
    type, name = Itamae::Resource.parse_description(desc)
    resource.resource_type == type && resource.resource_name == name
  end.tap do |resource|
    unless resource
      raise NotFoundError, "'#{desc}' resource is not found."
    end
  end
end

#recipes(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/itamae/recipe_children.rb', line 42

def recipes(options = {})
  options = {recursive: true}.merge(options)

  self.select do |item|
    item.is_a?(Recipe)
  end.map do |recipe|
    if options[:recursive]
      [recipe] + recipe.children.recipes
    else
      recipe
    end
  end.flatten
end

#resourcesObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/itamae/recipe_children.rb', line 31

def resources
  self.map do |item|
    case item
    when Resource::Base
      item
    when Recipe
      item.children.resources
    end
  end.flatten
end

#run(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/itamae/recipe_children.rb', line 56

def run(options)
  self.each do |resource|
    case resource
    when Resource::Base
      resource.run(nil, dry_run: options[:dry_run])
    when Recipe
      resource.run(options)
    end
  end
end

#subscribing(target) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/itamae/recipe_children.rb', line 17

def subscribing(target)
  resources.map do |resource|
    resource.subscriptions.select do |subscription|
      subscription.resource == target
    end
  end.flatten
end