Sha256: 0d31ab0fa93487cc14117a1dd660aa17a3e191452141d155390b8984f0ceaddc

Contents?: true

Size: 1018 Bytes

Versions: 3

Compression:

Stored size: 1018 Bytes

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, stop_dir)
      (@root_level || stop_dir) == path.to_s
    end

    def find_file_upwards(filename, start_dir, stop_dir = nil)
      traverse_files_upwards(filename, start_dir, stop_dir) do |file|
        # minimize iteration for performance
        return file if file
      end
    end

    def find_last_file_upwards(filename, start_dir, stop_dir = nil)
      last_file = nil
      traverse_files_upwards(filename, start_dir, stop_dir) do |file|
        last_file = file
      end
      last_file
    end

    private

    def traverse_files_upwards(filename, start_dir, stop_dir)
      Pathname.new(start_dir).expand_path.ascend do |dir|
        file = dir + filename
        yield(file.to_s) if file.exist?

        break if FileFinder.root_level?(dir, stop_dir)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/file_finder.rb
rubocop-0.89.0 lib/rubocop/file_finder.rb
rubocop-0.88.0 lib/rubocop/file_finder.rb