Class: Wool::Runner
- Inherits:
-
Object
- Object
- Wool::Runner
- Defined in:
- lib/wool/runner.rb
Instance Attribute Summary collapse
-
#fix ⇒ Object
Returns the value of attribute fix.
-
#using ⇒ Object
Returns the value of attribute using.
Instance Method Summary collapse
- #collect_options_and_arguments ⇒ Object
-
#collect_warnings(files, scanner) ⇒ Array<Warning>
Collects warnings from all the provided files by running them through the scanner.
-
#convert_warning_list(list) ⇒ Object
Converts a list of warnings and symbol shortcuts for warnings to just a list of warnings.
-
#display_warnings(warnings, settings) ⇒ Object
Displays warnings using user-provided settings.
-
#get_settings ⇒ Hash{Symbol => Object}
Parses the command-line options using Trollop.
-
#get_warning_options ⇒ Object
Gets all the options from the warning plugins and collects them with overriding rules.
-
#handle_global_options(settings) ⇒ Object
Processes the global options, which includes picking which warnings to run against the source code.
-
#initialize(argv) ⇒ Runner
constructor
A new instance of Runner.
- #run ⇒ Object
-
#swizzling_argv ⇒ Object
Sets the ARGV variable to the runner’s arguments during the execution of the block.
-
#warnings_to_consider ⇒ Object
Returns the list of warnings the user has activated for use.
-
#warnings_to_fix ⇒ Object
Returns the list of warnings the user has selected for fixing.
Constructor Details
#initialize(argv) ⇒ Runner
Returns a new instance of Runner.
5 6 7 8 9 |
# File 'lib/wool/runner.rb', line 5 def initialize(argv) @argv = argv @using = [:all] @fix = [:all] end |
Instance Attribute Details
#fix ⇒ Object
Returns the value of attribute fix.
3 4 5 |
# File 'lib/wool/runner.rb', line 3 def fix @fix end |
#using ⇒ Object
Returns the value of attribute using.
3 4 5 |
# File 'lib/wool/runner.rb', line 3 def using @using end |
Instance Method Details
#collect_options_and_arguments ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/wool/runner.rb', line 20 def swizzling_argv do settings = get_settings (settings) p settings if settings[:debug] files = ARGV.dup [settings, files] end end |
#collect_warnings(files, scanner) ⇒ Array<Warning>
Collects warnings from all the provided files by running them through the scanner.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/wool/runner.rb', line 125 def collect_warnings(files, scanner) full_list = files.map do |file| data = file == '(stdin)' ? STDIN.read : File.read(file) if scanner.settings[:fix] scanner.settings[:output_file] = scanner.settings[:stdin] ? STDOUT : File.open(file, 'w') end results = scanner.scan(data, file) scanner.settings[:output_file].close if scanner.settings[:fix] && !scanner.settings[:stdin] results end full_list.flatten end |
#convert_warning_list(list) ⇒ Object
Converts a list of warnings and symbol shortcuts for warnings to just a list of warnings.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/wool/runner.rb', line 85 def convert_warning_list(list) list.map do |list| case list when :all then Warning.all_warnings when :whitespace [ExtraBlankLinesWarning, ExtraWhitespaceWarning, OperatorSpacing, MisalignedUnindentationWarning] else list end end.flatten end |
#display_warnings(warnings, settings) ⇒ Object
Displays warnings using user-provided settings.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/wool/runner.rb', line 143 def display_warnings(warnings, settings) num_fixable = warnings.select { |warn| warn.fixable? }.size num_total = warnings.size results = "#{num_total} warnings found. #{num_fixable} are fixable." puts results puts '=' * results.size warnings.each do |warning| puts "#{warning.file}:#{warning.line_number} #{warning.name} (#{warning.severity}) - #{warning.desc}" end end |
#get_settings ⇒ Hash{Symbol => Object}
Parses the command-line options using Trollop
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wool/runner.rb', line 54 def get_settings warning_opts = Trollop:: do 'Ask Peeves - the Ruby Linter' opt :fix, 'Should errors be fixed in-line?', :short => '-f' opt :display, 'Should errors be displayed?', :short => '-b', :default => true opt :"report-fixed", 'Should fixed errors be reported anyway?', :short => '-r' opt :"line-length", 'Warn at the given line length', :short => '-l', :type => :int opt :only, 'Only consider the given warning (by short or full name)', :short => '-O', :type => :string opt :stdin, 'Read Ruby code from standard input', :short => '-s' warning_opts.each { |warning| opt(*warning) } end end |
#get_warning_options ⇒ Object
Gets all the options from the warning plugins and collects them with overriding rules. The later the declaration is run, the higher the priority the option has.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wool/runner.rb', line 71 def = Warning.all_warnings.inject({}) do |result, warning| = warning. = [] if .any? && ![0].is_a?(Array) .each do |option| result[option.first] = option end result end .values end |
#handle_global_options(settings) ⇒ Object
Processes the global options, which includes picking which warnings to run against the source code. The settings provided determine what modifies the runner’s settings.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wool/runner.rb', line 39 def (settings) if settings[:"line-length"] @using << Wool.LineLengthWarning(settings[:"line-length"]) end if (only_name = settings[:only]) @fix = @using = Warning.concrete_warnings.select do |w| (w.name && w.name.index(only_name)) || (w.short_name && only_name.index(w.short_name)) end end ARGV.replace(['(stdin)']) if settings[:stdin] end |
#run ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/wool/runner.rb', line 11 def run settings, files = settings[:__using__] = warnings_to_consider settings[:__fix__] = warnings_to_fix scanner = Scanner.new(settings) warnings = collect_warnings(files, scanner) display_warnings(warnings, settings) if settings[:display] end |
#swizzling_argv ⇒ Object
Sets the ARGV variable to the runner’s arguments during the execution of the block.
109 110 111 112 113 114 115 |
# File 'lib/wool/runner.rb', line 109 def swizzling_argv old_argv = ARGV.dup ARGV.replace @argv yield ensure ARGV.replace old_argv end |
#warnings_to_consider ⇒ Object
Returns the list of warnings the user has activated for use.
98 99 100 |
# File 'lib/wool/runner.rb', line 98 def warnings_to_consider convert_warning_list(@using) end |
#warnings_to_fix ⇒ Object
Returns the list of warnings the user has selected for fixing
103 104 105 |
# File 'lib/wool/runner.rb', line 103 def warnings_to_fix convert_warning_list(@fix) end |