2022-03-09 19:08:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
require 'active_support/inflector/inflections'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
# Helper class to manage file access to RuboCop TODOs in .rubocop_todo directory.
|
|
|
|
class TodoDir
|
2022-05-18 14:08:05 -04:00
|
|
|
# Suffix a TODO file.
|
|
|
|
SUFFIX_YAML = '.yml'
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
# Suffix to indicate TODOs being inspected right now.
|
|
|
|
SUFFIX_INSPECT = '.inspect'
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Instantiates a TodoDir.
|
|
|
|
#
|
|
|
|
# @param directory [String] base directory where all TODO YAML files are written to.
|
|
|
|
# @param inflector [ActiveSupport::Inflector, #underscore] an object which supports
|
|
|
|
# converting a string to its underscored version.
|
2022-03-09 19:08:36 -05:00
|
|
|
def initialize(directory, inflector: ActiveSupport::Inflector)
|
|
|
|
@directory = directory
|
|
|
|
@inflector = inflector
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Reads content of TODO YAML for given +cop_name+.
|
|
|
|
#
|
|
|
|
# @param cop_name [String] name of the cop rule
|
|
|
|
#
|
|
|
|
# @return [String, nil] content of the TODO YAML file if it exists
|
|
|
|
def read(cop_name)
|
|
|
|
path = path_for(cop_name)
|
|
|
|
|
|
|
|
File.read(path) if File.exist?(path)
|
2022-03-09 19:08:36 -05:00
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Saves +content+ for given +cop_name+ to TODO YAML file.
|
|
|
|
#
|
|
|
|
# @return [String] path of the written TODO YAML file
|
2022-03-09 19:08:36 -05:00
|
|
|
def write(cop_name, content)
|
|
|
|
path = path_for(cop_name)
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
File.write(path, content)
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Marks a TODO YAML file for inspection by renaming the original TODO YAML
|
|
|
|
# and appending the suffix +.inspect+ to it.
|
|
|
|
#
|
|
|
|
# @return [Boolean] +true+ a file was marked for inspection successfully.
|
2022-03-09 19:08:36 -05:00
|
|
|
def inspect(cop_name)
|
|
|
|
path = path_for(cop_name)
|
|
|
|
|
|
|
|
if File.exist?(path)
|
|
|
|
FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}")
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Marks all TODO YAML files for inspection.
|
|
|
|
#
|
|
|
|
# @return [Integer] number of renamed YAML TODO files.
|
|
|
|
#
|
|
|
|
# @see inspect
|
2022-03-09 19:08:36 -05:00
|
|
|
def inspect_all
|
2022-05-18 14:08:05 -04:00
|
|
|
pattern = File.join(@directory, "**/*#{SUFFIX_YAML}")
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
Dir.glob(pattern).count do |path|
|
|
|
|
FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Returns a list of TODO YAML files which are marked for inspection.
|
|
|
|
#
|
|
|
|
# @return [Array<String>] list of paths
|
|
|
|
#
|
|
|
|
# @see inspect
|
|
|
|
# @see inspect_all
|
2022-03-09 19:08:36 -05:00
|
|
|
def list_inspect
|
2022-05-18 14:08:05 -04:00
|
|
|
pattern = File.join(@directory, "**/*#{SUFFIX_YAML}#{SUFFIX_INSPECT}")
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
Dir.glob(pattern)
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
# Deletes a list of TODO yaml files which were marked for inspection.
|
|
|
|
#
|
|
|
|
# @return [Integer] number of deleted YAML TODO files.
|
|
|
|
#
|
|
|
|
# @see #inspect
|
|
|
|
# @see #inspect_all
|
2022-03-09 19:08:36 -05:00
|
|
|
def delete_inspected
|
2022-05-18 14:08:05 -04:00
|
|
|
list_inspect.count do |path|
|
2022-03-09 19:08:36 -05:00
|
|
|
File.delete(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
def path_for(cop_name)
|
|
|
|
todo_path = "#{@inflector.underscore(cop_name)}#{SUFFIX_YAML}"
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
File.join(@directory, todo_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|