module RuboCop::PathUtil
Common methods and behaviors for dealing with paths.
Public Class Methods
chdir(dir, &block)
click to toggle source
# File lib/rubocop/path_util.rb, line 65 def self.chdir(dir, &block) reset_pwd Dir.chdir(dir, &block) ensure reset_pwd end
pwd()
click to toggle source
# File lib/rubocop/path_util.rb, line 57 def self.pwd @pwd ||= Dir.pwd end
reset_pwd()
click to toggle source
# File lib/rubocop/path_util.rb, line 61 def self.reset_pwd @pwd = nil end
Public Instance Methods
absolute?(path)
click to toggle source
Returns true for an absolute Unix or Windows path.
# File lib/rubocop/path_util.rb, line 53 def absolute?(path) path =~ %r{\A([A-Z]:)?/} end
match_path?(pattern, path)
click to toggle source
# File lib/rubocop/path_util.rb, line 36 def match_path?(pattern, path) case pattern when String File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) || hidden_file_in_not_hidden_dir?(pattern, path) when Regexp begin path =~ pattern rescue ArgumentError => e return false if e.message.start_with?('invalid byte sequence') raise e end end end
relative_path(path, base_dir = PathUtil.pwd)
click to toggle source
# File lib/rubocop/path_util.rb, line 8 def relative_path(path, base_dir = PathUtil.pwd) # 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 return path[base_dir_length + 1, result_length] end 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
smart_path(path)
click to toggle source
# File lib/rubocop/path_util.rb, line 25 def smart_path(path) # 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