Sha256: 3e0336e8f475640c3fc7d47152c424500483e2b120b857d407ccb1e6fb82c852

Contents?: true

Size: 1.14 KB

Versions: 10

Compression:

Stored size: 1.14 KB

Contents

require "pathname"

module Rubycritic

  class SourceLocator
    RUBY_EXTENSION = ".rb"
    RUBY_FILES = File.join("**", "*#{RUBY_EXTENSION}")

    def initialize(paths)
      @initial_paths = Array(paths)
    end

    def paths
      @paths ||= pathnames.map(&:to_s)
    end

    def pathnames
      @pathnames ||= expand_paths
    end

    private

    def deduplicate_symlinks(path_list)
      # sort the symlinks to the end so files are preferred
      path_list.sort_by! { |path| File.symlink?(path.cleanpath) ? "z" : "a" }
      if defined?(JRUBY_VERSION)
        require "java"
        path_list.uniq! do |path|
          java.io.File.new(path.realpath.to_s).canonical_path
        end
      else
        path_list.uniq!(&:realpath)
      end
    end

    def expand_paths
      path_list = @initial_paths.flat_map do |path|
        if File.directory?(path)
          Pathname.glob(File.join(path, RUBY_FILES))
        elsif File.exist?(path) && File.extname(path) == RUBY_EXTENSION
          Pathname.new(path)
        end
      end.compact

      deduplicate_symlinks(path_list) if Config.deduplicate_symlinks

      path_list.map(&:cleanpath)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubycritic-2.6.0 lib/rubycritic/source_locator.rb
rubycritic-2.5.0 lib/rubycritic/source_locator.rb
rubycritic-2.4.1 lib/rubycritic/source_locator.rb
rubycritic-2.4.0 lib/rubycritic/source_locator.rb
rubycritic-2.3.0 lib/rubycritic/source_locator.rb
rubycritic-2.2.0 lib/rubycritic/source_locator.rb
rubycritic-2.1.0 lib/rubycritic/source_locator.rb
rubycritic-2.0.0 lib/rubycritic/source_locator.rb
rubycritic-1.4.0 lib/rubycritic/source_locator.rb
rubycritic-1.3.0 lib/rubycritic/source_locator.rb