38
30
attr_accessor :spec_dirs
41
include Gem::UserInteraction
44
# Factory method to construct a source index instance for a given
48
# If supplied, from_installed_gems will act just like
49
# +from_gems_in+. This argument is deprecated and is provided
50
# just for backwards compatibility, and should not generally
54
# SourceIndex instance
56
def from_installed_gems(*deprecated)
58
from_gems_in(*installed_spec_directories)
60
from_gems_in(*deprecated) # HACK warn
65
# Returns a list of directories from Gem.path that contain specifications.
67
def installed_spec_directories
68
Gem.path.collect { |dir| File.join(dir, "specifications") }
72
# Creates a new SourceIndex from the ruby format gem specifications in
75
def from_gems_in(*spec_dirs)
77
source_index.spec_dirs = spec_dirs
82
# Loads a ruby-format specification from +file_name+ and returns the
85
def load_specification(file_name)
86
return nil unless file_name and File.exist? file_name
88
spec_code = if defined? Encoding then
89
File.read file_name, :encoding => 'UTF-8'
95
gemspec = eval spec_code, binding, file_name
97
if gemspec.is_a?(Gem::Specification)
98
gemspec.loaded_from = file_name
101
alert_warning "File '#{file_name}' does not evaluate to a gem specification"
102
rescue SignalException, SystemExit
104
rescue SyntaxError => 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}'"
33
# Factory method to construct a source index instance for a given
37
# If supplied, from_installed_gems will act just like
38
# +from_gems_in+. This argument is deprecated and is provided
39
# just for backwards compatibility, and should not generally
43
# SourceIndex instance
45
def self.from_installed_gems(*deprecated)
47
from_gems_in(*installed_spec_directories)
49
warn "NOTE: from_installed_gems(arg) is deprecated. From #{caller.first}"
50
from_gems_in(*deprecated) # HACK warn
55
# Returns a list of directories from Gem.path that contain specifications.
57
def self.installed_spec_directories
58
# TODO: move to Gem::Utils
59
Gem.path.collect { |dir| File.join(dir, "specifications") }
63
# Creates a new SourceIndex from the ruby format gem specifications in
66
def self.from_gems_in(*spec_dirs)
71
# Loads a ruby-format specification from +file_name+ and returns the
74
def self.load_specification(file_name)
75
Deprecate.skip_during do
76
Gem::Specification.load Gem::Path.new(file_name)
118
81
# Constructs a source index instance from the provided specifications, which
119
82
# is a Hash of gem full names and Gem::Specifications.
121
# TODO merge @gems and @prerelease_gems and provide a separate method
124
def initialize(specifications={})
84
def initialize specs_or_dirs = []
126
specifications.each{ |full_name, spec| add_spec spec }
90
specs_or_dirs.each do |full_name, spec|
93
when Array, String then
94
self.spec_dirs = Array(specs_or_dirs)
97
arg = specs_or_dirs.inspect
98
warn "NOTE: SourceIndex.new(#{arg}) is deprecated; From #{caller.first}."
130
# TODO: remove method
135
106
def prerelease_gems
136
@gems.reject{ |name, gem| !gem.version.prerelease? }
107
@gems.reject { |name, gem| !gem.version.prerelease? }
139
110
def released_gems
140
@gems.reject{ |name, gem| gem.version.prerelease? }
111
@gems.reject { |name, gem| gem.version.prerelease? }
312
289
/^#{Regexp.escape gem_pattern.name}$/
315
version_requirement = platform_only || Gem::Requirement.default
292
requirement = platform_or_requirement || Gem::Requirement.default
316
293
gem_pattern = /#{gem_pattern}/i
319
unless Gem::Requirement === version_requirement then
320
version_requirement = Gem::Requirement.create version_requirement
296
unless Gem::Requirement === requirement then
297
requirement = Gem::Requirement.create requirement
323
specs = all_gems.values.select do |spec|
300
specs = @gems.values.select do |spec|
324
301
spec.name =~ gem_pattern and
325
version_requirement.satisfied_by? spec.version
302
requirement.satisfied_by? spec.version
328
305
if only_platform then
354
331
latest_specs.each do |local|
355
332
dependency = Gem::Dependency.new local.name, ">= #{local.version}"
358
fetcher = Gem::SpecFetcher.fetcher
359
remotes = fetcher.find_matching dependency
360
remotes = remotes.map { |(name, version,_),_| version }
361
rescue Gem::RemoteFetcher::FetchError => e
362
raise unless fetcher.warn_legacy e do
363
require 'rubygems/source_info_cache'
365
specs = Gem::SourceInfoCache.search_with_source dependency, true
367
remotes = specs.map { |spec,| spec.version }
334
fetcher = Gem::SpecFetcher.fetcher
335
remotes = fetcher.find_matching dependency
336
remotes = remotes.map { |(_, version, _), _| version }
371
338
latest = remotes.sort.last
380
# Updates this SourceIndex from +source_uri+. If +all+ is false, only the
381
# latest gems are fetched.
383
def update(source_uri, all)
384
source_uri = URI.parse source_uri unless URI::Generic === source_uri
385
source_uri.path += '/' unless source_uri.path =~ /\/$/
387
use_incremental = false
390
gem_names = fetch_quick_index source_uri, all
391
remove_extra gem_names
392
missing_gems = find_missing gem_names
394
return false if missing_gems.size.zero?
396
say "Missing metadata for #{missing_gems.size} gems" if
397
missing_gems.size > 0 and Gem.configuration.really_verbose
399
use_incremental = missing_gems.size <= Gem.configuration.bulk_threshold
400
rescue Gem::OperationNotSupportedError => ex
401
alert_error "Falling back to bulk fetch: #{ex.message}" if
402
Gem.configuration.really_verbose
403
use_incremental = false
406
if use_incremental then
407
update_with_missing(source_uri, missing_gems)
409
new_index = fetch_bulk_index(source_uri)
410
@gems.replace(new_index.gems)
416
346
def ==(other) # :nodoc:
417
347
self.class === other and @gems == other.gems
421
351
Marshal.dump(self)
427
require 'rubygems/remote_fetcher'
429
Gem::RemoteFetcher.fetcher
432
def fetch_index_from(source_uri)
436
Marshal.#{Gem.marshal_version}.Z
437
Marshal.#{Gem.marshal_version}
442
indexes.each do |name|
444
index = source_uri + name
446
spec_data = fetcher.fetch_path index
447
spec_data = unzip(spec_data) if name =~ /\.Z$/
449
if name =~ /Marshal/ then
450
return Marshal.load(spec_data)
452
return YAML.load(spec_data)
455
if Gem.configuration.really_verbose then
456
alert_error "Unable to fetch #{name}: #{e.message}"
466
def fetch_bulk_index(source_uri)
467
say "Bulk updating Gem source index for: #{source_uri}" if
468
Gem.configuration.verbose
470
index = fetch_index_from(source_uri)
472
raise Gem::RemoteSourceException,
473
"Error fetching remote gem cache: #{@fetch_error}"
480
# Get the quick index needed for incremental updates.
482
def fetch_quick_index(source_uri, all)
483
index = all ? 'index' : 'latest_index'
485
zipped_index = fetcher.fetch_path source_uri + "quick/#{index}.rz"
487
unzip(zipped_index).split("\n")
488
rescue ::Exception => e
490
say "Latest index not found, using quick index" if
491
Gem.configuration.really_verbose
493
fetch_quick_index source_uri, true
495
raise Gem::OperationNotSupportedError,
496
"No quick index found: #{e.message}"
501
# Make a list of full names for all the missing gemspecs.
503
def find_missing(spec_names)
504
unless defined? @originals then
506
each do |full_name, spec|
507
@originals[spec.original_name] = spec
511
spec_names.find_all { |full_name|
512
@originals[full_name].nil?
516
def remove_extra(spec_names)
517
dictionary = spec_names.inject({}) { |h, k| h[k] = true; h }
519
remove_spec name unless dictionary.include? spec.original_name
524
# Unzip the given string.
532
# Tries to fetch Marshal representation first, then YAML
534
def fetch_single_spec(source_uri, spec_name)
538
marshal_uri = source_uri + "quick/Marshal.#{Gem.marshal_version}/#{spec_name}.gemspec.rz"
539
zipped = fetcher.fetch_path marshal_uri
540
return Marshal.load(unzip(zipped))
544
if Gem.configuration.really_verbose then
545
say "unable to fetch marshal gemspec #{marshal_uri}: #{ex.class} - #{ex}"
550
yaml_uri = source_uri + "quick/#{spec_name}.gemspec.rz"
551
zipped = fetcher.fetch_path yaml_uri
552
return YAML.load(unzip(zipped))
555
if Gem.configuration.really_verbose then
556
say "unable to fetch YAML gemspec #{yaml_uri}: #{ex.class} - #{ex}"
564
# Update the cached source index with the missing names.
566
def update_with_missing(source_uri, missing_names)
567
progress = ui.progress_reporter(missing_names.size,
568
"Updating metadata for #{missing_names.size} gems from #{source_uri}")
569
missing_names.each do |spec_name|
570
gemspec = fetch_single_spec(source_uri, spec_name)
572
ui.say "Failed to download spec #{spec_name} from #{source_uri}:\n" \
573
"\t#{@fetch_error.message}"
576
progress.updated spec_name
593
362
Cache = SourceIndex
366
class Gem::SourceIndex
369
deprecate :all_gems, :none, 2011, 10
371
deprecate :==, :none, 2011, 11 # noisy
372
deprecate :add_specs, :none, 2011, 11 # noisy
373
deprecate :each, :none, 2011, 11
374
deprecate :gems, :none, 2011, 11
375
deprecate :load_gems_in, :none, 2011, 11
376
deprecate :refresh!, :none, 2011, 11
377
deprecate :spec_dirs=, "Specification.dirs=", 2011, 11 # noisy
378
deprecate :add_spec, "Specification.add_spec", 2011, 11
379
deprecate :find_name, "Specification.find_by_name", 2011, 11
380
deprecate :gem_signature, :none, 2011, 11
381
deprecate :index_signature, :none, 2011, 11
382
deprecate :initialize, :none, 2011, 11
383
deprecate :latest_specs, "Specification.latest_specs", 2011, 11
384
deprecate :length, "Specification.all.length", 2011, 11
385
deprecate :outdated, :none, 2011, 11
386
deprecate :prerelease_gems, :none, 2011, 11
387
deprecate :prerelease_specs, :none, 2011, 11
388
deprecate :released_gems, :none, 2011, 11
389
deprecate :released_specs, :none, 2011, 11
390
deprecate :remove_spec, "Specification.remove_spec", 2011, 11
391
deprecate :search, :none, 2011, 11
392
deprecate :size, "Specification.all.size", 2011, 11
393
deprecate :spec_dirs, "Specification.dirs", 2011, 11
394
deprecate :specification, "Specification.find", 2011, 11
399
deprecate :from_gems_in, :none, 2011, 10
400
deprecate :from_installed_gems, :none, 2011, 10
401
deprecate :installed_spec_directories, "Specification.dirs", 2011, 11
402
deprecate :load_specification, :none, 2011, 10