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)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

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_onlyObject

Returns the value of attribute autocorrect_only.



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

def autocorrect_only
  @autocorrect_only
end

#filenameObject

Returns the value of attribute filename.



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

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

#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.



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

def todo
  @todo
end

Instance Method Details

#backup_existing_fileObject



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.banner = "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_runnerObject



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

#runObject



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|
    delete_options = todo&.delete_options(autocorrect_only: autocorrect_only)
    next if delete_options.empty?
    object_to_delete = delete_options.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_fileObject



73
74
75
# File 'lib/rubomop/runner.rb', line 73

def save_new_file
  File.write(filename, todo&.output || "")
end