Class: Rubomop::Runner
- Inherits:
-
Object
- Object
- Rubomop::Runner
- 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)"
Instance Attribute Summary collapse
-
#autocorrect_only ⇒ Object
Returns the value of attribute autocorrect_only.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#number ⇒ Object
Returns the value of attribute number.
-
#run_rubocop ⇒ Object
Returns the value of attribute run_rubocop.
-
#todo ⇒ Object
Returns the value of attribute todo.
Instance Method Summary collapse
- #backup_existing_file ⇒ Object
- #execute(args) ⇒ Object
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #parse(args) ⇒ Object
- #rubocop_runner ⇒ Object
- #run ⇒ Object
- #save_new_file ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
11 12 13 14 15 16 17 |
# File 'lib/rubomop/runner.rb', line 11 def initialize @number = 10 @autocorrect_only = true @run_rubocop = true @filename = ".rubocop_todo.yml" @todo = nil end |
Instance Attribute Details
#autocorrect_only ⇒ Object
Returns the value of attribute autocorrect_only.
3 4 5 |
# File 'lib/rubomop/runner.rb', line 3 def autocorrect_only @autocorrect_only end |
#filename ⇒ Object
Returns the value of attribute filename.
3 4 5 |
# File 'lib/rubomop/runner.rb', line 3 def filename @filename end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/rubomop/runner.rb', line 3 def number @number end |
#run_rubocop ⇒ Object
Returns the value of attribute run_rubocop.
3 4 5 |
# File 'lib/rubomop/runner.rb', line 3 def run_rubocop @run_rubocop end |
#todo ⇒ Object
Returns the value of attribute todo.
3 4 5 |
# File 'lib/rubomop/runner.rb', line 3 def todo @todo end |
Instance Method Details
#backup_existing_file ⇒ Object
68 69 70 71 |
# File 'lib/rubomop/runner.rb', line 68 def backup_existing_file FileUtils.rm("#{filename}.bak") if File.exist?("#{filename}.bak") FileUtils.mv(filename, "#{filename}.bak") end |
#execute(args) ⇒ Object
19 20 21 22 |
# File 'lib/rubomop/runner.rb', line 19 def execute(args) parse(args) run end |
#parse(args) ⇒ Object
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 |
# File 'lib/rubomop/runner.rb', line 24 def parse(args) option_parser = OptionParser.new do |opts| opts. = "Usage: rubomop [options]" opts.on("-nNUMBER", "--number NUMBER", Integer, NUM_STRING) do |value| self.number = value end opts.on("-a", "--autocorrect-only", AUTOCORRECT_STRING) do self.autocorrect_only = true end opts.on("--no_autocorrect-only", NO_AUTOCORRECT_STRING) do self.autocorrect_only = false end opts.on("-r", "--run-rubocop", RUBOCOP_STRING) do self.run_rubocop = true end opts.on("-fFILENAME", "--filename FILENAME", FILENAME_STRING) do |value| self.filename = value end opts.on("--no-run-rubocop", NO_RUBOCOP_STRING) do self.run_rubocop = false end opts.on("-h", "--help", "Prints this help") do puts opts exit end end option_parser.parse(args) end |
#rubocop_runner ⇒ Object
77 78 79 80 81 |
# File 'lib/rubomop/runner.rb', line 77 def rubocop_runner return unless run_rubocop print "Running bundle exec rubocop -aD" system("bundle exec rubocop -aD") end |
#run ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubomop/runner.rb', line 53 def run self.todo = TodoFile.new(filename: filename)&.parse return if todo.nil? number.times do |i| = todo&.(autocorrect_only: autocorrect_only) next if .empty? object_to_delete = .sample print "#{i + 1}: Deleting #{object_to_delete[:file]} from #{object_to_delete[:cop].name}\n" todo&.delete!(object_to_delete) end backup_existing_file save_new_file rubocop_runner end |
#save_new_file ⇒ Object
73 74 75 |
# File 'lib/rubomop/runner.rb', line 73 def save_new_file File.write(filename, todo&.output || "") end |