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