class Chronometer::Command::Chronometer

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# File lib/chronometer/command.rb, line 25
def initialize(argv)
  @chronofile = argv.shift_argument
  @output = argv.option('output', "#{@chronofile}.trace")
  @file_to_load = argv.shift_argument
  @arguments = argv.remainder! if @chronofile && @file_to_load
  super
end
options() click to toggle source
Calls superclass method
# File lib/chronometer/command.rb, line 19
def self.options
  [
    ['--output=TRACE', 'The path to the tracefile chronometer will write']
  ].concat(super)
end

Public Instance Methods

run() click to toggle source
# File lib/chronometer/command.rb, line 51
def run
  argv = ::ARGV.dup
  ::ARGV.replace(@arguments)
  time { load(@file_to_load) }
ensure
  ::ARGV.replace(argv)
end
validate!() click to toggle source
Calls superclass method
# File lib/chronometer/command.rb, line 33
def validate!
  super
  help! 'Must supply a chronofile' unless @chronofile
  @chronofile_contents = begin
                           File.read(@chronofile)
                         rescue StandardError
                           help!("No such chronofile `#{@chronofile}`")
                         end
  help! 'Must supply a ruby file to load' unless @file_to_load
  @file_to_load = ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).push('.').reduce(nil) do |a, e|
    next a if a
    a = File.join(e, @file_to_load)
    next nil unless File.file?(a)
    a
  end
  help! "Could not find `#{@file_to_load}`" unless @file_to_load
end

Private Instance Methods

time() { || ... } click to toggle source
# File lib/chronometer/command.rb, line 61
def time
  timer = ::Chronometer.from_file(@chronofile, contents: @chronofile_contents)
  timer.install!

  begin
    yield
  ensure
    timer.drain!
    timer.print_trace_event_report(@output, metadata: { meta_success: $ERROR_INFO.nil? })
  end
end