2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2008-06-17 18:04:18 -04:00
|
|
|
require 'rubygems/remote_fetcher'
|
|
|
|
require 'rubygems/user_interaction'
|
2010-04-22 04:24:42 -04:00
|
|
|
require 'rubygems/errors'
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'rubygems/text'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/name_tuple'
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# SpecFetcher handles metadata updates from remote gem repositories.
|
|
|
|
|
|
|
|
class Gem::SpecFetcher
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2011-01-18 19:08:49 -05:00
|
|
|
include Gem::Text
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Cache of latest specs
|
|
|
|
|
|
|
|
attr_reader :latest_specs # :nodoc:
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
|
|
|
# Sources for this SpecFetcher
|
|
|
|
|
|
|
|
attr_reader :sources # :nodoc:
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2009-12-08 02:19:09 -05:00
|
|
|
# Cache of all released specs
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
attr_reader :specs # :nodoc:
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Cache of prerelease specs
|
|
|
|
|
|
|
|
attr_reader :prerelease_specs # :nodoc:
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
@fetcher = nil
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# Default fetcher instance. Use this instead of ::new to reduce object
|
|
|
|
# allocation.
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
def self.fetcher
|
|
|
|
@fetcher ||= new
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetcher=(fetcher) # :nodoc:
|
|
|
|
@fetcher = fetcher
|
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
2013-11-18 19:34:13 -05:00
|
|
|
# Creates a new SpecFetcher. Ordinarily you want to use the default fetcher
|
|
|
|
# from Gem::SpecFetcher::fetcher which uses the Gem.sources.
|
2013-11-10 12:51:40 -05:00
|
|
|
#
|
|
|
|
# If you need to retrieve specifications from a different +source+, you can
|
|
|
|
# send it as an argument.
|
|
|
|
|
|
|
|
def initialize sources = nil
|
|
|
|
@sources = sources || Gem.sources
|
|
|
|
|
2013-10-18 17:56:18 -04:00
|
|
|
@update_cache =
|
|
|
|
begin
|
|
|
|
File.stat(Gem.user_home).uid == Process.uid
|
|
|
|
rescue Errno::EACCES, Errno::ENOENT
|
|
|
|
false
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
@specs = {}
|
|
|
|
@latest_specs = {}
|
2009-06-09 17:38:59 -04:00
|
|
|
@prerelease_specs = {}
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
@caches = {
|
|
|
|
:latest => @latest_specs,
|
|
|
|
:prerelease => @prerelease_specs,
|
2012-11-29 01:52:18 -05:00
|
|
|
:released => @specs,
|
2011-01-18 19:08:49 -05:00
|
|
|
}
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
@fetcher = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
|
|
|
# Find and fetch gem name tuples that match +dependency+.
|
|
|
|
#
|
|
|
|
# If +matching_platform+ is false, gems for all platforms are returned.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def search_for_dependency(dependency, matching_platform=true)
|
|
|
|
found = {}
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
rejected_specs = {}
|
|
|
|
|
|
|
|
if dependency.prerelease?
|
2013-11-18 19:34:13 -05:00
|
|
|
if dependency.specific?
|
|
|
|
type = :complete
|
|
|
|
else
|
|
|
|
type = :abs_latest
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
elsif dependency.latest_version?
|
|
|
|
type = :latest
|
|
|
|
else
|
|
|
|
type = :released
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
list, errors = available_specs(type)
|
|
|
|
list.each do |source, specs|
|
2013-09-14 04:59:02 -04:00
|
|
|
if dependency.name.is_a?(String) && specs.respond_to?(:bsearch)
|
|
|
|
start_index = (0 ... specs.length).bsearch{ |i| specs[i].name >= dependency.name }
|
|
|
|
end_index = (0 ... specs.length).bsearch{ |i| specs[i].name > dependency.name }
|
|
|
|
specs = specs[start_index ... end_index] if start_index && end_index
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
found[source] = specs.select do |tup|
|
|
|
|
if dependency.match?(tup)
|
|
|
|
if matching_platform and !Gem::Platform.match(tup.platform)
|
|
|
|
pm = (
|
|
|
|
rejected_specs[dependency] ||= \
|
|
|
|
Gem::PlatformMismatch.new(tup.name, tup.version))
|
|
|
|
pm.add_platform tup.platform
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
errors += rejected_specs.values
|
2010-04-22 04:24:42 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
tuples = []
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
found.each do |source, specs|
|
|
|
|
specs.each do |s|
|
|
|
|
tuples << [s, source]
|
|
|
|
end
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
tuples = tuples.sort_by { |x| x[0] }
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
return [tuples, errors]
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Return all gem name tuples who's names match +obj+
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def detect(type=:complete)
|
|
|
|
tuples = []
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
list, _ = available_specs(type)
|
|
|
|
list.each do |source, specs|
|
|
|
|
specs.each do |tup|
|
|
|
|
if yield(tup)
|
|
|
|
tuples << [tup, source]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
tuples
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Find and fetch specs that match +dependency+.
|
|
|
|
#
|
|
|
|
# If +matching_platform+ is false, gems for all platforms are returned.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def spec_for_dependency(dependency, matching_platform=true)
|
|
|
|
tuples, errors = search_for_dependency(dependency, matching_platform)
|
2010-04-22 04:24:42 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
specs = []
|
|
|
|
tuples.each do |tup, source|
|
|
|
|
begin
|
|
|
|
spec = source.fetch_spec(tup)
|
|
|
|
rescue Gem::RemoteFetcher::FetchError => e
|
|
|
|
errors << Gem::SourceFetchProblem.new(source, e)
|
|
|
|
else
|
|
|
|
specs << [spec, source]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
return [specs, errors]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Suggests gems based on the supplied +gem_name+. Returns an array of
|
|
|
|
# alternative gem names.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def suggest_gems_from_name(gem_name, type = :latest)
|
2012-11-29 01:52:18 -05:00
|
|
|
gem_name = gem_name.downcase.tr('_-', '')
|
2011-01-18 19:08:49 -05:00
|
|
|
max = gem_name.size / 2
|
2017-10-07 21:32:18 -04:00
|
|
|
names = available_specs(type).first.values.flatten(1)
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
matches = names.map { |n|
|
|
|
|
next unless n.match_platform?
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
distance = levenshtein_distance gem_name, n.name.downcase.tr('_-', '')
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
next if distance >= max
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
return [n.name] if distance == 0
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
[n.name, distance]
|
2011-01-18 19:08:49 -05:00
|
|
|
}.compact
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
matches = if matches.empty? && type != :prerelease
|
|
|
|
suggest_gems_from_name gem_name, :prerelease
|
|
|
|
else
|
|
|
|
matches.uniq.sort_by { |name, dist| dist }
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
matches.first(5).map { |name, dist| name }
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
# Returns a list of gems available for each source in Gem::sources.
|
|
|
|
#
|
|
|
|
# +type+ can be one of 3 values:
|
|
|
|
# :released => Return the list of all released specs
|
|
|
|
# :complete => Return the list of all specs
|
|
|
|
# :latest => Return the list of only the highest version of each gem
|
|
|
|
# :prerelease => Return the list of all prerelease only specs
|
|
|
|
#
|
|
|
|
|
|
|
|
def available_specs(type)
|
|
|
|
errors = []
|
|
|
|
list = {}
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
@sources.each_source do |source|
|
2012-04-17 20:04:12 -04:00
|
|
|
begin
|
2012-11-29 01:52:18 -05:00
|
|
|
names = case type
|
|
|
|
when :latest
|
|
|
|
tuples_for source, :latest
|
|
|
|
when :released
|
|
|
|
tuples_for source, :released
|
|
|
|
when :complete
|
2013-09-14 04:59:02 -04:00
|
|
|
names =
|
|
|
|
tuples_for(source, :prerelease, true) +
|
2012-11-29 22:28:04 -05:00
|
|
|
tuples_for(source, :released)
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
names.sort
|
|
|
|
when :abs_latest
|
|
|
|
names =
|
|
|
|
tuples_for(source, :prerelease, true) +
|
|
|
|
tuples_for(source, :latest)
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
names.sort
|
2012-11-29 01:52:18 -05:00
|
|
|
when :prerelease
|
|
|
|
tuples_for(source, :prerelease)
|
|
|
|
else
|
|
|
|
raise Gem::Exception, "Unknown type - :#{type}"
|
|
|
|
end
|
2012-04-17 20:04:12 -04:00
|
|
|
rescue Gem::RemoteFetcher::FetchError => e
|
2012-11-29 01:52:18 -05:00
|
|
|
errors << Gem::SourceFetchProblem.new(source, e)
|
|
|
|
else
|
|
|
|
list[source] = names
|
2012-04-17 20:04:12 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
[list, errors]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# Retrieves NameTuples from +source+ of the given +type+ (:prerelease,
|
|
|
|
# etc.). If +gracefully_ignore+ is true, errors are ignored.
|
|
|
|
|
|
|
|
def tuples_for(source, type, gracefully_ignore=false) # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
@caches[type][source.uri] ||=
|
|
|
|
source.load_specs(type).sort_by { |tup| tup.name }
|
|
|
|
rescue Gem::RemoteFetcher::FetchError
|
|
|
|
raise unless gracefully_ignore
|
|
|
|
[]
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-02-28 17:25:55 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|