module Bundler::GemHelpers

def generic(p)

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

def generic_local_platform

def generic_local_platform
  generic(local_platform)
end

def local_platform

def local_platform
  Bundler.local_platform
end

def platform_specificity_match(spec_platform, user_platform)

def platform_specificity_match(spec_platform, user_platform)
  spec_platform = Gem::Platform.new(spec_platform)
  PlatformMatch.specificity_score(spec_platform, user_platform)
end

def same_deps(spec, exemplary_spec)

def same_deps(spec, exemplary_spec)
  same_runtime_deps = spec.dependencies.sort == exemplary_spec.dependencies.sort
  return same_runtime_deps unless spec.is_a?(Gem::Specification) && exemplary_spec.is_a?(Gem::Specification)
  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

def same_specificity(platform, spec, exemplary_spec)

def same_specificity(platform, spec, exemplary_spec)
  platform_specificity_match(spec.platform, platform) == platform_specificity_match(exemplary_spec.platform, platform)
end

def select_best_platform_match(specs, platform)

def select_best_platform_match(specs, platform)
  matching = specs.select {|spec| spec.match_platform(platform) }
  exact = matching.select {|spec| spec.platform == platform }
  return exact if exact.any?
  sorted_matching = matching.sort_by {|spec| platform_specificity_match(spec.platform, platform) }
  exemplary_spec = sorted_matching.first
  sorted_matching.take_while{|spec| same_specificity(platform, spec, exemplary_spec) && same_deps(spec, exemplary_spec) }
end