Class: Wool::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/wool/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fixObject

Returns the value of attribute fix.



3
4
5
# File 'lib/wool/runner.rb', line 3

def fix
  @fix
end

#usingObject

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_argumentsObject



20
21
22
23
24
25
26
27
28
# File 'lib/wool/runner.rb', line 20

def collect_options_and_arguments
  swizzling_argv do
    settings = get_settings
    handle_global_options(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.

Parameters:

  • files (Array<String>)

    the files to scan. If (stdin) is in the array, then data will be read from STDIN until EOF is reached.

  • scanner (Scanner)

    the scanner that will look for warnings in the source text.

Returns:

  • (Array<Warning>)

    a set of warnings, ordered by file.



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.

Parameters:

  • warnings (Array<Warning>)

    the warnings generated by the input files, ordered by file

  • settings (Hash{Symbol => Object})

    the user-set display 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_settingsHash{Symbol => Object}

Parses the command-line options using Trollop

Returns:

  • (Hash{Symbol => Object})

    the settings entered by the user



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 = get_warning_options
  Trollop::options do
    banner '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_optionsObject

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 get_warning_options
  all_options = Warning.all_warnings.inject({}) do |result, warning|
    options = warning.options
    options = [options] if options.any? && !options[0].is_a?(Array)
    options.each do |option|
      result[option.first] = option
    end
    result
  end
  all_options.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.

Parameters:

  • settings (Hash)

    the settings from the command-line to process.

Options Hash (settings):

  • :only (Object) — default: String

    a list of warning names or short names that will be the only warnings run. The names should be whitespace-delimited.

  • :(Integer)amaximumlinelengthto ("line-length")

    generate a warning for. A common choice is 80/83.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wool/runner.rb', line 39

def handle_global_options(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

#runObject



11
12
13
14
15
16
17
18
# File 'lib/wool/runner.rb', line 11

def run
  settings, files = collect_options_and_arguments
  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_argvObject

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_considerObject

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_fixObject

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