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