Module: RuboCop::PathUtil

Overview

Common methods and behaviors for dealing with paths.

Constant Summary collapse

HIDDEN_FILE_PATTERN =
"#{File::SEPARATOR}."

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.relative_paths_cacheObject

Returns the value of attribute relative_paths_cache.



7
8
9
# File 'lib/rubocop/path_util.rb', line 7

def relative_paths_cache
  @relative_paths_cache
end

Class Method Details

.absolute?(path) ⇒ Boolean

Returns true for an absolute Unix or Windows path.

Returns:

  • (Boolean)


95
96
97
# File 'lib/rubocop/path_util.rb', line 95

def absolute?(path)
  %r{\A([A-Z]:)?/}i.match?(path)
end

.glob?(path) ⇒ Boolean

Returns true for a glob

Returns:

  • (Boolean)


100
101
102
# File 'lib/rubocop/path_util.rb', line 100

def glob?(path)
  path.match?(/[*{\[?]/)
end

.hidden_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rubocop/path_util.rb', line 130

def hidden_dir?(path)
  File.dirname(path).split(File::SEPARATOR).any? { |dir| dir.start_with?('.') }
end

.hidden_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/rubocop/path_util.rb', line 113

def hidden_file?(path)
  maybe_hidden_file?(path) && File.basename(path).start_with?('.')
end

.hidden_file_in_not_hidden_dir?(pattern, path) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
# File 'lib/rubocop/path_util.rb', line 104

def hidden_file_in_not_hidden_dir?(pattern, path)
  hidden_file?(path) &&
    File.fnmatch?(
      pattern, path,
      File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
    ) &&
    !hidden_dir?(path)
end

.match_path?(pattern, path) ⇒ Boolean

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Returns:

  • (Boolean)


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
# File 'lib/rubocop/path_util.rb', line 67

def match_path?(pattern, path)
  case pattern
  when String
    matches =
      if pattern == path
        true
      elsif glob?(pattern)
        # File name matching doesn't really work with relative patterns that start with "..". We
        # get around that problem by converting the pattern to an absolute path.
        pattern = File.expand_path(pattern) if pattern.start_with?('..')

        File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
      end

    matches || hidden_file_in_not_hidden_dir?(pattern, path)
  when Regexp
    begin
      pattern.match?(path)
    rescue ArgumentError => e
      return false if e.message.start_with?('invalid byte sequence')

      raise e
    end
  end
end

.maybe_hidden_file?(path) ⇒ Boolean

Loose check to reduce memory allocations

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/rubocop/path_util.rb', line 120

def maybe_hidden_file?(path)
  return false unless path.include?(HIDDEN_FILE_PATTERN)

  separator_index = path.rindex(File::SEPARATOR)
  return false unless separator_index

  dot_index = path.index('.', separator_index + 1)
  dot_index == separator_index + 1
end

.pwdObject

Returns the current working directory, cached for the duration of a run. Dir.pwd is a syscall; caching it avoids repeated overhead since RuboCop never changes the working directory during a run.



16
17
18
# File 'lib/rubocop/path_util.rb', line 16

def pwd
  @pwd ||= Dir.pwd
end

.relative_path(path, base_dir = PathUtil.pwd) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/path_util.rb', line 25

def relative_path(path, base_dir = PathUtil.pwd)
  PathUtil.relative_paths_cache[base_dir][path] ||=
    # Optimization for the common case where path begins with the base
    # dir. Just cut off the first part.
    if path.start_with?(base_dir)
      base_dir_length = base_dir.length
      result_length = path.length - base_dir_length - 1
      path[base_dir_length + 1, result_length]
    else
      path_name = Pathname.new(File.expand_path(path))
      begin
        path_name.relative_path_from(Pathname.new(base_dir)).to_s
      rescue ArgumentError
        path
      end
    end
end

.remote_file?(uri) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubocop/path_util.rb', line 43

def remote_file?(uri)
  uri.start_with?('http://', 'https://')
end

.reset_pwdObject

Reset the cached pwd. Only needed in tests that use Dir.chdir.



21
22
23
# File 'lib/rubocop/path_util.rb', line 21

def reset_pwd
  @pwd = nil
end

.smart_path(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/path_util.rb', line 50

def smart_path(path)
  SMART_PATH_CACHE[path] ||=
    if path.is_a?(RemoteConfig)
      path.uri.to_s
    else
      # Ideally, we calculate this relative to the project root.
      base_dir = PathUtil.pwd

      if path.start_with? base_dir
        relative_path(path, base_dir)
      else
        path
      end
    end
end