module Bundler::GemHelpers - bundler: Ruby Standard Library Documentation

# module Bundler::GemHelpers

### Constants

GENERICS
GENERIC_CACHE

### Public Class Methods

generic(p) click to toggle source

    # File bundler/gem_helpers.rb, line 17
    def generic(p)
      GENERIC_CACHE[p] ||= begin
        _, found = GENERICS.find do |match, _generic|
          p.os == match.os && (!match.cpu || p.cpu == match.cpu)
        end
        found || Gem::Platform::RUBY
      end
    end

generic_local_platform() click to toggle source

    # File bundler/gem_helpers.rb, line 27
    def generic_local_platform
      generic(local_platform)
    end

generic_local_platform_is_ruby?() click to toggle source

    # File bundler/gem_helpers.rb, line 37
    def generic_local_platform_is_ruby?
      generic_local_platform == Gem::Platform::RUBY
    end

local_platform() click to toggle source

    # File bundler/gem_helpers.rb, line 32
    def local_platform
      Bundler.local_platform
    end

platform_specificity_match(spec_platform, user_platform) click to toggle source

    # File bundler/gem_helpers.rb, line 42
    def platform_specificity_match(spec_platform, user_platform)
      spec_platform = Gem::Platform.new(spec_platform)

      PlatformMatch.specificity_score(spec_platform, user_platform)
    end

same_deps(spec, exemplary_spec) click to toggle source

    # File bundler/gem_helpers.rb, line 143
    def same_deps(spec, exemplary_spec)
      same_runtime_deps = spec.dependencies.sort == exemplary_spec.dependencies.sort
      same_metadata_deps = spec.required_ruby_version == exemplary_spec.required_ruby_version && spec.required_rubygems_version == exemplary_spec.required_rubygems_version
      same_runtime_deps && same_metadata_deps
    end

same_specificity(platform, spec, exemplary_spec) click to toggle source

    # File bundler/gem_helpers.rb, line 138
    def same_specificity(platform, spec, exemplary_spec)
      platform_specificity_match(spec.platform, platform) == platform_specificity_match(exemplary_spec.platform, platform)
    end

select_all_platform_match(specs, platform, force_ruby: false, prefer_locked: false) click to toggle source

    # File bundler/gem_helpers.rb, line 49
    def select_all_platform_match(specs, platform, force_ruby: false, prefer_locked: false)
      matching = if force_ruby
        specs.select {|spec| spec.match_platform(Gem::Platform::RUBY) && spec.force_ruby_platform! }
      else
        specs.select {|spec| spec.match_platform(platform) }
      end

      if prefer_locked
        locked_originally = matching.select {|spec| spec.is_a?(LazySpecification) }
        return locked_originally if locked_originally.any?
      end

      matching
    end

select_best_local_platform_match(specs, force_ruby: false) click to toggle source

    # File bundler/gem_helpers.rb, line 72
    def select_best_local_platform_match(specs, force_ruby: false)
      matching = select_all_platform_match(specs, local_platform, force_ruby: force_ruby).filter_map(&:materialized_for_installation)

      sort_best_platform_match(matching, local_platform)
    end

select_best_platform_match(specs, platform, force_ruby: false, prefer_locked: false) click to toggle source

    # File bundler/gem_helpers.rb, line 65
    def select_best_platform_match(specs, platform, force_ruby: false, prefer_locked: false)
      matching = select_all_platform_match(specs, platform, force_ruby: force_ruby, prefer_locked: prefer_locked)

      sort_and_filter_best_platform_match(matching, platform)
    end

sort_and_filter_best_platform_match(matching, platform) click to toggle source

    # File bundler/gem_helpers.rb, line 79
    def sort_and_filter_best_platform_match(matching, platform)
      return matching if matching.one?

      exact = matching.select {|spec| spec.platform == platform }
      return exact if exact.any?

      sorted_matching = sort_best_platform_match(matching, platform)
      exemplary_spec = sorted_matching.first

      sorted_matching.take_while {|spec| same_specificity(platform, spec, exemplary_spec) && same_deps(spec, exemplary_spec) }
    end

sort_best_platform_match(matching, platform) click to toggle source

    # File bundler/gem_helpers.rb, line 92
    def sort_best_platform_match(matching, platform)
      matching.sort_by {|spec| platform_specificity_match(spec.platform, platform) }
    end

