22
23
attr_reader :latest_specs # :nodoc:
26
# Cache of all released specs
27
28
attr_reader :specs # :nodoc:
31
# Cache of prerelease specs
33
attr_reader :prerelease_specs # :nodoc:
51
@prerelease_specs = {}
46
53
@fetcher = Gem::RemoteFetcher.fetcher
50
# Retuns the local directory to write +uri+ to.
57
# Returns the local directory to write +uri+ to.
53
60
File.join @dir, "#{uri.host}%#{uri.port}", File.dirname(uri.path)
57
64
# Fetch specs matching +dependency+. If +all+ is true, all matching
58
# versions are returned. If +matching_platform+ is false, all platforms are
61
def fetch(dependency, all = false, matching_platform = true)
62
specs_and_sources = find_matching dependency, all, matching_platform
64
specs_and_sources.map do |spec_tuple, source_uri|
65
# (released) versions are returned. If +matching_platform+ is
66
# false, all platforms are returned. If +prerelease+ is true,
67
# prerelease versions are included.
69
def fetch_with_errors(dependency, all = false, matching_platform = true, prerelease = false)
70
specs_and_sources, errors = find_matching_with_errors dependency, all, matching_platform, prerelease
72
ss = specs_and_sources.map do |spec_tuple, source_uri|
65
73
[fetch_spec(spec_tuple, URI.parse(source_uri)), source_uri]
68
78
rescue Gem::RemoteFetcher::FetchError => e
69
79
raise unless warn_legacy e do
70
80
require 'rubygems/source_info_cache'
72
return Gem::SourceInfoCache.search_with_source(dependency,
73
matching_platform, all)
82
return [Gem::SourceInfoCache.search_with_source(dependency,
83
matching_platform, all), nil]
88
fetch_with_errors(*args).first
77
91
def fetch_spec(spec, source_uri)
78
92
spec = spec - [nil, 'ruby', '']
79
93
spec_file_name = "#{spec.join '-'}.gemspec"
109
# Find spec names that match +dependency+. If +all+ is true, all matching
110
# versions are returned. If +matching_platform+ is false, gems for all
111
# platforms are returned.
123
# Find spec names that match +dependency+. If +all+ is true, all
124
# matching released versions are returned. If +matching_platform+
125
# is false, gems for all platforms are returned.
113
def find_matching(dependency, all = false, matching_platform = true)
127
def find_matching_with_errors(dependency, all = false, matching_platform = true, prerelease = false)
116
list(all).each do |source_uri, specs|
132
list(all, prerelease).each do |source_uri, specs|
117
133
found[source_uri] = specs.select do |spec_name, version, spec_platform|
118
dependency =~ Gem::Dependency.new(spec_name, version) and
119
(not matching_platform or Gem::Platform.match(spec_platform))
134
if dependency.match?(spec_name, version)
135
if matching_platform and !Gem::Platform.match(spec_platform)
136
pm = (rejected_specs[dependency] ||= Gem::PlatformMismatch.new(spec_name, version))
137
pm.add_platform spec_platform
146
errors = rejected_specs.values
123
148
specs_and_sources = []
125
150
found.each do |source_uri, specs|
157
186
# Returns a list of gems available for each source in Gem::sources. If
158
# +all+ is true, all versions are returned instead of only latest versions.
160
def list(all = false)
187
# +all+ is true, all released versions are returned instead of only latest
188
# versions. If +prerelease+ is true, include prerelease versions.
190
def list(all = false, prerelease = false)
191
# TODO: make type the only argument
163
file = all ? 'specs' : 'latest_specs'
202
file = { :latest => 'latest_specs',
203
:prerelease => 'prerelease_specs',
204
:all => 'specs' }[type]
206
cache = { :latest => @latest_specs,
207
:prerelease => @prerelease_specs,
208
:all => @specs }[type]
165
210
Gem.sources.each do |source_uri|
166
211
source_uri = URI.parse source_uri
168
if all and @specs.include? source_uri then
169
list[source_uri] = @specs[source_uri]
170
elsif not all and @latest_specs.include? source_uri then
171
list[source_uri] = @latest_specs[source_uri]
173
specs = load_specs source_uri, file
175
cache = all ? @specs : @latest_specs
177
cache[source_uri] = specs
178
list[source_uri] = specs
213
unless cache.include? source_uri
214
cache[source_uri] = load_specs source_uri, file
217
list[source_uri] = cache[source_uri]
221
list.values.map do |gems|
222
gems.reject! { |g| !g[1] || g[1].prerelease? }
209
specs = Marshal.load spec_dump
254
Marshal.load spec_dump
256
spec_dump = @fetcher.fetch_path spec_path
259
Marshal.load spec_dump
211
262
if loaded and @update_cache then
213
264
FileUtils.mkdir_p cache_dir
215
266
open local_file, 'wb' do |io|
216
Marshal.dump specs, io