Class: RubyLint::Command
- Inherits:
-
Object
- Object
- RubyLint::Command
- Defined in:
- lib/ruby-lint/command.rb
Overview
Class used for running the CLI.
Constant Summary
- OPTION_MAPPING =
{ :levels => :report_levels=, :presenter => :presenter=, :analysis => :analysis_classes= }
Instance Attribute Summary collapse
-
#options ⇒ Hash|Slop
readonly
Class Method Summary collapse
-
.format_names(name) ⇒ String
Formats the keys of a particular Hash stored on class level in Configuration.
Instance Method Summary collapse
-
#configure(configuration, options) ⇒ Object
-
#extract_files(files) ⇒ Array
Returns an Array containing the file paths that exist.
-
#initialize(options = {}) ⇒ Command
constructor
A new instance of Command.
-
#load_configuration ⇒ RubyLint::Configuration
-
#run(args) ⇒ Object
Runs the command with the supplied arguments.
-
#show_benchmark_info(exec_time) ⇒ Object
-
#stderr(text) ⇒ Object
private
Constructor Details
#initialize(options = {}) ⇒ Command
Returns a new instance of Command
34 35 36 |
# File 'lib/ruby-lint/command.rb', line 34 def initialize( = {}) @options = end |
Instance Attribute Details
#options ⇒ Hash|Slop (readonly)
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 = 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, ) 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 [:benchmark] exit(status) end ## # @return [RubyLint::Configuration] # def load_configuration if [:config] unless File.file?([:config]) raise Errno::ENOENT, [:config] end return Configuration.load_from_file([[: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, ) OPTION_MAPPING.each do |key, setter| configuration.send(setter, [key]) if [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.
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
94 95 96 97 98 |
# File 'lib/ruby-lint/command.rb', line 94 def configure(configuration, ) OPTION_MAPPING.each do |key, setter| configuration.send(setter, [key]) if [key] end end |
#extract_files(files) ⇒ Array
Returns an Array containing the file paths that exist.
86 87 88 |
# File 'lib/ruby-lint/command.rb', line 86 def extract_files(files) return RubyLint::FileList.new.process(files) end |
#load_configuration ⇒ RubyLint::Configuration
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby-lint/command.rb', line 68 def load_configuration if [:config] unless File.file?([:config]) raise Errno::ENOENT, [:config] end return Configuration.load_from_file([[:config]]) else return Configuration.load_from_file end end |
#run(args) ⇒ Object
Runs the command with the supplied arguments.
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, ) 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 [:benchmark] exit(status) end |
#show_benchmark_info(exec_time) ⇒ Object
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)
116 117 118 |
# File 'lib/ruby-lint/command.rb', line 116 def stderr(text) STDERR.puts(text) end |