Class: Gem::SourceIndex
- Inherits:
-
Object
- Object
- Gem::SourceIndex
- Extended by:
- Deprecate
- Includes:
- Enumerable
- Defined in:
- lib/rubygems/source_index.rb,
lib/rubygems/source_index.rb
Overview
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.
Instance Attribute Summary collapse
-
#gems ⇒ Object
readonly
:nodoc:.
-
#spec_dirs ⇒ Object
Directories to use to refresh this SourceIndex when calling refresh!.
Class Method Summary collapse
-
.from_gems_in(*spec_dirs) ⇒ Object
Creates a new SourceIndex from the ruby format gem specifications in
spec_dirs
. -
.from_installed_gems(*deprecated) ⇒ Object
Factory method to construct a source index instance for a given path.
-
.installed_spec_directories ⇒ Object
Returns a list of directories from Gem.path that contain specifications.
-
.load_specification(file_name) ⇒ Object
Loads a ruby-format specification from
file_name
and returns the loaded spec.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#add_spec(gem_spec, name = gem_spec.full_name) ⇒ Object
Add a gem specification to the source index.
-
#add_specs(*gem_specs) ⇒ Object
Add gem specifications to the source index.
- #all_gems ⇒ Object
- #dump ⇒ Object
-
#each(&block) ⇒ Object
Iterate over the specifications in the source index.
-
#find_name(gem_name, requirement = Gem::Requirement.default) ⇒ Object
Find a gem by an exact match on the short name.
-
#gem_signature(gem_full_name) ⇒ Object
The signature for the given gem specification.
-
#index_signature ⇒ Object
The signature for the source index.
-
#initialize(specs_or_dirs = []) ⇒ SourceIndex
constructor
Constructs a source index instance from the provided specifications, which is a Hash of gem full names and Gem::Specifications.
-
#latest_specs(include_prerelease = false) ⇒ Object
Returns an Array specifications for the latest released versions of each gem in this index.
-
#load_gems_in(*spec_dirs) ⇒ Object
Reconstruct the source index from the specifications in
spec_dirs
. -
#outdated ⇒ Object
Returns an Array of Gem::Specifications that are not up to date.
- #prerelease_gems ⇒ Object
-
#prerelease_specs ⇒ Object
An array including only the prerelease gemspecs.
-
#refresh! ⇒ Object
Replaces the gems in the source index from specifications in the directories this source index was created from.
- #released_gems ⇒ Object
-
#released_specs ⇒ Object
An array including only the released gemspecs.
-
#remove_spec(full_name) ⇒ Object
Remove a gem specification named
full_name
. -
#search(gem_pattern, platform_or_requirement = false) ⇒ Object
Search for a gem by Gem::Dependency
gem_pattern
. - #size ⇒ Object (also: #length)
-
#specification(full_name) ⇒ Object
The gem specification given a full gem spec name.
Methods included from Deprecate
deprecate, skip, skip=, skip_during
Constructor Details
#initialize(specs_or_dirs = []) ⇒ SourceIndex
Constructs a source index instance from the provided specifications, which is a Hash of gem full names and Gem::Specifications.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rubygems/source_index.rb', line 84 def initialize specs_or_dirs = [] @gems = {} @spec_dirs = nil case specs_or_dirs when Hash then specs_or_dirs.each do |full_name, spec| add_spec spec end when Array, String then self.spec_dirs = Array(specs_or_dirs) refresh! else arg = specs_or_dirs.inspect warn "NOTE: SourceIndex.new(#{arg}) is deprecated; From #{caller.first}." end end |
Instance Attribute Details
#gems ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/rubygems/source_index.rb', line 25 def gems @gems end |
#spec_dirs ⇒ Object
Directories to use to refresh this SourceIndex when calling refresh!
30 31 32 |
# File 'lib/rubygems/source_index.rb', line 30 def spec_dirs @spec_dirs end |
Class Method Details
.from_gems_in(*spec_dirs) ⇒ Object
Creates a new SourceIndex from the ruby format gem specifications in spec_dirs
.
66 67 68 |
# File 'lib/rubygems/source_index.rb', line 66 def self.from_gems_in(*spec_dirs) new spec_dirs end |
.from_installed_gems(*deprecated) ⇒ Object
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
45 46 47 48 49 50 51 52 |
# File 'lib/rubygems/source_index.rb', line 45 def self.from_installed_gems(*deprecated) if deprecated.empty? from_gems_in(*installed_spec_directories) else warn "NOTE: from_installed_gems(arg) is deprecated. From #{caller.first}" from_gems_in(*deprecated) # HACK warn end end |
.installed_spec_directories ⇒ Object
Returns a list of directories from Gem.path that contain specifications.
57 58 59 60 |
# File 'lib/rubygems/source_index.rb', line 57 def self.installed_spec_directories # TODO: move to Gem::Utils Gem.path.collect { |dir| File.join(dir, "specifications") } end |
.load_specification(file_name) ⇒ Object
Loads a ruby-format specification from file_name
and returns the loaded spec.
74 75 76 77 78 |
# File 'lib/rubygems/source_index.rb', line 74 def self.load_specification(file_name) Gem::Deprecate.skip_during do Gem::Specification.load Gem::Path.new(file_name) end end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
346 347 348 |
# File 'lib/rubygems/source_index.rb', line 346 def ==(other) # :nodoc: self.class === other and @gems == other.gems end |
#add_spec(gem_spec, name = gem_spec.full_name) ⇒ Object
Add a gem specification to the source index.
186 187 188 189 190 |
# File 'lib/rubygems/source_index.rb', line 186 def add_spec(gem_spec, name = gem_spec.full_name) # No idea why, but the Indexer wants to insert them using original_name # instead of full_name. So we make it an optional arg. @gems[name] = gem_spec end |
#add_specs(*gem_specs) ⇒ Object
Add gem specifications to the source index.
195 196 197 198 199 200 201 |
# File 'lib/rubygems/source_index.rb', line 195 def add_specs(*gem_specs) Gem::Deprecate.skip_during do gem_specs.each do |spec| add_spec spec end end end |
#all_gems ⇒ Object
102 103 104 |
# File 'lib/rubygems/source_index.rb', line 102 def all_gems gems end |
#dump ⇒ Object
350 351 352 |
# File 'lib/rubygems/source_index.rb', line 350 def dump Marshal.dump(self) end |
#each(&block) ⇒ Object
Iterate over the specifications in the source index.
213 214 215 |
# File 'lib/rubygems/source_index.rb', line 213 def each(&block) # :yields: gem.full_name, gem @gems.each(&block) end |
#find_name(gem_name, requirement = Gem::Requirement.default) ⇒ Object
Find a gem by an exact match on the short name.
251 252 253 254 255 256 257 |
# File 'lib/rubygems/source_index.rb', line 251 def find_name(gem_name, requirement = Gem::Requirement.default) dep = Gem::Dependency.new gem_name, requirement Gem::Deprecate.skip_during do search dep end end |
#gem_signature(gem_full_name) ⇒ Object
The signature for the given gem specification.
237 238 239 240 241 |
# File 'lib/rubygems/source_index.rb', line 237 def gem_signature(gem_full_name) require 'digest' Digest::SHA256.new.hexdigest(@gems[gem_full_name].to_yaml).to_s end |
#index_signature ⇒ Object
The signature for the source index. Changes in the signature indicate a change in the index.
228 229 230 231 232 |
# File 'lib/rubygems/source_index.rb', line 228 def index_signature require 'digest' Digest::SHA256.new.hexdigest(@gems.keys.sort.join(',')).to_s end |
#latest_specs(include_prerelease = false) ⇒ Object
Returns an Array specifications for the latest released versions of each gem in this index.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rubygems/source_index.rb', line 138 def latest_specs(include_prerelease=false) result = Hash.new { |h,k| h[k] = [] } latest = {} sort.each do |_, spec| name = spec.name curr_ver = spec.version prev_ver = latest.key?(name) ? latest[name].version : nil next if !include_prerelease && curr_ver.prerelease? next unless prev_ver.nil? or curr_ver >= prev_ver or latest[name].platform != Gem::Platform::RUBY if prev_ver.nil? or (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then result[name].clear latest[name] = spec end if spec.platform != Gem::Platform::RUBY then result[name].delete_if do |result_spec| result_spec.platform == spec.platform end end result[name] << spec end result.values.flatten end |
#load_gems_in(*spec_dirs) ⇒ Object
Reconstruct the source index from the specifications in spec_dirs
.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rubygems/source_index.rb', line 117 def load_gems_in(*spec_dirs) @gems.clear spec_dirs.reverse_each do |spec_dir| spec_files = Dir[File.join(spec_dir, "*.gemspec")] spec_files.each do |spec_file| gemspec = Gem::Deprecate.skip_during do Gem::Specification.load spec_file end add_spec gemspec if gemspec end end self end |
#outdated ⇒ Object
Returns an Array of Gem::Specifications that are not up to date.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rubygems/source_index.rb', line 328 def outdated outdateds = [] latest_specs.each do |local| dependency = Gem::Dependency.new local.name, ">= #{local.version}" fetcher = Gem::SpecFetcher.fetcher remotes = fetcher.find_matching dependency remotes = remotes.map { |(_, version, _), _| version } latest = remotes.sort.last outdateds << local.name if latest and local.version < latest end outdateds end |
#prerelease_gems ⇒ Object
106 107 108 |
# File 'lib/rubygems/source_index.rb', line 106 def prerelease_gems @gems.reject { |name, gem| !gem.version.prerelease? } end |
#prerelease_specs ⇒ Object
An array including only the prerelease gemspecs
172 173 174 |
# File 'lib/rubygems/source_index.rb', line 172 def prerelease_specs prerelease_gems.values end |
#refresh! ⇒ Object
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).
320 321 322 323 |
# File 'lib/rubygems/source_index.rb', line 320 def refresh! raise 'source index not created from disk' if @spec_dirs.nil? load_gems_in(*@spec_dirs) end |
#released_gems ⇒ Object
110 111 112 |
# File 'lib/rubygems/source_index.rb', line 110 def released_gems @gems.reject { |name, gem| gem.version.prerelease? } end |
#released_specs ⇒ Object
An array including only the released gemspecs
179 180 181 |
# File 'lib/rubygems/source_index.rb', line 179 def released_specs released_gems.values end |
#remove_spec(full_name) ⇒ Object
Remove a gem specification named full_name
.
206 207 208 |
# File 'lib/rubygems/source_index.rb', line 206 def remove_spec(full_name) @gems.delete full_name end |
#search(gem_pattern, platform_or_requirement = false) ⇒ Object
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.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/rubygems/source_index.rb', line 268 def search(gem_pattern, platform_or_requirement = false) requirement = nil only_platform = false # FIX: WTF is this?!? # TODO - Remove support and warning for legacy arguments after 2008/11 unless Gem::Dependency === gem_pattern warn "#{Gem.location_of_caller.join ':'}:Warning: Gem::SourceIndex#search support for #{gem_pattern.class} patterns is deprecated, use #find_name" end case gem_pattern when Regexp then requirement = platform_or_requirement || Gem::Requirement.default when Gem::Dependency then only_platform = platform_or_requirement requirement = gem_pattern.requirement gem_pattern = if Regexp === gem_pattern.name then gem_pattern.name elsif gem_pattern.name.empty? then // else /^#{Regexp.escape gem_pattern.name}$/ end else requirement = platform_or_requirement || Gem::Requirement.default gem_pattern = /#{gem_pattern}/i end unless Gem::Requirement === requirement then requirement = Gem::Requirement.create requirement end specs = @gems.values.select do |spec| spec.name =~ gem_pattern and requirement.satisfied_by? spec.version end if only_platform then specs = specs.select do |spec| Gem::Platform.match spec.platform end end specs.sort_by { |s| s.sort_obj } end |
#size ⇒ Object Also known as: length
243 244 245 |
# File 'lib/rubygems/source_index.rb', line 243 def size @gems.size end |
#specification(full_name) ⇒ Object
The gem specification given a full gem spec name.
220 221 222 |
# File 'lib/rubygems/source_index.rb', line 220 def specification(full_name) @gems[full_name] end |