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.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def initialize(sources = nil)
|
2013-11-10 12:51:40 -05:00
|
|
|
@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 = {}
|
|
|
|
|
2019-09-29 10:43:17 -04:00
|
|
|
list, errors = available_specs(dependency.identity)
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
# 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
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
def suggest_gems_from_name(gem_name, type = :latest, num_results = 5)
|
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
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
matches = names.map do |n|
|
2012-11-29 01:52:18 -05:00
|
|
|
next unless n.match_platform?
|
2020-03-24 02:39:24 -04:00
|
|
|
[n.name, 0] if n.name.downcase.tr('_-', '').include?(gem_name)
|
2019-02-14 07:59:03 -05:00
|
|
|
end.compact
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
if matches.length < num_results
|
|
|
|
matches += names.map do |n|
|
|
|
|
next unless n.match_platform?
|
|
|
|
distance = levenshtein_distance gem_name, n.name.downcase.tr('_-', '')
|
|
|
|
next if distance >= max
|
|
|
|
return [n.name] if distance == 0
|
|
|
|
[n.name, distance]
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
matches = if matches.empty? && type != :prerelease
|
2018-08-27 06:05:04 -04:00
|
|
|
suggest_gems_from_name gem_name, :prerelease
|
2018-10-21 20:27:02 -04:00
|
|
|
else
|
|
|
|
matches.uniq.sort_by { |name, dist| dist }
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
matches.map { |name, dist| name }.uniq.first(num_results)
|
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
|