Sha256: 4415bab4401aef249f9a3b14fe77a5cb26de147332aa1aa3e6dc62f974e43954

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'pathname'

module RuboCop
  # Common methods for finding files.
  module FileFinder
    def self.root_level=(level)
      @root_level = level
    end

    def self.root_level?(path)
      @root_level == path.to_s
    end

    def find_file_upwards(filename, start_dir, use_home: false)
      traverse_files_upwards(filename, start_dir, use_home) do |file|
        # minimize iteration for performance
        return file if file
      end
    end

    def find_files_upwards(filename, start_dir, use_home: false)
      files = []
      traverse_files_upwards(filename, start_dir, use_home) do |file|
        files << file
      end
      files
    end

    private

    def traverse_files_upwards(filename, start_dir, use_home)
      Pathname.new(start_dir).expand_path.ascend do |dir|
        break if FileFinder.root_level?(dir)
        file = dir + filename
        yield(file.to_s) if file.exist?
      end

      return unless use_home && ENV.key?('HOME')
      file = File.join(Dir.home, filename)
      yield(file) if File.exist?(file)
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/file_finder.rb
rubocop-0.58.1 lib/rubocop/file_finder.rb
rubocop-0.58.0 lib/rubocop/file_finder.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/file_finder.rb
rubocop-0.57.2 lib/rubocop/file_finder.rb
rubocop-0.57.1 lib/rubocop/file_finder.rb
rubocop-0.57.0 lib/rubocop/file_finder.rb
rubocop-0.56.0 lib/rubocop/file_finder.rb
rubocop-0.55.0 lib/rubocop/file_finder.rb
rubocop-0.54.0 lib/rubocop/file_finder.rb
rubocop-0.53.0 lib/rubocop/file_finder.rb