2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/source'
|
|
|
|
|
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
|
|
|
|
# sources.add 'https://rubygems.example'
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
when URI
|
|
|
|
Gem::Source.new(obj)
|
|
|
|
when Gem::Source
|
|
|
|
obj
|
|
|
|
else
|
|
|
|
Gem::Source.new(URI.parse(obj))
|
|
|
|
end
|
|
|
|
|
|
|
|
@sources << src
|
|
|
|
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
|
|
|
|
@sources.each { |s| yield s.uri.to_s }
|
|
|
|
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
|
|
|
|
|
2013-11-18 19:34:13 -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
|
|
|
|
@sources.map { |x| x.uri.to_s }
|
|
|
|
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
|
|
|
|
@sources.find { |x| x.uri.to_s == other.to_s }
|
|
|
|
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.
|
|
|
|
|
|
|
|
def delete source
|
|
|
|
if source.kind_of? Gem::Source
|
|
|
|
@sources.delete source
|
2012-11-29 01:52:18 -05:00
|
|
|
else
|
2013-11-10 12:51:40 -05:00
|
|
|
@sources.delete_if { |x| x.uri.to_s == source.to_s }
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|