module Rake::Funnel::Extensions::Shell

Public Instance Methods

shell(*cmd, log_file: nil, error_lines: nil) { |success, readable_cmd, result, log| ... } click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 12
def shell(*cmd, log_file: nil, error_lines: nil, &block)
  mkdir_p(File.dirname(log_file)) if log_file

  run(cmd, log_file, error_lines) do |success, readable_cmd, result, log|
    if block
      yield(success, readable_cmd, result, log)
      return
    end
  end
end

Private Instance Methods

handle_line(line, error_lines) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 70
def handle_line(line, error_lines)
  to_stderr(line, error_lines) || to_stdout(line)
end
log_lines(stdout_and_stderr, log_file, error_lines, log_string) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 61
def log_lines(stdout_and_stderr, log_file, error_lines, log_string)
  stdout_and_stderr.map do |line|
    log_string.write(line)
    File.open(log_file, 'a') { |f| f.write(line) } if log_file

    handle_line(line, error_lines)
  end
end
log_output(stdout_and_stderr, log_file, error_lines) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 49
def log_output(stdout_and_stderr, log_file, error_lines)
  log_string = StringIO.new

  begin
    statuses = log_lines(stdout_and_stderr, log_file, error_lines, log_string)

    [log_string.string, statuses.any? { |s| s == :error }]
  ensure
    log_string.close
  end
end
normalize(cmd) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 42
def normalize(cmd)
  cmd = cmd.flatten.reject(&:nil?)
  readable_cmd = cmd.join(' ')

  [cmd, readable_cmd]
end
run(cmd, log_file, error_lines) { |success, *result| ... } click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 25
def run(cmd, log_file, error_lines) # rubocop:disable Metrics/AbcSize
  cmd, readable_cmd = normalize(cmd)

  $stderr.print(readable_cmd + "\n")

  Open3.popen2e(*cmd) do |_, stdout_and_stderr, wait_thread|
    log, error_logged = log_output(stdout_and_stderr, log_file, error_lines)
    success = wait_thread.value.success? && error_logged == false

    result = [readable_cmd, wait_thread.value.exitstatus, log]

    yield(success, *result) if block_given?

    raise Rake::Funnel::ExecutionError.new(*result) unless success
  end
end
to_stderr(line, error_lines) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 79
def to_stderr(line, error_lines)
  return unless error_lines && line =~ error_lines

  $stderr.print(line.rstrip.bold.red + "\n")
  :error
end
to_stdout(line) click to toggle source
# File lib/rake/funnel/extensions/shell.rb, line 74
def to_stdout(line)
  $stdout.print(line.rstrip.green + "\n")
  :success
end