Class: Rubomop::Runner

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

Constant Summary collapse

NUM_STRING =
"Number of cleanups to perform (default: 10)"
AUTOCORRECT_STRING =
"Only clean autocorrectable cops (default)"
NO_AUTOCORRECT_STRING =
"Clean all cops (not default)"
RUBOCOP_STRING =
"Run rubocop -aD after (default)"
NO_RUBOCOP_STRING =
"Don't run rubocop -aD after (not default)"
FILENAME_STRING =
"Name of todo file (default: ./.rubocop_todo.yml)"
CONFIG_STRING =
"Name of optional config file (default: .rubomop.yml)"
ONLY_STRING =
"String or regex of cops to limit removal do, can have multiple"
EXCEPT_STRING =
"String or regex of cops to not remove from, can have multiple"
BLOCK_STRING =
"String or regex of files to not touch, can have multiple"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubomop/runner.rb', line 18

def initialize
  @number = 10
  @autocorrect_only = true
  @run_rubocop = true
  @filename = ".rubocop_todo.yml"
  @config = ".rubomop.yml"
  @todo = nil
  @verbose = true
  @options_from_command_line = []
  @only = []
  @except = []
  @block = []
end

Instance Attribute Details

#autocorrect_onlyObject

Returns the value of attribute autocorrect_only.



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

def autocorrect_only
  @autocorrect_only
end

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/rubomop/runner.rb', line 5

def block
  @block
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/rubomop/runner.rb', line 4

def config
  @config
end

#exceptObject

Returns the value of attribute except.



5
6
7
# File 'lib/rubomop/runner.rb', line 5

def except
  @except
end

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/rubomop/runner.rb', line 4

def filename
  @filename
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#onlyObject

Returns the value of attribute only.



5
6
7
# File 'lib/rubomop/runner.rb', line 5

def only
  @only
end

#options_from_command_lineObject

Returns the value of attribute options_from_command_line.



4
5
6
# File 'lib/rubomop/runner.rb', line 4

def options_from_command_line
  @options_from_command_line
end

#run_rubocopObject

Returns the value of attribute run_rubocop.



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

def run_rubocop
  @run_rubocop
end

#todoObject

Returns the value of attribute todo.



4
5
6
# File 'lib/rubomop/runner.rb', line 4

def todo
  @todo
end

#verboseObject

Returns the value of attribute verbose.



4
5
6
# File 'lib/rubomop/runner.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#backup_existing_fileObject



116
117
118
119
# File 'lib/rubomop/runner.rb', line 116

def backup_existing_file
  FileUtils.rm("#{filename}.bak") if File.exist?("#{filename}.bak")
  FileUtils.mv(filename, "#{filename}.bak")
end

#execute(args) ⇒ Object



32
33
34
35
# File 'lib/rubomop/runner.rb', line 32

def execute(args)
  load_options(args)
  run
end

#load_from_fileObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rubomop/runner.rb', line 42

def load_from_file
  return unless File.exist?(config)
  file_options = YAML.safe_load(File.read(config))
  file_options.each do |key, value|
    next if options_from_command_line.include?(key)
    send("#{key.underscore}=", value) if respond_to?("#{key.underscore}=")
  end
rescue Psych::Exception
  nil
end

#load_options(args) ⇒ Object



37
38
39
40
# File 'lib/rubomop/runner.rb', line 37

def load_options(args)
  parse(args)
  load_from_file
end

#mopObject



104
105
106
# File 'lib/rubomop/runner.rb', line 104

def mop
  Mop.new(todo, number, autocorrect_only, verbose, run_rubocop, only, except, block)
end

#parse(args) ⇒ Object



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
# File 'lib/rubomop/runner.rb', line 53

def parse(args)
  option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: rubomop [options]"
    opts.on("-nNUMBER", "--number NUMBER", Integer, NUM_STRING) do |value|
      self.number = value
      @options_from_command_line << "number"
    end
    opts.on("-a", "--autocorrect-only", AUTOCORRECT_STRING) do
      self.autocorrect_only = true
      @options_from_command_line << "autocorrect-only"
    end
    opts.on("--no_autocorrect-only", NO_AUTOCORRECT_STRING) do
      self.autocorrect_only = false
      @options_from_command_line << "autocorrect-only"
    end
    opts.on("-r", "--run-rubocop", RUBOCOP_STRING) do
      self.run_rubocop = true
      @options_from_command_line << "run-rubocop"
    end
    opts.on("-fFILENAME", "--filename=FILENAME", FILENAME_STRING) do |value|
      self.filename = value
      @options_from_command_line << "filename"
    end
    opts.on("--no-run-rubocop", NO_RUBOCOP_STRING) do
      self.run_rubocop = false
      @options_from_command_line << "run-rubocop"
    end
    opts.on("-cCONFIG_FILE", "--config=CONFIG_FILE", CONFIG_STRING) do |value|
      self.config = value
      @options_from_command_line << "config"
    end
    opts.on("--only=ONLY", ONLY_STRING) do |value|
      only << value
      @options_from_command_line << "only"
    end
    opts.on("--except=EXCEPT", ONLY_STRING) do |value|
      except << value
      @options_from_command_line << "except"
    end
    opts.on("--block=BLOCK", BLOCK_STRING) do |value|
      block << value
      @options_from_command_line << "block"
    end
    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end
  option_parser.parse(args)
end

#runObject



108
109
110
111
112
113
114
# File 'lib/rubomop/runner.rb', line 108

def run
  self.todo = TodoFile.new(filename: filename)&.parse
  return if todo.nil?
  backup_existing_file
  mop.mop!
  todo&.save!
end