2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-07-09 19:21:36 -04:00
|
|
|
##
|
|
|
|
# The global rubygems pool, available via the rubygems.org API.
|
|
|
|
# Returns instances of APISpecification.
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
class Gem::Resolver::APISet < Gem::Resolver::Set
|
2020-12-22 18:45:19 -05:00
|
|
|
autoload :GemParser, File.expand_path("api_set/gem_parser", __dir__)
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
|
|
|
# The URI for the dependency API this APISet uses.
|
|
|
|
|
|
|
|
attr_reader :dep_uri # :nodoc:
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# The Gem::Source that gems are fetched from
|
|
|
|
|
|
|
|
attr_reader :source
|
|
|
|
|
|
|
|
##
|
|
|
|
# The corresponding place to fetch gems.
|
|
|
|
|
|
|
|
attr_reader :uri
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
|
|
|
# Creates a new APISet that will retrieve gems from +uri+ using the RubyGems
|
2013-11-21 18:27:30 -05:00
|
|
|
# API URL +dep_uri+ which is described at
|
2020-01-31 21:14:04 -05:00
|
|
|
# https://guides.rubygems.org/rubygems-org-api
|
2013-11-21 18:27:30 -05:00
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
def initialize(dep_uri = "https://index.rubygems.org/info/")
|
2014-02-03 19:48:31 -05:00
|
|
|
super()
|
|
|
|
|
2020-03-26 13:43:34 -04:00
|
|
|
dep_uri = URI dep_uri unless URI === dep_uri
|
2013-11-10 12:51:40 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
@dep_uri = dep_uri
|
2020-12-22 18:45:19 -05:00
|
|
|
@uri = dep_uri + ".."
|
2013-11-21 18:27:30 -05:00
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
@data = Hash.new {|h,k| h[k] = [] }
|
2013-11-21 18:27:30 -05:00
|
|
|
@source = Gem::Source.new @uri
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
@to_fetch = []
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Return an array of APISpecification objects matching
|
|
|
|
# DependencyRequest +req+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def find_all(req)
|
2013-07-09 19:21:36 -04:00
|
|
|
res = []
|
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
return res unless @remote
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
if @to_fetch.include?(req.name)
|
|
|
|
prefetch_now
|
|
|
|
end
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
versions(req.name).each do |ver|
|
2020-12-14 18:32:54 -05:00
|
|
|
if req.dependency.match? req.name, ver[:number], @prerelease
|
2013-11-18 19:34:13 -05:00
|
|
|
res << Gem::Resolver::APISpecification.new(self, ver)
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A hint run by the resolver to allow the Set to fetch
|
|
|
|
# data for DependencyRequests +reqs+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def prefetch(reqs)
|
2014-02-03 19:48:31 -05:00
|
|
|
return unless @remote
|
2020-06-10 13:46:05 -04:00
|
|
|
names = reqs.map {|r| r.dependency.name }
|
2014-09-13 23:30:02 -04:00
|
|
|
needed = names - @data.keys - @to_fetch
|
|
|
|
|
|
|
|
@to_fetch += needed
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2014-12-06 19:53:01 -05:00
|
|
|
def prefetch_now # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
needed, @to_fetch = @to_fetch, []
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
needed.sort.each do |name|
|
|
|
|
versions(name)
|
2013-11-21 18:27:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def pretty_print(q) # :nodoc:
|
2013-11-21 18:27:30 -05:00
|
|
|
q.group 2, "[APISet", "]" do
|
|
|
|
q.breakable
|
|
|
|
q.text "URI: #{@dep_uri}"
|
|
|
|
|
|
|
|
q.breakable
|
|
|
|
q.text "gem names:"
|
|
|
|
q.pp @data.keys
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Return data for all versions of the gem +name+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def versions(name) # :nodoc:
|
2013-07-09 19:21:36 -04:00
|
|
|
if @data.key?(name)
|
|
|
|
return @data[name]
|
|
|
|
end
|
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
uri = @dep_uri + name
|
2013-07-09 19:21:36 -04:00
|
|
|
str = Gem::RemoteFetcher.fetcher.fetch_path uri
|
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
lines(str).each do |ver|
|
|
|
|
number, platform, dependencies, requirements = parse_gem(ver)
|
|
|
|
|
|
|
|
platform ||= "ruby"
|
|
|
|
dependencies = dependencies.map {|dep_name, reqs| [dep_name, reqs.join(", ")] }
|
|
|
|
requirements = requirements.map {|req_name, reqs| [req_name.to_sym, reqs] }.to_h
|
|
|
|
|
|
|
|
@data[name] << { name: name, number: number, platform: platform, dependencies: dependencies, requirements: requirements }
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@data[name]
|
|
|
|
end
|
2020-12-22 18:45:19 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def lines(str)
|
|
|
|
lines = str.split("\n")
|
|
|
|
header = lines.index("---")
|
|
|
|
header ? lines[header + 1..-1] : lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_gem(string)
|
|
|
|
@gem_parser ||= GemParser.new
|
|
|
|
@gem_parser.parse(string)
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|