2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
##
|
|
|
|
# The SourceList represents the sources rubygems has been configured to use.
|
|
|
|
# A source may be created from an array of sources:
|
|
|
|
#
|
|
|
|
# Gem::SourceList.from %w[https://rubygems.example https://internal.example]
|
|
|
|
#
|
|
|
|
# Or by adding them:
|
|
|
|
#
|
|
|
|
# sources = Gem::SourceList.new
|
2016-02-01 15:43:26 +03:00
|
|
|
# sources << 'https://rubygems.example'
|
2013-11-19 04:34:13 +04:00
|
|
|
#
|
|
|
|
# The most common way to get a SourceList is Gem.sources.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
class Gem::SourceList
|
2013-11-10 21:51:40 +04:00
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new SourceList
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def initialize
|
|
|
|
@sources = []
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# The sources in this list
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
attr_reader :sources
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Creates a new SourceList from an array of sources.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def self.from(ary)
|
|
|
|
list = new
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
list.replace ary
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
def initialize_copy(other) # :nodoc:
|
2012-11-29 10:52:18 +04:00
|
|
|
@sources = @sources.dup
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Appends +obj+ to the source list which may be a Gem::Source, URI or URI
|
|
|
|
# String.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def <<(obj)
|
2019-12-17 13:47:15 +03:00
|
|
|
require "uri"
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
src = case obj
|
|
|
|
when URI
|
|
|
|
Gem::Source.new(obj)
|
|
|
|
when Gem::Source
|
|
|
|
obj
|
|
|
|
else
|
|
|
|
Gem::Source.new(URI.parse(obj))
|
|
|
|
end
|
|
|
|
|
2016-03-04 03:29:40 +03:00
|
|
|
@sources << src unless @sources.include?(src)
|
2012-11-29 10:52:18 +04:00
|
|
|
src
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Replaces this SourceList with the sources in +other+ See #<< for
|
|
|
|
# acceptable items in +other+.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def replace(other)
|
2013-11-10 21:51:40 +04:00
|
|
|
clear
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
other.each do |x|
|
|
|
|
self << x
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Removes all sources from the SourceList.
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@sources.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Yields each source URI in the list.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def each
|
2020-06-10 20:46:05 +03:00
|
|
|
@sources.each {|s| yield s.uri.to_s }
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Yields each source in the list.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def each_source(&b)
|
|
|
|
@sources.each(&b)
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Returns true if there are no sources in this SourceList.
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@sources.empty?
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def ==(other) # :nodoc:
|
2012-11-29 10:52:18 +04:00
|
|
|
to_a == other
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Returns an Array of source URI Strings.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def to_a
|
2020-06-10 20:46:05 +03:00
|
|
|
@sources.map {|x| x.uri.to_s }
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :to_ary, :to_a
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Returns the first source in the list.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def first
|
|
|
|
@sources.first
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Returns true if this source list includes +other+ which may be a
|
|
|
|
# Gem::Source or a source URI.
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def include?(other)
|
|
|
|
if other.kind_of? Gem::Source
|
|
|
|
@sources.include? other
|
|
|
|
else
|
2020-06-10 20:46:05 +03:00
|
|
|
@sources.find {|x| x.uri.to_s == other.to_s }
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Deletes +source+ from the source list which may be a Gem::Source or a URI.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def delete(source)
|
2013-11-10 21:51:40 +04:00
|
|
|
if source.kind_of? Gem::Source
|
|
|
|
@sources.delete source
|
2012-11-29 10:52:18 +04:00
|
|
|
else
|
2020-06-10 20:46:05 +03:00
|
|
|
@sources.delete_if {|x| x.uri.to_s == source.to_s }
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|