Sha256: b72bc46b3696e74e3558d987dcd44133c5ad609768441be2f5c15ce2386951cc

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module CobraCommander
  # Calculates directly & transitively affected components
  class Affected
    attr_reader :directly, :transitively

    def initialize(tree, changes, path)
      @tree = tree
      @changes = changes
      @path = path
      run!
    end

    def needs_testing
      @needs_testing ||= all_affected.map! do |component|
        File.join(component[:path], "test.sh")
      end
    end

  private

    def run!
      @transitively = Set.new
      @directly = Set.new
      find_dependencies(@tree)
      @transitively.delete(name: UMBRELLA_APP_NAME, path: @path, type: @tree[:type])
      @transitively = @transitively.to_a.sort_by { |h| h[:name] }
      @directly = @directly.to_a.sort_by { |h| h[:name] }
    end

    def find_dependencies(parent_component)
      parent_component[:dependencies].each do |component|
        add_if_changed(component)
        find_dependencies(component)
      end
    end

    def add_if_changed(component)
      @changes.each do |change|
        if change.start_with?(component[:path])
          @directly << component.reject { |k| k == :dependencies || k == :ancestry }
          @transitively.merge component[:ancestry]
        end
      end
    end

    def all_affected
      (@directly + @transitively).uniq.sort_by { |h| h[:path] }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cobra_commander-0.2.0 lib/cobra_commander/affected.rb