debian-mirror-gitlab/rubocop/todo_dir.rb

109 lines
2.8 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# 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-07-23 23:45:48 +05:30
# Suffix a TODO file.
SUFFIX_YAML = '.yml'
2022-05-07 20:08:51 +05:30
# Suffix to indicate TODOs being inspected right now.
SUFFIX_INSPECT = '.inspect'
2022-07-23 23:45:48 +05:30
# 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-05-07 20:08:51 +05:30
def initialize(directory, inflector: ActiveSupport::Inflector)
@directory = directory
@inflector = inflector
end
2022-07-23 23:45:48 +05:30
# 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-05-07 20:08:51 +05:30
end
2022-07-23 23:45:48 +05:30
# Saves +content+ for given +cop_name+ to TODO YAML file.
#
# @return [String] path of the written TODO YAML file
2022-05-07 20:08:51 +05:30
def write(cop_name, content)
path = path_for(cop_name)
FileUtils.mkdir_p(File.dirname(path))
File.write(path, content)
path
end
2022-07-23 23:45:48 +05:30
# 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-05-07 20:08:51 +05:30
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-07-23 23:45:48 +05:30
# Marks all TODO YAML files for inspection.
#
# @return [Integer] number of renamed YAML TODO files.
#
# @see inspect
2022-05-07 20:08:51 +05:30
def inspect_all
2022-07-23 23:45:48 +05:30
pattern = File.join(@directory, "**/*#{SUFFIX_YAML}")
2022-05-07 20:08:51 +05:30
Dir.glob(pattern).count do |path|
FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}")
end
end
2022-07-23 23:45:48 +05:30
# Returns a list of TODO YAML files which are marked for inspection.
#
# @return [Array<String>] list of paths
#
# @see inspect
# @see inspect_all
2022-05-07 20:08:51 +05:30
def list_inspect
2022-07-23 23:45:48 +05:30
pattern = File.join(@directory, "**/*#{SUFFIX_YAML}#{SUFFIX_INSPECT}")
2022-05-07 20:08:51 +05:30
Dir.glob(pattern)
end
2022-07-23 23:45:48 +05:30
# 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-05-07 20:08:51 +05:30
def delete_inspected
2022-07-23 23:45:48 +05:30
list_inspect.count do |path|
2022-05-07 20:08:51 +05:30
File.delete(path)
end
end
private
2022-07-23 23:45:48 +05:30
def path_for(cop_name)
todo_path = "#{@inflector.underscore(cop_name)}#{SUFFIX_YAML}"
2022-05-07 20:08:51 +05:30
File.join(@directory, todo_path)
end
end
end