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.
- .find_file_upwards(filename, start_dir = PathUtil.pwd) ⇒ Object
- .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.
54 55 56 |
# File 'lib/rubocop/path_util.rb', line 54 def absolute?(path) path =~ %r{\A([A-Z]:)?/} end |
.find_file_upwards(filename, start_dir = PathUtil.pwd) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rubocop/path_util.rb', line 46 def find_file_upwards(filename, start_dir = PathUtil.pwd) Pathname(File.(start_dir)).ascend do |dir| file = File.join(dir, filename) return file if File.exist?(file) end end |
.match_path?(pattern, path) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rubocop/path_util.rb', line 32 def match_path?(pattern, path) case pattern when String File.fnmatch?(pattern, path, File::FNM_PATHNAME) when Regexp begin path =~ pattern rescue ArgumentError => e return false if e..start_with?('invalid byte sequence') raise e end end end |
.pwd ⇒ Object
58 59 60 |
# File 'lib/rubocop/path_util.rb', line 58 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 |
# 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)) path_name.relative_path_from(Pathname.new(base_dir)).to_s end |
.reset_pwd ⇒ Object
62 63 64 |
# File 'lib/rubocop/path_util.rb', line 62 def self.reset_pwd @pwd = nil end |