diff options
Diffstat (limited to 'lib/rubygems/source/local.rb')
| -rw-r--r-- | lib/rubygems/source/local.rb | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/lib/rubygems/source/local.rb b/lib/rubygems/source/local.rb index 3227fb61b0..4bef31a265 100644 --- a/lib/rubygems/source/local.rb +++ b/lib/rubygems/source/local.rb @@ -1,20 +1,21 @@ # frozen_string_literal: true + ## # The local source finds gems in the current directory for fulfilling # dependencies. class Gem::Source::Local < Gem::Source - def initialize # :nodoc: @specs = nil @api_uri = nil @uri = nil + @load_specs_names = {} end ## # Local sorts before Gem::Source and after Gem::Source::Installed - def <=> other + def <=>(other) case other when Gem::Source::Installed, Gem::Source::Lock then @@ -23,28 +24,27 @@ class Gem::Source::Local < Gem::Source 0 when Gem::Source then 1 - else - nil end end def inspect # :nodoc: - keys = @specs ? @specs.keys.sort : 'NOT LOADED' - "#<%s specs: %p>" % [self.class, keys] + keys = @specs ? @specs.keys.sort : "NOT LOADED" + format("#<%s specs: %p>", self.class, keys) end - def load_specs type # :nodoc: - names = [] + def load_specs(type) # :nodoc: + @load_specs_names[type] ||= begin + names = [] - @specs = {} + @specs = {} - Dir["*.gem"].each do |file| - begin + Dir["*.gem"].each do |file| pkg = Gem::Package.new(file) + spec = pkg.spec rescue SystemCallError, Gem::Package::FormatError # ignore else - tup = pkg.spec.name_tuple + tup = spec.name_tuple @specs[tup] = [File.expand_path(file), pkg] case type @@ -59,7 +59,7 @@ class Gem::Source::Local < Gem::Source when :latest tup = pkg.spec.name_tuple - cur = names.find { |x| x.name == tup.name } + cur = names.find {|x| x.name == tup.name } if !cur names << tup elsif cur.version < tup.version @@ -70,35 +70,37 @@ class Gem::Source::Local < Gem::Source names << pkg.spec.name_tuple end end + + names end + end - names + def find_gem(gem_name, version = Gem::Requirement.default, prerelease = false) # :nodoc: + find_all_gems(gem_name, version, prerelease).max_by(&:version) end - def find_gem gem_name, version = Gem::Requirement.default, # :nodoc: - prerelease = false + def find_all_gems(gem_name, version = Gem::Requirement.default, prerelease = false) # :nodoc: load_specs :complete found = [] @specs.each do |n, data| - if n.name == gem_name - s = data[1].spec - - if version.satisfied_by?(s.version) - if prerelease - found << s - elsif !s.version.prerelease? - found << s - end + next unless n.name == gem_name + s = data[1].spec + + if version.satisfied_by?(s.version) + if prerelease + found << s + elsif !s.version.prerelease? || version.prerelease? + found << s end end end - found.max_by { |s| s.version } + found end - def fetch_spec name # :nodoc: + def fetch_spec(name) # :nodoc: load_specs :complete if data = @specs[name] @@ -108,23 +110,26 @@ class Gem::Source::Local < Gem::Source end end - def download spec, cache_dir = nil # :nodoc: + def download(spec, cache_dir = nil) # :nodoc: load_specs :complete - @specs.each do |name, data| + @specs.each do |_name, data| return data[0] if data[1].spec == spec end raise Gem::Exception, "Unable to find file for '#{spec.full_name}'" end - def pretty_print q # :nodoc: - q.group 2, '[Local gems:', ']' do - q.breakable - q.seplist @specs.keys do |v| - q.text v.full_name + def pretty_print(q) # :nodoc: + q.object_group(self) do + q.group 2, "[Local gems:", "]" do + q.breakable + if @specs + q.seplist @specs.keys do |v| + q.text v.full_name + end + end end end end - end |
