Sha256: 4ff00fbe36f8511f13186b5ef09dfa0940ae4acda87d54094254b0b716789d0e

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Buildkite
  module Builder
    module Processors
      class Abstract
        include LoggingUtils
        using Rainbow

        def self.process(context)
          new(context).run
        end

        def initialize(context)
          @context = context
        end

        def run
          _log_run { process }
        end

        private

        attr_reader :context

        def process
          raise NotImplementedError
        end

        def log
          context.logger
        end

        def pipeline
          context.pipeline
        end

        def buildkite
          @buildkite ||= begin
            unless Buildkite.env
              raise 'Must be in Buildkite environment to access the Buildkite API'
            end

            Buildkite::Pipelines::Api.new(Buildkite.env.api_token)
          end
        end

        def pipeline_steps(*types)
          steps = pipeline.steps
          types = types.flatten
          steps = steps.select { |step| types.include?(step.class.to_sym) } if types.any?
          steps
        end

        def _log_run
          log.info "\nProcessing ".color(:dimgray) + self.class.name.color(:springgreen)

          results = benchmark('└──'.color(:springgreen) + ' Finished in %s'.color(:dimgray)) do
            formatter = log.formatter
            log.formatter = proc do |_severity, _datetime, _progname, msg|
              '│'.color(:springgreen) + " #{msg}\n"
            end

            begin
              yield
            ensure
              log.formatter = formatter
            end
          end

          log.info results
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
buildkite-builder-1.4.1 lib/buildkite/builder/processors/abstract.rb
buildkite-builder-1.4.0 lib/buildkite/builder/processors/abstract.rb
buildkite-builder-1.3.1 lib/buildkite/builder/processors/abstract.rb
buildkite-builder-1.3.0 lib/buildkite/builder/processors/abstract.rb
buildkite-builder-1.2.0 lib/buildkite/builder/processors/abstract.rb
buildkite-builder-1.1.0 lib/buildkite/builder/processors/abstract.rb