class AppUp::Shell::Runner

Constants

CommandFailureError
MAX_PROCESS_COUNT

Attributes

log_path[RW]
queue[RW]
working_directory[RW]

Public Class Methods

new(log_path:, working_directory:, verbose: false) click to toggle source
# File lib/app_up/shell/runner.rb, line 15
def initialize(log_path:, working_directory:, verbose: false)
  @working_directory = working_directory
  @log_path = log_path
  @verbose = verbose

  @queue = WorkQueue.new(MAX_PROCESS_COUNT, nil)

  %x{echo "" > #{log_path}}
  Dir.chdir(%x[ git rev-parse --show-toplevel ].chomp)
end

Public Instance Methods

enqueue(method, *args, &block) click to toggle source
# File lib/app_up/shell/runner.rb, line 49
def enqueue(method, *args, &block)
  queue.enqueue_b do
    send(method, *args, &block)
  end
end
flush() click to toggle source
# File lib/app_up/shell/runner.rb, line 55
def flush
  queue.join
end
notify(msg) click to toggle source
# File lib/app_up/shell/runner.rb, line 42
def notify(msg)
  return if @verbose

  log msg.to_s
  print "#{yellow(msg.to_s)}\n"
end
run(cmd, dir: working_directory, &block) click to toggle source

The block passed to run is a callback. It is used to add a dependent command to the queue.

# File lib/app_up/shell/runner.rb, line 28
def run(cmd, dir: working_directory, &block)
  command = "cd #{Utils::Path.relative_join(dir)} && #{cmd}"
  handle_output_for(command)

  shell_out(command).split("\n").tap do
    block.call if block_given?
  end
end
warn(msg) click to toggle source
# File lib/app_up/shell/runner.rb, line 37
def warn(msg)
  log msg.to_s
  print "#{red(msg.to_s)}\n"
end

Private Instance Methods

handle_output_for(cmd) click to toggle source
# File lib/app_up/shell/runner.rb, line 65
def handle_output_for(cmd)
  puts cmd if @verbose
  log(cmd)
end
log(msg) click to toggle source
# File lib/app_up/shell/runner.rb, line 61
def log(msg)
  %x{echo "#{msg.to_s}" >> #{log_path}}
end
shell_out(command) click to toggle source
# File lib/app_up/shell/runner.rb, line 70
def shell_out(command)
  %x{ set -o pipefail && #{command} 2>> #{log_path} | tee -a #{log_path} }.chomp.tap do
    warn "The following command has failed: #{command}.  See #{log_path} for a full log." if ($?.exitstatus != 0)
  end
end