Sha256: 218d9a761f830782ff75d19e0c275c280b2d1551bb341efb2268405ac4b431c5

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module RuboCop
  # Common methods and behaviors for dealing with paths.
  module PathUtil
    module_function

    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

    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

    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

    # Returns true for an absolute Unix or Windows path.
    def absolute?(path)
      path =~ %r{\A([A-Z]:)?/}
    end

    def self.pwd
      @pwd ||= Dir.pwd
    end

    def self.reset_pwd
      @pwd = nil
    end

    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

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

Version data entries

14 entries across 12 versions & 2 rubygems

Version Path
rubocop-0.64.0 lib/rubocop/path_util.rb
rubocop-0.63.1 lib/rubocop/path_util.rb
rubocop-0.63.0 lib/rubocop/path_util.rb
rubocop-0.62.0 lib/rubocop/path_util.rb
rubocop-0.61.1 lib/rubocop/path_util.rb
rubocop-0.61.0 lib/rubocop/path_util.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/path_util.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/path_util.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/path_util.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/path_util.rb
rubocop-0.60.0 lib/rubocop/path_util.rb
rubocop-0.59.2 lib/rubocop/path_util.rb
rubocop-0.59.1 lib/rubocop/path_util.rb
rubocop-0.59.0 lib/rubocop/path_util.rb