Class: MetricFu::GemRun
- Inherits:
-
Object
- Object
- MetricFu::GemRun
- Defined in:
- lib/metric_fu/gem_run.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#gem_name ⇒ Object
readonly
Returns the value of attribute gem_name.
-
#library_name ⇒ Object
readonly
Returns the value of attribute library_name.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #execute ⇒ Object
- #handle_run_error(run_error) ⇒ Object
- #handle_system_exit(system_exit) ⇒ Object
-
#initialize(arguments = {}) ⇒ GemRun
constructor
A new instance of GemRun.
- #print_errors ⇒ Object
- #run ⇒ Object
- #summary ⇒ Object
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
9 10 11 |
# File 'lib/metric_fu/gem_run.rb', line 9 def arguments @arguments end |
#gem_name ⇒ Object (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_name ⇒ Object (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 |
#output ⇒ Object (readonly)
Returns the value of attribute output.
9 10 11 |
# File 'lib/metric_fu/gem_run.rb', line 9 def output @output end |
#version ⇒ Object (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
#execute ⇒ Object
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" = "#{status} with code #{system_exit.status}: " << "#{system_exit.}: #{system_exit.backtrace.inspect}" if status == "SUCCESS" mf_debug else @errors << end end |
#print_errors ⇒ Object
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 |
#run ⇒ Object
24 25 26 |
# File 'lib/metric_fu/gem_run.rb', line 24 def run @output = execute end |
#summary ⇒ Object
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 |