Class: RubyLint::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-lint/command.rb

Overview

Class used for running the CLI.

Constant Summary

OPTION_MAPPING =

Returns:

  • (Hash)
{
  :levels    => :report_levels=,
  :presenter => :presenter=,
  :analysis  => :analysis_classes=
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command

Parameters:

  • options (Hash|Slop) (defaults to: {})


34
35
36
# File 'lib/ruby-lint/command.rb', line 34

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsHash|Slop (readonly)

Returns:

  • (Hash|Slop)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ruby-lint/command.rb', line 8

class Command
  attr_reader :options

  ##
  # @return [Hash]
  #
  OPTION_MAPPING = {
    :levels    => :report_levels=,
    :presenter => :presenter=,
    :analysis  => :analysis_classes=
  }

  ##
  # Formats the keys of a particular Hash stored on class level in
  # {RubyLint::Configuration}.
  #
  # @param [Symbol] name
  # @return [String]
  #
  def self.format_names(name)
    return "* #{Configuration.send(name).keys.sort.join("\n  * ")}"
  end

  ##
  # @param [Hash|Slop] options
  #
  def initialize(options = {})
    @options = options
  end

  ##
  # Runs the command with the supplied arguments.
  #
  # @param [Array] args
  #
  def run(args)
    start_time    = Time.now.to_f
    files         = extract_files(args)
    configuration = load_configuration

    configure(configuration, options)

    runner    = Runner.new(configuration)
    output    = runner.analyze(files)
    exec_time = Time.now.to_f - start_time
    status    = 0

    unless output.empty?
      status = 1
      puts output
    end

    show_benchmark_info(exec_time) if options[:benchmark]

    exit(status)
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def load_configuration
    if options[:config]
      unless File.file?(options[:config])
        raise Errno::ENOENT, options[:config]
      end

      return Configuration.load_from_file([options[:config]])
    else
      return Configuration.load_from_file
    end
  end

  ##
  # Returns an Array containing the file paths that exist.
  #
  # @param [Array] files
  # @return [Array]
  #
  def extract_files(files)
    return RubyLint::FileList.new.process(files)
  end

  ##
  # @param [RubyLint::Configuration] configuration
  # @param [Hash] options
  #
  def configure(configuration, options)
    OPTION_MAPPING.each do |key, setter|
      configuration.send(setter, options[key]) if options[key]
    end
  end

  ##
  # @param [Float] exec_time
  #
  def show_benchmark_info(exec_time)
    mem_kb = `ps -o rss= #{Process.pid}`.strip.to_f
    mem_mb = mem_kb / 1024

    stderr "Execution time: #{exec_time.round(4)} seconds"
    stderr "Memory usage: #{mem_mb.round(2)} MB (#{mem_kb.round(2)} KB)"
  end

  private

  ##
  # @param [String] text
  #
  def stderr(text)
    STDERR.puts(text)
  end
end

Class Method Details

.format_names(name) ⇒ String

Formats the keys of a particular Hash stored on class level in RubyLint::Configuration.

Parameters:

  • name (Symbol)

Returns:



27
28
29
# File 'lib/ruby-lint/command.rb', line 27

def self.format_names(name)
  return "* #{Configuration.send(name).keys.sort.join("\n  * ")}"
end

Instance Method Details

#configure(configuration, options) ⇒ Object

Parameters:



94
95
96
97
98
# File 'lib/ruby-lint/command.rb', line 94

def configure(configuration, options)
  OPTION_MAPPING.each do |key, setter|
    configuration.send(setter, options[key]) if options[key]
  end
end

#extract_files(files) ⇒ Array

Returns an Array containing the file paths that exist.

Parameters:

  • files (Array)

Returns:

  • (Array)


86
87
88
# File 'lib/ruby-lint/command.rb', line 86

def extract_files(files)
  return RubyLint::FileList.new.process(files)
end

#load_configurationRubyLint::Configuration



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby-lint/command.rb', line 68

def load_configuration
  if options[:config]
    unless File.file?(options[:config])
      raise Errno::ENOENT, options[:config]
    end

    return Configuration.load_from_file([options[:config]])
  else
    return Configuration.load_from_file
  end
end

#run(args) ⇒ Object

Runs the command with the supplied arguments.

Parameters:

  • args (Array)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby-lint/command.rb', line 43

def run(args)
  start_time    = Time.now.to_f
  files         = extract_files(args)
  configuration = load_configuration

  configure(configuration, options)

  runner    = Runner.new(configuration)
  output    = runner.analyze(files)
  exec_time = Time.now.to_f - start_time
  status    = 0

  unless output.empty?
    status = 1
    puts output
  end

  show_benchmark_info(exec_time) if options[:benchmark]

  exit(status)
end

#show_benchmark_info(exec_time) ⇒ Object

Parameters:

  • exec_time (Float)


103
104
105
106
107
108
109
# File 'lib/ruby-lint/command.rb', line 103

def show_benchmark_info(exec_time)
  mem_kb = `ps -o rss= #{Process.pid}`.strip.to_f
  mem_mb = mem_kb / 1024

  stderr "Execution time: #{exec_time.round(4)} seconds"
  stderr "Memory usage: #{mem_mb.round(2)} MB (#{mem_kb.round(2)} KB)"
end

#stderr(text) ⇒ Object (private)

Parameters:



116
117
118
# File 'lib/ruby-lint/command.rb', line 116

def stderr(text)
  STDERR.puts(text)
end