Sha256: ca58f3941b1417daed35461e55a61e068f98ee2de76c5952dc904142b51975a7

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

module Buildkite
  module Builder
    class StepCollection
      STEP_TYPES = {
        block: Pipelines::Steps::Block,
        command: Pipelines::Steps::Command,
        group: Pipelines::Steps::Group,
        input: Pipelines::Steps::Input,
        trigger: Pipelines::Steps::Trigger,
        wait: Pipelines::Steps::Wait
      }.freeze

      attr_reader :steps

      def initialize
        @steps = []
      end

      def each(*types, &block)
        types = types.flatten
        types.map! { |type| STEP_TYPES.values.include?(type) ? type : STEP_TYPES.fetch(type) }
        types = STEP_TYPES.values if types.empty?

        matched_steps = steps.each_with_object([]) do |step, matches|
          if types.any? { |step_type| step.is_a?(step_type) }
            matches << step
          end
          if step.is_a?(Pipelines::Steps::Group)
            step.steps.each(types) { |step| matches << step }
          end
        end
        matched_steps.each(&block)
      end

      def find(key)
        steps.find { |step| step.has?(:key) && step.key.to_s == key.to_s }
      end

      def remove(step)
        steps.delete(step)
      end

      def replace(old_step, new_step)
        steps[steps.index(old_step)] = new_step
      end

      def find!(key)
        find(key) || raise(ArgumentError, "Can't find step with key: #{key}")
      end

      def push(step)
        @steps.push(step)
      end

      def to_definition
        @steps.map(&:to_h)
      end

      def empty?
        @steps.empty? || (@steps.all? { |step| step.is_a?(Pipelines::Steps::Group) && step.steps.empty? })
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
buildkite-builder-4.10.0 lib/buildkite/builder/step_collection.rb
buildkite-builder-4.8.0 lib/buildkite/builder/step_collection.rb
buildkite-builder-4.7.0 lib/buildkite/builder/step_collection.rb
buildkite-builder-4.6.0 lib/buildkite/builder/step_collection.rb
buildkite-builder-4.5.0 lib/buildkite/builder/step_collection.rb