2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Dependency class holds a Gem name and a Gem::Requirement
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
class Gem::Dependency
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Valid dependency types.
|
|
|
|
#--
|
|
|
|
# When this list is updated, be sure to change
|
|
|
|
# Gem::Specification::CURRENT_SPECIFICATION_VERSION as well.
|
|
|
|
|
|
|
|
TYPES = [
|
|
|
|
:development,
|
|
|
|
:runtime,
|
|
|
|
]
|
|
|
|
|
|
|
|
##
|
|
|
|
# Dependency name or regular expression.
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
attr_accessor :name
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Dependency type.
|
|
|
|
|
|
|
|
attr_reader :type
|
|
|
|
|
|
|
|
##
|
|
|
|
# Dependent versions.
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
attr_writer :version_requirements
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Orders dependencies by name only.
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def <=>(other)
|
|
|
|
[@name] <=> [other.name]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2008-06-17 18:04:18 -04:00
|
|
|
# Constructs a dependency with +name+ and +requirements+.
|
|
|
|
|
|
|
|
def initialize(name, version_requirements, type=:runtime)
|
2007-11-10 02:48:56 -05:00
|
|
|
@name = name
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
unless TYPES.include? type
|
|
|
|
raise ArgumentError, "Valid types are #{TYPES.inspect}, not #{@type.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@type = type
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
@version_requirements = Gem::Requirement.create version_requirements
|
|
|
|
@version_requirement = nil # Avoid warnings.
|
|
|
|
end
|
|
|
|
|
|
|
|
def version_requirements
|
|
|
|
normalize if defined? @version_requirement and @version_requirement
|
|
|
|
@version_requirements
|
|
|
|
end
|
|
|
|
|
|
|
|
def requirement_list
|
|
|
|
version_requirements.as_list
|
|
|
|
end
|
|
|
|
|
|
|
|
alias requirements_list requirement_list
|
|
|
|
|
|
|
|
def normalize
|
2009-06-09 17:38:59 -04:00
|
|
|
ver = @version_requirement.instance_variable_get :@version
|
2007-11-10 02:48:56 -05:00
|
|
|
@version_requirements = Gem::Requirement.new([ver])
|
|
|
|
@version_requirement = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
2008-06-17 18:04:18 -04:00
|
|
|
"#{name} (#{version_requirements}, #{@type || :runtime})"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def pretty_print(q) # :nodoc:
|
|
|
|
q.group 1, 'Gem::Dependency.new(', ')' do
|
|
|
|
q.pp @name
|
|
|
|
q.text ','
|
|
|
|
q.breakable
|
|
|
|
|
|
|
|
q.pp @version_requirements
|
|
|
|
|
|
|
|
q.text ','
|
|
|
|
q.breakable
|
|
|
|
|
|
|
|
q.pp @type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def ==(other) # :nodoc:
|
|
|
|
self.class === other &&
|
|
|
|
self.name == other.name &&
|
2008-06-17 18:04:18 -04:00
|
|
|
self.type == other.type &&
|
2007-11-10 02:48:56 -05:00
|
|
|
self.version_requirements == other.version_requirements
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# Uses this dependency as a pattern to compare to +other+. This dependency
|
|
|
|
# will match if the name matches the other's name, and other has only an
|
|
|
|
# equal version requirement that satisfies this dependency.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
def =~(other)
|
2009-06-09 17:38:59 -04:00
|
|
|
other = if self.class === other then
|
|
|
|
other
|
|
|
|
else
|
|
|
|
return false unless other.respond_to? :name and
|
|
|
|
other.respond_to? :version
|
|
|
|
|
|
|
|
Gem::Dependency.new other.name, other.version
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
pattern = @name
|
2009-06-09 17:38:59 -04:00
|
|
|
pattern = /\A#{Regexp.escape @name}\Z/ unless Regexp === pattern
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
return false unless pattern =~ other.name
|
|
|
|
|
|
|
|
reqs = other.version_requirements.requirements
|
|
|
|
|
|
|
|
return false unless reqs.length == 1
|
|
|
|
return false unless reqs.first.first == '='
|
|
|
|
|
|
|
|
version = reqs.first.last
|
|
|
|
|
|
|
|
version_requirements.satisfied_by? version
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# A dependency's hash is the sum of the hash of the #name, #type and
|
|
|
|
# #version_requirements
|
|
|
|
|
|
|
|
def hash
|
2008-06-17 18:04:18 -04:00
|
|
|
name.hash + type.hash + version_requirements.hash
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def inspect # :nodoc:
|
|
|
|
"<%s type=%p name=%p requirements=%p>" % [self.class, @type, @name,
|
|
|
|
version_requirements.to_s]
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|