### Private Instance Methods

generic(p) click to toggle source

    # File bundler/gem_helpers.rb, line 17
    def generic(p)
      GENERIC_CACHE[p] ||= begin
        _, found = GENERICS.find do |match, _generic|
          p.os == match.os && (!match.cpu || p.cpu == match.cpu)
        end
        found || Gem::Platform::RUBY
      end
    end

generic_local_platform() click to toggle source

    # File bundler/gem_helpers.rb, line 27
    def generic_local_platform
      generic(local_platform)
    end

generic_local_platform_is_ruby?() click to toggle source

    # File bundler/gem_helpers.rb, line 37
    def generic_local_platform_is_ruby?
      generic_local_platform == Gem::Platform::RUBY
    end

local_platform() click to toggle source

    # File bundler/gem_helpers.rb, line 32
    def local_platform
      Bundler.local_platform
    end

platform_specificity_match(spec_platform, user_platform) click to toggle source

    # File bundler/gem_helpers.rb, line 42
    def platform_specificity_match(spec_platform, user_platform)
      spec_platform = Gem::Platform.new(spec_platform)

      PlatformMatch.specificity_score(spec_platform, user_platform)
    end

same_deps(spec, exemplary_spec) click to toggle source

    # File bundler/gem_helpers.rb, line 143
    def same_deps(spec, exemplary_spec)
      same_runtime_deps = spec.dependencies.sort == exemplary_spec.dependencies.sort
      same_metadata_deps = spec.required_ruby_version == exemplary_spec.required_ruby_version && spec.required_rubygems_version == exemplary_spec.required_rubygems_version
      same_runtime_deps && same_metadata_deps
    end

same_specificity(platform, spec, exemplary_spec) click to toggle source

    # File bundler/gem_helpers.rb, line 138
    def same_specificity(platform, spec, exemplary_spec)
      platform_specificity_match(spec.platform, platform) == platform_specificity_match(exemplary_spec.platform, platform)
    end

select_all_platform_match(specs, platform, force_ruby: false, prefer_locked: false) click to toggle source

    # File bundler/gem_helpers.rb, line 49
    def select_all_platform_match(specs, platform, force_ruby: false, prefer_locked: false)
      matching = if force_ruby
        specs.select {|spec| spec.match_platform(Gem::Platform::RUBY) && spec.force_ruby_platform! }
      else
        specs.select {|spec| spec.match_platform(platform) }
      end

      if prefer_locked
        locked_originally = matching.select {|spec| spec.is_a?(LazySpecification) }
        return locked_originally if locked_originally.any?
      end

      matching
    end

select_best_local_platform_match(specs, force_ruby: false) click to toggle source

    # File bundler/gem_helpers.rb, line 72
    def select_best_local_platform_match(specs, force_ruby: false)
      matching = select_all_platform_match(specs, local_platform, force_ruby: force_ruby).filter_map(&:materialized_for_installation)

      sort_best_platform_match(matching, local_platform)
    end

select_best_platform_match(specs, platform, force_ruby: false, prefer_locked: false) click to toggle source

    # File bundler/gem_helpers.rb, line 65
    def select_best_platform_match(specs, platform, force_ruby: false, prefer_locked: false)
      matching = select_all_platform_match(specs, platform, force_ruby: force_ruby, prefer_locked: prefer_locked)

      sort_and_filter_best_platform_match(matching, platform)
    end

sort_and_filter_best_platform_match(matching, platform) click to toggle source

    # File bundler/gem_helpers.rb, line 79
    def sort_and_filter_best_platform_match(matching, platform)
      return matching if matching.one?

      exact = matching.select {|spec| spec.platform == platform }
      return exact if exact.any?

      sorted_matching = sort_best_platform_match(matching, platform)
      exemplary_spec = sorted_matching.first

      sorted_matching.take_while {|spec| same_specificity(platform, spec, exemplary_spec) && same_deps(spec, exemplary_spec) }
    end

sort_best_platform_match(matching, platform) click to toggle source

    # File bundler/gem_helpers.rb, line 92
    def sort_best_platform_match(matching, platform)
      matching.sort_by {|spec| platform_specificity_match(spec.platform, platform) }
    end