class MetricFu::GemRun

Attributes

arguments[R]
gem_name[R]
library_name[R]
output[R]
version[R]

Public Class Methods

new(arguments = {}) click to toggle source
# File lib/metric_fu/gem_run.rb, line 10
def initialize(arguments = {})
  @gem_name    = arguments.fetch(:gem_name)
  @library_name = arguments.fetch(:metric_name)
  @version = arguments.fetch(:version) { MetricFu::GemVersion.for(library_name) }
  args = arguments.fetch(:args)
  @arguments = args.respond_to?(:scan) ? Shellwords.shellwords(args) : args
  @output = ""
  @errors = []
end

Public Instance Methods

execute() click to toggle source
# File lib/metric_fu/gem_run.rb, line 28
def execute
  mf_debug "Running #{summary}"
  captured_output = ""
  captured_errors = ""
  thread = ""
  Open3.popen3("#{library_name}", *arguments) do |_stdin, stdout, stderr, wait_thr|
    captured_output << stdout.read.chomp
    captured_errors << stderr.read.chomp
    thread = wait_thr
  end
rescue StandardError => run_error
  handle_run_error(run_error)
rescue SystemExit => system_exit
  handle_system_exit(system_exit)
ensure
  print_errors
  return captured_output, captured_errors, thread.value
end
handle_run_error(run_error) click to toggle source
# File lib/metric_fu/gem_run.rb, line 47
def handle_run_error(run_error)
  @errors << "ERROR: #{run_error.inspect}"
end
handle_system_exit(system_exit) click to toggle source
# File lib/metric_fu/gem_run.rb, line 51
def handle_system_exit(system_exit)
  status =  system_exit.success? ? "SUCCESS" : "FAILURE"
  message = "#{status} with code #{system_exit.status}: " <<
            "#{system_exit.message}: #{system_exit.backtrace.inspect}"
  if status == "SUCCESS"
    mf_debug message
  else
    @errors << message
  end
end
print_errors() click to toggle source
run() click to toggle source
# File lib/metric_fu/gem_run.rb, line 24
def run
  @output = execute
end
summary() click to toggle source
# File lib/metric_fu/gem_run.rb, line 20
def summary
  "RubyGem #{gem_name}, library #{library_name}, version #{version}, arguments #{arguments}"
end