Class: RuboCop::ConfigurationCleaner::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/configuration_cleaner/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



4
5
6
7
# File 'lib/rubocop/configuration_cleaner/cli.rb', line 4

def initialize(argv)
  @argv = argv
  @params = {}
end

Instance Method Details

#runObject



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
# File 'lib/rubocop/configuration_cleaner/cli.rb', line 12

def run
  parse_option!

  target_configs.each do |path|
    config = YAML.load(path.read)
    default = default_configuration(requires(config))
    needless_cops = config.map do |cop_name, values|
      next unless values.is_a? Hash
      next unless cop_name.include?('/')

      cop_config = default[cop_name]
      next unless values.all? { |key, config_val| cop_config[key] == config_val }

      cop_name
    end.compact

    needless_re = Regexp.new("^(?:#{needless_cops.join('|')}):")
    removing = false
    content = path.read.lines.reject do |line|
      if removing
        if line.match?(/^\S/)
          removing = false
        else
          true
        end
      else
        if line.match?(needless_re)
          removing = true
        else
          false
        end
      end
    end.join

    case
    when params[:write]
      path.write content
    when params[:diff]
      Tempfile.create do |f|
        f.write content
        f.flush
        system("git", "diff", "--no-index", "--", path.to_s, f.path)
      end
    else
      puts content
    end
  end
end