diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-18 23:02:30 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-18 23:02:30 +0300 |
commit | 41fe97390ceddf945f3d967b8fdb3de4c66b7dea (patch) | |
tree | 9c8d89a8624828992f06d892cd2f43818ff5dcc8 /rubocop/todo_dir.rb | |
parent | 0804d2dc31052fb45a1efecedc8e06ce9bc32862 (diff) |
Add latest changes from gitlab-org/gitlab@14-9-stable-eev14.9.0-rc42
Diffstat (limited to 'rubocop/todo_dir.rb')
-rw-r--r-- | rubocop/todo_dir.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/rubocop/todo_dir.rb b/rubocop/todo_dir.rb new file mode 100644 index 00000000000..4aca4454a06 --- /dev/null +++ b/rubocop/todo_dir.rb @@ -0,0 +1,81 @@ +# 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 + DEFAULT_TODO_DIR = File.expand_path('../.rubocop_todo', __dir__) + + # Suffix to indicate TODOs being inspected right now. + SUFFIX_INSPECT = '.inspect' + + attr_reader :directory + + def initialize(directory, inflector: ActiveSupport::Inflector) + @directory = directory + @inflector = inflector + end + + def read(cop_name, suffix = nil) + read_suffixed(cop_name) + end + + def write(cop_name, content) + path = path_for(cop_name) + + FileUtils.mkdir_p(File.dirname(path)) + File.write(path, content) + + path + end + + def inspect(cop_name) + path = path_for(cop_name) + + if File.exist?(path) + FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}") + true + else + false + end + end + + def inspect_all + pattern = File.join(@directory, '**/*.yml') + + Dir.glob(pattern).count do |path| + FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}") + end + end + + def list_inspect + pattern = File.join(@directory, "**/*.yml.inspect") + + Dir.glob(pattern) + end + + def delete_inspected + pattern = File.join(@directory, '**/*.yml.inspect') + + Dir.glob(pattern).count do |path| + File.delete(path) + end + end + + private + + def read_suffixed(cop_name, suffix = nil) + path = path_for(cop_name, suffix) + + File.read(path) if File.exist?(path) + end + + def path_for(cop_name, suffix = nil) + todo_path = "#{@inflector.underscore(cop_name)}.yml#{suffix}" + + File.join(@directory, todo_path) + end + end +end |