Class: Pod::Dependency
- Inherits:
-
Object
- Object
- Pod::Dependency
- Defined in:
- lib/cocoapods-core/dependency.rb
Overview
The Dependency allows to specify dependencies of a Podfile or a Specification on a Pod. It stores the name of the dependency, version requirements and external sources information.
This class is based on the dependency class of RubyGems and mimics its implementation with adjustments specific to CocoaPods. RubyGems is available under the MIT license.
Instance Attribute Summary collapse
-
#external_source ⇒ Hash{Symbol=>String}
A hash describing the external source where the pod should be fetched.
-
#name ⇒ String
The name of the Pod described by this dependency.
-
#podspec_repo ⇒ String
The source URL of the podspec repo to use to resolve this dependency.
-
#specific_version ⇒ Version
Whether the dependency points to a specific version.
Class Method Summary collapse
-
.from_string(string) ⇒ Dependency
Generates a dependency from its string representation.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
How the dependency should be sorted respect to another one according to its name.
-
#==(other) ⇒ Boolean
(also: #eql?)
Whether the dependency is equal to another taking into account the loaded specification, the head options and the external source.
-
#compatible?(other) ⇒ Boolean
Checks if a dependency would be satisfied by the requirements of another dependency.
-
#external? ⇒ Boolean
Whether the dependency points to an external source.
-
#external_source_description(source) ⇒ String
private
Creates a string representation of the external source suitable for UI.
-
#hash ⇒ Object
@return [Fixnum] The hash value based on the name and on the requirements.
-
#initialize(name = nil, *requirements) ⇒ Dependency
constructor
A new instance of Dependency.
-
#inspect ⇒ String
A string representation suitable for debugging.
-
#local? ⇒ Boolean
Whether the dependency points to a local path.
-
#match?(name, version) ⇒ Boolean
Checks whether the dependency would be satisfied by the specification with the given name and version.
-
#merge(other) ⇒ Dependency
Merges the version requirements of the dependency with another one.
-
#prerelease? ⇒ Boolean
Whether the dependency has any pre-release requirements.
-
#requirement ⇒ Requirement
The requirement of this dependency (a set of one or more version restrictions).
-
#root_name ⇒ String
Returns the name of the Pod that the dependency is pointing to.
-
#subspec_dependency? ⇒ Boolean
Whether the dependency points to a subspec.
-
#to_root_dependency ⇒ Dependency
Creates a new dependency with the name of the top level spec and the same version requirements.
-
#to_s ⇒ String
Creates a string representation of the dependency suitable for serialization and de-serialization without loss of information.
Constructor Details
#initialize(name, requirements) ⇒ Dependency #initialize(name, external_source) ⇒ Dependency #initialize(name, requirements, podspec_repo) ⇒ Dependency
Returns a new instance of Dependency.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cocoapods-core/dependency.rb', line 72 def initialize(name = nil, *requirements) if requirements.last.is_a?(Hash) additional_params = requirements.pop.select { |_, v| !v.nil? } additional_params = nil if additional_params.empty? if additional_params && @podspec_repo = additional_params[:source] # This dependency specifies the exact source podspec repo to use. additional_params.delete(:source) unless additional_params.empty? raise Informative, 'A dependency with a specified podspec repo may ' \ "not include other source parameters (#{name})." end elsif @external_source = additional_params unless requirements.empty? raise Informative, 'A dependency with an external source may not ' \ "specify version requirements (#{name})." end end elsif requirements.last == :head raise Informative, '`:head` dependencies have been removed. Please use ' \ "normal external source dependencies (`:git => 'GIT_REPO_URL'`) " \ "instead of `:head` for `#{name}`." end if requirements.length == 1 && requirements.first.is_a?(Requirement) requirements = requirements.first end @name = name @requirement = Requirement.create(requirements) @specific_requirement ||= nil @external_source ||= nil end |
Instance Attribute Details
#external_source ⇒ Hash{Symbol=>String}
Returns a hash describing the external source where the pod should be fetched. The external source has to provide its own Specification file.
20 21 22 |
# File 'lib/cocoapods-core/dependency.rb', line 20 def external_source @external_source end |
#name ⇒ String
Returns The name of the Pod described by this dependency.
14 15 16 |
# File 'lib/cocoapods-core/dependency.rb', line 14 def name @name end |
#podspec_repo ⇒ String
Returns The source URL of the podspec repo to use to resolve this dependency. If not set then the standard source list should be used to resolve the dependency.
25 26 27 |
# File 'lib/cocoapods-core/dependency.rb', line 25 def podspec_repo @podspec_repo end |
#specific_version ⇒ Version
Returns whether the dependency points to a specific version.
108 109 110 |
# File 'lib/cocoapods-core/dependency.rb', line 108 def specific_version @specific_version end |
Class Method Details
.from_string(string) ⇒ Dependency
The information about external sources is not completely serialized in the string representation and should be stored a part by clients that need to create a dependency equal to the original one.
Generates a dependency from its string representation.
351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/cocoapods-core/dependency.rb', line 351 def self.from_string(string) match_data = string.match(/((?:\s?[^\s(])+)( (?:.*))?/) name = match_data[1] version = match_data[2] version = version.gsub(/[()]/, '') if version case version when nil, /from `(.*)(`|')/ Dependency.new(name) else version_requirements = version.split(',') if version Dependency.new(name, version_requirements) end end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Returns How the dependency should be sorted respect to another one according to its name.
222 223 224 |
# File 'lib/cocoapods-core/dependency.rb', line 222 def <=>(other) name <=> other.name end |
#==(other) ⇒ Boolean Also known as: eql?
Returns whether the dependency is equal to another taking into account the loaded specification, the head options and the external source.
203 204 205 206 207 208 209 |
# File 'lib/cocoapods-core/dependency.rb', line 203 def ==(other) self.class == other.class && name == other.name && external_source == other.external_source && podspec_repo == other.podspec_repo && requirement == other.requirement end |
#compatible?(other) ⇒ Boolean
This is used by the Lockfile to check if a stored dependency is still compatible with the Podfile.
Checks if a dependency would be satisfied by the requirements of another dependency.
190 191 192 193 194 195 196 197 |
# File 'lib/cocoapods-core/dependency.rb', line 190 def compatible?(other) return false unless name == other.name return false unless external_source == other.external_source other.requirement.requirements.all? do |_operator, version| requirement.satisfied_by? Version.new(version) end end |
#external? ⇒ Boolean
Returns whether the dependency points to an external source.
134 135 136 |
# File 'lib/cocoapods-core/dependency.rb', line 134 def external? !@external_source.nil? end |
#external_source_description(source) ⇒ String (private)
Improve the description for Mercurial and Subversion.
Creates a string representation of the external source suitable for UI.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/cocoapods-core/dependency.rb', line 388 def external_source_description(source) if source.key?(:git) desc = "`#{source[:git]}`" desc << ", commit `#{source[:commit]}`" if source[:commit] desc << ", branch `#{source[:branch]}`" if source[:branch] desc << ", tag `#{source[:tag]}`" if source[:tag] elsif source.key?(:hg) desc = "`#{source[:hg]}`" elsif source.key?(:svn) desc = "`#{source[:svn]}`" elsif source.key?(:podspec) desc = "`#{source[:podspec]}`" elsif source.key?(:path) desc = "`#{source[:path]}`" else desc = "`#{source}`" end "from #{desc}" end |
#hash ⇒ Object
@return [Fixnum] The hash value based on the name and on the requirements.
215 216 217 |
# File 'lib/cocoapods-core/dependency.rb', line 215 def hash name.hash ^ requirement.hash end |
#inspect ⇒ String
Returns a string representation suitable for debugging.
367 368 369 370 |
# File 'lib/cocoapods-core/dependency.rb', line 367 def inspect "<#{self.class} name=#{name} requirements=#{requirement} " \ "source=#{podspec_repo || 'nil'} external_source=#{external_source || 'nil'}>" end |
#local? ⇒ Boolean
Returns whether the dependency points to a local path.
140 141 142 143 144 |
# File 'lib/cocoapods-core/dependency.rb', line 140 def local? if external_source external_source[:path] end end |
#match?(name, version) ⇒ Boolean
Checks whether the dependency would be satisfied by the specification with the given name and version.
298 299 300 301 302 |
# File 'lib/cocoapods-core/dependency.rb', line 298 def match?(name, version) return false unless self.name == name return true if requirement.none? requirement.satisfied_by?(Version.new(version)) end |
#merge(other) ⇒ Dependency
If one of the dependencies specifies an external source or is head, the resulting dependency preserves this attributes.
Merges the version requirements of the dependency with another one.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/cocoapods-core/dependency.rb', line 237 def merge(other) unless name == other.name raise ArgumentError, "#{self} and #{other} have different names" end default = Requirement.default self_req = requirement other_req = other.requirement req = if other_req == default self_req elsif self_req == default other_req else self_req.as_list.concat(other_req.as_list) end opts = {} if external_source || other.external_source opts. merge!(external_source || {}). merge!(other.external_source || {}) req_to_set = req req = [] end if podspec_repo && other.podspec_repo && podspec_repo != other.podspec_repo raise ArgumentError, "#{self} and #{other} have different podspec repos" end if repo = podspec_repo || other.podspec_repo opts[:source] = repo end self.class.new(name, *req, opts).tap do |dep| dep.instance_variable_set(:@requirement, Requirement.create(req_to_set)) if req_to_set end end |
#prerelease? ⇒ Boolean
Whether the dependency has any pre-release requirements
282 283 284 285 |
# File 'lib/cocoapods-core/dependency.rb', line 282 def prerelease? return @prerelease if defined?(@prerelease) @prerelease = requirement.requirements.any? { |_op, version| version.prerelease? } end |
#requirement ⇒ Requirement
Returns the requirement of this dependency (a set of one or more version restrictions).
113 114 115 |
# File 'lib/cocoapods-core/dependency.rb', line 113 def requirement @specific_requirement || @requirement end |
#root_name ⇒ String
In case this is a dependency for a subspec, e.g. 'RestKit/Networking', this returns 'RestKit', which is what the Pod::Source needs to know to retrieve the correct Specification from disk.
Returns the name of the Pod that the dependency is pointing to.
175 176 177 |
# File 'lib/cocoapods-core/dependency.rb', line 175 def root_name subspec_dependency? ? @name.split('/').first : @name end |
#subspec_dependency? ⇒ Boolean
Returns whether the dependency points to a subspec.
128 129 130 |
# File 'lib/cocoapods-core/dependency.rb', line 128 def subspec_dependency? @name.include?('/') end |
#to_root_dependency ⇒ Dependency
This should not use dup
. The name
property should be an
attr_reader.
This is used by the Specification::Set class to merge dependencies and resolve the required version of a Pod regardless what particular specification (subspecs or top level) is required.
Creates a new dependency with the name of the top level spec and the same version requirements.
160 161 162 163 164 |
# File 'lib/cocoapods-core/dependency.rb', line 160 def to_root_dependency dep = dup dep.name = root_name dep end |
#to_s ⇒ String
This representation is used by the Lockfile.
Creates a string representation of the dependency suitable for serialization and de-serialization without loss of information. The string is also suitable for UI.
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/cocoapods-core/dependency.rb', line 326 def to_s version = '' if external? version << external_source_description(external_source) elsif requirement != Requirement.default version << requirement.to_s end result = @name.dup result << " (#{version})" unless version.empty? result end |