Sha256: c77caa358c25392b994a055101cf83f63ec21edd549a8ec691f463ebd3201e3e

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module CIHelper
  module Commands
    class Error < StandardError; end

    class BaseCommand
      class << self
        def call!(**options)
          new(**options).call
        end

        # :nocov:
        def process_stdout
          @process_stdout ||= STDOUT
        end
        # :nocov:
      end

      def initialize(**options)
        self.options = options
      end

      def execute_with_env(*commands)
        commands = ["export RAILS_ENV=#{env}", *commands] if env
        execute(*commands)
      end

      def execute(*commands)
        command = commands.join(" && ")

        process_stdout.puts(Tools::Colorize.command(command))

        Open3.popen2e(command) do |_stdin, stdout, thread|
          stdout.each_char { |char| process_stdout.print(char) }
          exit_code = thread.value.exitstatus

          fail!("Bad exit code #{exit_code} for command #{command.inspect}") unless exit_code.zero?
          0
        end
      end

      private

      attr_accessor :options

      def env; end

      def create_and_migrate_database!
        execute_with_env("bundle exec rake db:drop db:create db:migrate")
      end

      def fail!(message)
        raise Error, message
      end

      def path
        @path ||= Pathname.pwd
      end

      def process_stdout
        self.class.process_stdout
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ci-helper-0.1.1 lib/ci_helper/commands.rb