Sha256: d28b2067c1f9c2d7b1136b31a40849c3dab6ebe91b485723621335cc96944c85

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require 'pathname'
require 'set'

module Slimembedcop
  # Collect file paths from given path patterns.
  class PathFinder
    def initialize(options, config)
      @default_patterns = options.default_path_patterns
      @exclude_patterns = config.for_all_cops['Exclude'] || []
      @path_patterns = options.args
    end

    def run
      matching_paths(patterns) { |path| !excluded?(path) }.sort
    end

    private

    def matching_paths(patterns, &block)
      patterns.each_with_object(Set.new) do |pattern, set|
        ::Pathname.glob(pattern) do |pathname|
          next unless pathname.file?

          path = pathname.expand_path.to_s
          set.add(path) if block.nil? || yield(path)
        end
      end
    end

    def excluded?(path)
      excluded.include?(path)
    end

    def excluded
      @excluded ||= matching_paths(@exclude_patterns)
    end

    def patterns
      return @default_patterns if @path_patterns.empty?

      @path_patterns.map do |pattern|
        next pattern unless File.directory?(pattern)

        @default_patterns.map do |default|
          File.join(pattern, default)
        end.flatten
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slimembedcop-1.0.0 lib/slimembedcop/path_finder.rb
slimembedcop-0.2.0 lib/slimembedcop/path_finder.rb