Sha256: 1c67dc13ddfe5e66fadda4ac2e8297831311006eed28a80f91c124f7545dd7d1

Contents?: true

Size: 1.93 KB

Versions: 44

Compression:

Stored size: 1.93 KB

Contents

module Itamae
  class RecipeChildren < Array
    NotFoundError = Class.new(StandardError)

    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

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

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

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

    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

    def run
      self.each do |resource|
        resource.run
      end
    end

    # returns dependencies graph in DOT
    def dependency_in_dot
      result = ""
      result << "digraph recipes {\n"
      result << "  rankdir=LR;\n"
      result << _dependency_in_dot
      result << "}"

      result
    end

    def _dependency_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._dependency_in_dot
      end

      result
    end
  end
end

Version data entries

44 entries across 44 versions & 2 rubygems

Version Path
itamae-1.14.1 lib/itamae/recipe_children.rb
itamae-1.14.0 lib/itamae/recipe_children.rb
itamae-1.13.1 lib/itamae/recipe_children.rb
itamae-1.13.0 lib/itamae/recipe_children.rb
itamae-1.12.6 lib/itamae/recipe_children.rb
itamae-1.12.5 lib/itamae/recipe_children.rb
itamae-1.12.4 lib/itamae/recipe_children.rb
itamae-1.12.3 lib/itamae/recipe_children.rb
itamae-1.12.2 lib/itamae/recipe_children.rb
itamae-1.12.1 lib/itamae/recipe_children.rb
itamae-1.12.0 lib/itamae/recipe_children.rb
itamae-1.11.2 lib/itamae/recipe_children.rb
itamae-1.11.1 lib/itamae/recipe_children.rb
itamae-1.11.0 lib/itamae/recipe_children.rb
itamae-1.10.10 lib/itamae/recipe_children.rb
itamae-1.10.9 lib/itamae/recipe_children.rb
itamae-1.10.8 lib/itamae/recipe_children.rb
itamae-1.10.7 lib/itamae/recipe_children.rb
itamae-1.10.6 lib/itamae/recipe_children.rb
itamae-1.10.5 lib/itamae/recipe_children.rb