Module: RuboCop::PathUtil
- Included in:
- Config, Cop::Util, Formatter::DisabledLinesFormatter, Formatter::HTMLFormatter::ERBContext, Formatter::JSONFormatter, Formatter::SimpleTextFormatter
- Defined in:
- lib/rubocop/path_util.rb
Overview
Common methods and behaviors for dealing with paths.
Class Method Summary collapse
-
.absolute?(path) ⇒ Boolean
Returns true for an absolute Unix or Windows path.
- .chdir(dir, &block) ⇒ Object
- .hidden_dir?(path) ⇒ Boolean
- .hidden_file_in_not_hidden_dir?(pattern, path) ⇒ Boolean
- .match_path?(pattern, path) ⇒ Boolean
- .pwd ⇒ Object
- .relative_path(path, base_dir = PathUtil.pwd) ⇒ Object
- .reset_pwd ⇒ Object
- .smart_path(path) ⇒ Object
Class Method Details
.absolute?(path) ⇒ Boolean
Returns true for an absolute Unix or Windows path.
53 54 55 |
# File 'lib/rubocop/path_util.rb', line 53 def absolute?(path) path =~ %r{\A([A-Z]:)?/}i end |
.chdir(dir, &block) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/rubocop/path_util.rb', line 65 def self.chdir(dir, &block) reset_pwd Dir.chdir(dir, &block) ensure reset_pwd end |
.hidden_dir?(path) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/rubocop/path_util.rb', line 79 def hidden_dir?(path) File.dirname(path).split(File::SEPARATOR).any? do |dir| dir.start_with?('.') end end |
.hidden_file_in_not_hidden_dir?(pattern, path) ⇒ Boolean
72 73 74 75 76 77 |
# File 'lib/rubocop/path_util.rb', line 72 def hidden_file_in_not_hidden_dir?(pattern, path) File.fnmatch?( pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH ) && File.basename(path).start_with?('.') && !hidden_dir?(path) end |
.match_path?(pattern, path) ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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..start_with?('invalid byte sequence') raise e end end end |
.pwd ⇒ Object
57 58 59 |
# File 'lib/rubocop/path_util.rb', line 57 def self.pwd @pwd ||= Dir.pwd end |
.relative_path(path, base_dir = PathUtil.pwd) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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.(path)) begin path_name.relative_path_from(Pathname.new(base_dir)).to_s rescue ArgumentError path end end |
.reset_pwd ⇒ Object
61 62 63 |
# File 'lib/rubocop/path_util.rb', line 61 def self.reset_pwd @pwd = nil end |