2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# The local source finds gems in the current directory for fulfilling
|
|
|
|
# dependencies.
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
class Gem::Source::Local < Gem::Source
|
2013-11-18 19:34:13 -05:00
|
|
|
def initialize # :nodoc:
|
2013-08-26 16:24:51 -04:00
|
|
|
@specs = nil
|
|
|
|
@api_uri = nil
|
|
|
|
@uri = nil
|
2017-10-07 21:32:18 -04:00
|
|
|
@load_specs_names = {}
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Local sorts before Gem::Source and after Gem::Source::Installed
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def <=>(other)
|
2013-07-09 19:21:36 -04:00
|
|
|
case other
|
2013-11-30 18:27:52 -05:00
|
|
|
when Gem::Source::Installed,
|
|
|
|
Gem::Source::Lock then
|
2013-07-09 19:21:36 -04:00
|
|
|
-1
|
|
|
|
when Gem::Source::Local then
|
|
|
|
0
|
|
|
|
when Gem::Source then
|
|
|
|
1
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect # :nodoc:
|
2013-08-26 16:24:51 -04:00
|
|
|
keys = @specs ? @specs.keys.sort : "NOT LOADED"
|
|
|
|
"#<%s specs: %p>" % [self.class, keys]
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def load_specs(type) # :nodoc:
|
2017-10-07 21:32:18 -04:00
|
|
|
@load_specs_names[type] ||= begin
|
|
|
|
names = []
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
@specs = {}
|
|
|
|
|
|
|
|
Dir["*.gem"].each do |file|
|
|
|
|
begin
|
|
|
|
pkg = Gem::Package.new(file)
|
|
|
|
rescue SystemCallError, Gem::Package::FormatError
|
|
|
|
# ignore
|
2013-07-09 19:21:36 -04:00
|
|
|
else
|
2017-10-07 21:32:18 -04:00
|
|
|
tup = pkg.spec.name_tuple
|
|
|
|
@specs[tup] = [File.expand_path(file), pkg]
|
|
|
|
|
|
|
|
case type
|
|
|
|
when :released
|
|
|
|
unless pkg.spec.version.prerelease?
|
|
|
|
names << pkg.spec.name_tuple
|
|
|
|
end
|
|
|
|
when :prerelease
|
|
|
|
if pkg.spec.version.prerelease?
|
|
|
|
names << pkg.spec.name_tuple
|
|
|
|
end
|
|
|
|
when :latest
|
|
|
|
tup = pkg.spec.name_tuple
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
cur = names.find {|x| x.name == tup.name }
|
2017-10-07 21:32:18 -04:00
|
|
|
if !cur
|
|
|
|
names << tup
|
|
|
|
elsif cur.version < tup.version
|
|
|
|
names.delete cur
|
|
|
|
names << tup
|
|
|
|
end
|
|
|
|
else
|
|
|
|
names << pkg.spec.name_tuple
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
names
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def find_gem(gem_name, version = Gem::Requirement.default, # :nodoc:
|
|
|
|
prerelease = false)
|
2013-07-09 19:21:36 -04:00
|
|
|
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
|
2017-10-07 21:32:18 -04:00
|
|
|
elsif !s.version.prerelease? || version.prerelease?
|
2013-07-09 19:21:36 -04:00
|
|
|
found << s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
found.max_by {|s| s.version }
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_spec(name) # :nodoc:
|
2013-07-09 19:21:36 -04:00
|
|
|
load_specs :complete
|
|
|
|
|
|
|
|
if data = @specs[name]
|
|
|
|
data.last.spec
|
|
|
|
else
|
2013-10-15 20:14:16 -04:00
|
|
|
raise Gem::Exception, "Unable to find spec for #{name.inspect}"
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def download(spec, cache_dir = nil) # :nodoc:
|
2013-07-09 19:21:36 -04:00
|
|
|
load_specs :complete
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def pretty_print(q) # :nodoc:
|
2013-07-09 19:21:36 -04:00
|
|
|
q.group 2, "[Local gems:", "]" do
|
|
|
|
q.breakable
|
|
|
|
q.seplist @specs.keys do |v|
|
|
|
|
q.text v.full_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|