Class: CIHelper::Commands::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ci_helper/commands.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ BaseCommand

Returns a new instance of BaseCommand.



27
28
29
# File 'lib/ci_helper/commands.rb', line 27

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

Class Method Details

.call!Object



16
17
18
# File 'lib/ci_helper/commands.rb', line 16

def call!(**)
  new(**).call
end

.process_stdoutObject

:nocov:



21
22
23
# File 'lib/ci_helper/commands.rb', line 21

def process_stdout
  @process_stdout ||= $stdout
end

Instance Method Details

#execute(*commands, capture: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ci_helper/commands.rb', line 36

def execute(*commands, capture: false)
  command = commands.join(" && ")

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

  captured = +"" if capture
  Open3.popen2e(command) do |_stdin, stdout, thread|
    stdout.each_char { |char| capture ? captured << char : process_stdout.print(char) }
    exit_code = thread.value.exitstatus

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

#execute_with_env(*commands) ⇒ Object



31
32
33
34
# File 'lib/ci_helper/commands.rb', line 31

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