| Class | Gem::SourceIndex |
| In: |
lib/rubygems/source_index.rb
|
| Parent: | Object |
The SourceIndex object indexes all the gems available from a particular source (e.g. a list of gem directories, or a remote source). A SourceIndex maps a gem full name to a gem specification.
| NOTE: | The class used to be named Cache, but that became confusing when cached source fetchers where introduced. The constant Gem::Cache is an alias for this class to allow old YAMLized source index objects to load properly. |
| spec_dirs | [RW] | Directories to use to refresh this SourceIndex when calling refresh! |
Creates a new SourceIndex from the ruby format gem specifications in spec_dirs.
# File lib/rubygems/source_index.rb, line 75
75: def from_gems_in(*spec_dirs)
76: source_index = new
77: source_index.spec_dirs = spec_dirs
78: source_index.refresh!
79: end
Factory method to construct a source index instance for a given path.
| deprecated: | If supplied, from_installed_gems will act just like from_gems_in. This argument is deprecated and is provided just for backwards compatibility, and should not generally be used. |
| return: | SourceIndex instance |
# File lib/rubygems/source_index.rb, line 56
56: def from_installed_gems(*deprecated)
57: if deprecated.empty?
58: from_gems_in(*installed_spec_directories)
59: else
60: from_gems_in(*deprecated) # HACK warn
61: end
62: end
Loads a ruby-format specification from file_name and returns the loaded spec.
# File lib/rubygems/source_index.rb, line 85
85: def load_specification(file_name)
86: return nil unless file_name and File.exist? file_name
87:
88: spec_code = if RUBY_VERSION < '1.9' then
89: File.read file_name
90: else
91: File.read file_name, :encoding => 'UTF-8'
92: end.untaint
93:
94: begin
95: gemspec = eval spec_code, binding, file_name
96:
97: if gemspec.is_a?(Gem::Specification)
98: gemspec.loaded_from = file_name
99: return gemspec
100: end
101: alert_warning "File '#{file_name}' does not evaluate to a gem specification"
102: rescue SignalException, SystemExit
103: raise
104: rescue SyntaxError => e
105: alert_warning e
106: alert_warning spec_code
107: rescue Exception => e
108: alert_warning "#{e.inspect}\n#{spec_code}"
109: alert_warning "Invalid .gemspec format in '#{file_name}'"
110: end
111:
112: return nil
113: end
Constructs a source index instance from the provided specifications, which is a Hash of gem full names and Gem::Specifications.
# File lib/rubygems/source_index.rb, line 124
124: def initialize(specifications={})
125: @gems, @prerelease_gems = [{}, {}]
126: specifications.each{ |full_name, spec| add_spec spec }
127: @spec_dirs = nil
128: end
Add a gem specification to the source index.
# File lib/rubygems/source_index.rb, line 201
201: def add_spec(gem_spec, name = gem_spec.full_name)
202: # No idea why, but the Indexer wants to insert them using original_name
203: # instead of full_name. So we make it an optional arg.
204: if gem_spec.version.prerelease?
205: @prerelease_gems[name] = gem_spec
206: else
207: @gems[name] = gem_spec
208: end
209: end
Add gem specifications to the source index.
# File lib/rubygems/source_index.rb, line 214
214: def add_specs(*gem_specs)
215: gem_specs.each do |spec|
216: add_spec spec
217: end
218: end
Both regular and prerelease gems
# File lib/rubygems/source_index.rb, line 133
133: def all_gems
134: @gems.merge @prerelease_gems
135: end
Iterate over the specifications in the source index.
# File lib/rubygems/source_index.rb, line 234
234: def each(&block) # :yields: gem.full_name, gem
235: @gems.each(&block)
236: end
Find a gem by an exact match on the short name.
# File lib/rubygems/source_index.rb, line 272
272: def find_name(gem_name, version_requirement = Gem::Requirement.default)
273: dep = Gem::Dependency.new gem_name, version_requirement
274: search dep
275: end
The signature for the given gem specification.
# File lib/rubygems/source_index.rb, line 258
258: def gem_signature(gem_full_name)
259: require 'rubygems/digest/sha2'
260:
261: Gem::SHA256.new.hexdigest(@gems[gem_full_name].to_yaml).to_s
262: end
The signature for the source index. Changes in the signature indicate a change in the index.
# File lib/rubygems/source_index.rb, line 249
249: def index_signature
250: require 'rubygems/digest/sha2'
251:
252: Gem::SHA256.new.hexdigest(@gems.keys.sort.join(',')).to_s
253: end
Returns an Array specifications for the latest versions of each gem in this index.
# File lib/rubygems/source_index.rb, line 159
159: def latest_specs
160: result = Hash.new { |h,k| h[k] = [] }
161: latest = {}
162:
163: sort.each do |_, spec|
164: name = spec.name
165: curr_ver = spec.version
166: prev_ver = latest.key?(name) ? latest[name].version : nil
167:
168: next unless prev_ver.nil? or curr_ver >= prev_ver or
169: latest[name].platform != Gem::Platform::RUBY
170:
171: if prev_ver.nil? or
172: (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then
173: result[name].clear
174: latest[name] = spec
175: end
176:
177: if spec.platform != Gem::Platform::RUBY then
178: result[name].delete_if do |result_spec|
179: result_spec.platform == spec.platform
180: end
181: end
182:
183: result[name] << spec
184: end
185:
186: # TODO: why is this a hash while @gems is an array? Seems like
187: # structural similarity would be good.
188: result.values.flatten
189: end
Reconstruct the source index from the specifications in spec_dirs.
# File lib/rubygems/source_index.rb, line 140
140: def load_gems_in(*spec_dirs)
141: @gems.clear
142:
143: spec_dirs.reverse_each do |spec_dir|
144: spec_files = Dir.glob File.join(spec_dir, '*.gemspec')
145:
146: spec_files.each do |spec_file|
147: gemspec = self.class.load_specification spec_file.untaint
148: add_spec gemspec if gemspec
149: end
150: end
151:
152: self
153: end
Returns an Array of Gem::Specifications that are not up to date.
# File lib/rubygems/source_index.rb, line 345
345: def outdated
346: outdateds = []
347:
348: latest_specs.each do |local|
349: dependency = Gem::Dependency.new local.name, ">= #{local.version}"
350:
351: begin
352: fetcher = Gem::SpecFetcher.fetcher
353: remotes = fetcher.find_matching dependency
354: remotes = remotes.map { |(name, version,_),_| version }
355: rescue Gem::RemoteFetcher::FetchError => e
356: raise unless fetcher.warn_legacy e do
357: require 'rubygems/source_info_cache'
358:
359: specs = Gem::SourceInfoCache.search_with_source dependency, true
360:
361: remotes = specs.map { |spec,| spec.version }
362: end
363: end
364:
365: latest = remotes.sort.last
366:
367: outdateds << local.name if latest and local.version < latest
368: end
369:
370: outdateds
371: end
An array including only the prerelease gemspecs
# File lib/rubygems/source_index.rb, line 194
194: def prerelease_specs
195: @prerelease_gems.values
196: end
Replaces the gems in the source index from specifications in the directories this source index was created from. Raises an exception if this source index wasn‘t created from a directory (via from_gems_in or from_installed_gems, or having spec_dirs set).
# File lib/rubygems/source_index.rb, line 337
337: def refresh!
338: raise 'source index not created from disk' if @spec_dirs.nil?
339: load_gems_in(*@spec_dirs)
340: end
Remove a gem specification named full_name.
# File lib/rubygems/source_index.rb, line 223
223: def remove_spec(full_name)
224: if @gems.key? full_name then
225: @gems.delete full_name
226: else
227: @prerelease_gems.delete full_name
228: end
229: end
Search for a gem by Gem::Dependency gem_pattern. If only_platform is true, only gems matching Gem::Platform.local will be returned. An Array of matching Gem::Specification objects is returned.
For backwards compatibility, a String or Regexp pattern may be passed as gem_pattern, and a Gem::Requirement for platform_only. This behavior is deprecated and will be removed.
# File lib/rubygems/source_index.rb, line 286
286: def search(gem_pattern, platform_only = false)
287: version_requirement = nil
288: only_platform = false
289:
290: # TODO - Remove support and warning for legacy arguments after 2008/11
291: unless Gem::Dependency === gem_pattern
292: warn "#{Gem.location_of_caller.join ':'}:Warning: Gem::SourceIndex#search support for #{gem_pattern.class} patterns is deprecated, use #find_name"
293: end
294:
295: case gem_pattern
296: when Regexp then
297: version_requirement = platform_only || Gem::Requirement.default
298: when Gem::Dependency then
299: only_platform = platform_only
300: version_requirement = gem_pattern.version_requirements
301: gem_pattern = if Regexp === gem_pattern.name then
302: gem_pattern.name
303: elsif gem_pattern.name.empty? then
304: //
305: else
306: /^#{Regexp.escape gem_pattern.name}$/
307: end
308: else
309: version_requirement = platform_only || Gem::Requirement.default
310: gem_pattern = /^#{gem_pattern}/i
311: end
312:
313: unless Gem::Requirement === version_requirement then
314: version_requirement = Gem::Requirement.create version_requirement
315: end
316:
317: specs = all_gems.values.select do |spec|
318: spec.name =~ gem_pattern and
319: version_requirement.satisfied_by? spec.version
320: end
321:
322: if only_platform then
323: specs = specs.select do |spec|
324: Gem::Platform.match spec.platform
325: end
326: end
327:
328: specs.sort_by { |s| s.sort_obj }
329: end
The gem specification given a full gem spec name.
# File lib/rubygems/source_index.rb, line 241
241: def specification(full_name)
242: @gems[full_name]
243: end
Updates this SourceIndex from source_uri. If all is false, only the latest gems are fetched.
# File lib/rubygems/source_index.rb, line 377
377: def update(source_uri, all)
378: source_uri = URI.parse source_uri unless URI::Generic === source_uri
379: source_uri.path += '/' unless source_uri.path =~ /\/$/
380:
381: use_incremental = false
382:
383: begin
384: gem_names = fetch_quick_index source_uri, all
385: remove_extra gem_names
386: missing_gems = find_missing gem_names
387:
388: return false if missing_gems.size.zero?
389:
390: say "Missing metadata for #{missing_gems.size} gems" if
391: missing_gems.size > 0 and Gem.configuration.really_verbose
392:
393: use_incremental = missing_gems.size <= Gem.configuration.bulk_threshold
394: rescue Gem::OperationNotSupportedError => ex
395: alert_error "Falling back to bulk fetch: #{ex.message}" if
396: Gem.configuration.really_verbose
397: use_incremental = false
398: end
399:
400: if use_incremental then
401: update_with_missing(source_uri, missing_gems)
402: else
403: new_index = fetch_bulk_index(source_uri)
404: @gems.replace(new_index.gems)
405: end
406:
407: true
408: end