Class: MetricFu::GemRun

Inherits:
Object
  • Object
show all
Defined in:
lib/metric_fu/gem_run.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ GemRun

Returns a new instance of GemRun.



10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



9
10
11
# File 'lib/metric_fu/gem_run.rb', line 9

def arguments
  @arguments
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



9
10
11
# File 'lib/metric_fu/gem_run.rb', line 9

def gem_name
  @gem_name
end

#library_nameObject (readonly)

Returns the value of attribute library_name.



9
10
11
# File 'lib/metric_fu/gem_run.rb', line 9

def library_name
  @library_name
end

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/metric_fu/gem_run.rb', line 9

def output
  @output
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/metric_fu/gem_run.rb', line 9

def version
  @version
end

Instance Method Details

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# 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


62
63
64
65
66
67
68
# File 'lib/metric_fu/gem_run.rb', line 62

def print_errors
  return if @errors.empty?
  STDERR.puts "ERRORS running #{summary}"
  @errors.each do |error|
    STDERR.puts "\t" << error
  end
end

#runObject



24
25
26
# File 'lib/metric_fu/gem_run.rb', line 24

def run
  @output = execute
end

#summaryObject



20
21
22
# File 'lib/metric_fu/gem_run.rb', line 20

def summary
  "RubyGem #{gem_name}, library #{library_name}, version #{version}, arguments #{arguments}"
end