Welcome to mirror list, hosted at ThFree Co, Russian Federation.

todo_dir.rb « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57b9895a925b17adabe901576216edd896126bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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
    # Suffix a TODO file.
    SUFFIX_YAML = '.yml'

    # Suffix to indicate TODOs being inspected right now.
    SUFFIX_INSPECT = '.inspect'

    # 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.
    def initialize(directory, inflector: ActiveSupport::Inflector)
      @directory = directory
      @inflector = inflector
    end

    # 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)
    end

    # Saves +content+ for given +cop_name+ to TODO YAML file.
    #
    # @return [String] path of the written TODO YAML file
    def write(cop_name, content)
      path = path_for(cop_name)

      FileUtils.mkdir_p(File.dirname(path))
      File.write(path, content)

      path
    end

    # 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.
    def inspect(cop_name)
      path = path_for(cop_name)

      if File.exist?(path)
        FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}")
        true
      else
        false
      end
    end

    # Marks all TODO YAML files for inspection.
    #
    # @return [Integer] number of renamed YAML TODO files.
    #
    # @see inspect
    def inspect_all
      pattern = File.join(@directory, "**/*#{SUFFIX_YAML}")

      Dir.glob(pattern).count do |path|
        FileUtils.mv(path, "#{path}#{SUFFIX_INSPECT}")
      end
    end

    # Returns a list of TODO YAML files which are marked for inspection.
    #
    # @return [Array<String>] list of paths
    #
    # @see inspect
    # @see inspect_all
    def list_inspect
      pattern = File.join(@directory, "**/*#{SUFFIX_YAML}#{SUFFIX_INSPECT}")

      Dir.glob(pattern)
    end

    # 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
    def delete_inspected
      list_inspect.count do |path|
        File.delete(path)
      end
    end

    private

    def path_for(cop_name)
      todo_path = "#{@inflector.underscore(cop_name)}#{SUFFIX_YAML}"

      File.join(@directory, todo_path)
    end
  end
end