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)"
NAME_STRING =
"Name of cop to clean up. Choosing a name overrides choosing a number"
AUTOCORRECT_STRING =
"Only clean auto-correctable 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"
DIRECTORY_STRING =
"Directory to work in (default: current directory)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



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

def initialize
  @number = 10
  @autocorrect_only = true
  @run_rubocop = true
  @filename = ".rubocop_todo.yml"
  @config = ".rubomop.yml"
  @directory = "."
  @todo_file = TodoFile.new(filename: full_path(@filename))
  @verbose = true
  @options_from_command_line = []
  @only = []
  @except = []
  @blocklist = []
  @name = nil
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

#blocklistObject

Returns the value of attribute blocklist.



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

def blocklist
  @blocklist
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#directoryObject

Returns the value of attribute directory.



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

def directory
  @directory
end

#exceptObject

Returns the value of attribute except.



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

def except
  @except
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
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.



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

def only
  @only
end

#options_from_command_lineObject

Returns the value of attribute options_from_command_line.



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

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

#todo_fileObject

Returns the value of attribute todo_file.



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

def todo_file
  @todo_file
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#backup_existing_fileObject



151
152
153
154
155
156
# File 'lib/rubomop/runner.rb', line 151

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

#execute(args) ⇒ Object



35
36
37
38
# File 'lib/rubomop/runner.rb', line 35

def execute(args)
  load_options(args)
  run
end

#load_from_fileObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubomop/runner.rb', line 47

def load_from_file
  config_path = full_path(config)
  return unless File.exist?(config_path)

  file_options = YAML.safe_load_file(config_path)
  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



40
41
42
43
44
45
# File 'lib/rubomop/runner.rb', line 40

def load_options(args)
  parse(args)
  load_from_file
  # Recreate todo_file with the correct directory after options are loaded
  @todo_file = TodoFile.new(filename: full_path(@filename))
end

#mopObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubomop/runner.rb', line 120

def mop
  if name
    NamedMop.new(
      todo_file:,
      name:,
      verbose:,
      run_rubocop:
    )
  else
    RandomMop.new(
      todo_file:,
      number:,
      autocorrect_only:,
      verbose:,
      run_rubocop:,
      only:,
      except:,
      blocklist:
    )
  end
end

#parse(args) ⇒ Object



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

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("-dDIRECTORY", "--directory=DIRECTORY", "--dir=DIRECTORY", DIRECTORY_STRING) do |value|
      self.directory = value
      @options_from_command_line << "directory"
    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|
      blocklist << value
      @options_from_command_line << "block"
    end
    opts.on("--name=NAME", NAME_STRING) do |value|
      self.name = value
      @options_from_command_line << "name"
    end
    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end
  option_parser.parse(args)
end

#runObject



142
143
144
145
146
147
148
149
# File 'lib/rubomop/runner.rb', line 142

def run
  self.todo_file = TodoFile.new(filename: full_path(filename))&.parse
  return if todo_file.nil?

  backup_existing_file
  mop.mop!
  todo_file&.save!
end