Class: Pod::Dependency

Inherits:
Object
  • Object
show all
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](github.com/rubygems/rubygems/blob/master/MIT.txt).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, requirements) ⇒ Dependency #initialize(name, external_source) ⇒ Dependency #initialize(name, is_head) ⇒ Dependency

Returns a new instance of Dependency.

Overloads:

  • #initialize(name, requirements) ⇒ Dependency

    Examples:

    Initialization with version requirements.

    
    Dependency.new('AFNetworking')
    Dependency.new('AFNetworking', '~> 1.0')
    Dependency.new('AFNetworking', '>= 0.5', '< 0.7')
    
  • #initialize(name, external_source) ⇒ Dependency

    Examples:

    Initialization with an external source.

    
    Dependency.new('libPusher', {:git     => 'example.com/repo.git'})
    Dependency.new('libPusher', {:path   => 'path/to/folder'})
    Dependency.new('libPusher', {:podspec => 'example.com/libPusher.podspec'})
    
  • #initialize(name, is_head) ⇒ Dependency

    Examples:

    Initialization with the head option

    
    Dependency.new('RestKit', :head)
    


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cocoapods-core/dependency.rb', line 72

def initialize(name = nil, *requirements)
  if requirements.last.is_a?(Hash)
    @external_source = requirements.pop
    unless requirements.empty?
      raise Informative, "A dependency with an external source may not specify version requirements (#{name})."
    end

  elsif requirements.last == :head
    @head = true
    requirements.pop
    unless requirements.empty?
      raise Informative, "A `:head` dependency may not specify version requirements (#{name})."
    end
  end

  if requirements.length == 1 && requirements.first.is_a?(Requirement)
    requirements = requirements.first
  end
  @name = name
  @requirement = Requirement.create(requirements)
end

Instance Attribute Details

#external_sourceHash{Symbol=>String}



22
23
24
# File 'lib/cocoapods-core/dependency.rb', line 22

def external_source
  @external_source
end

#headBool Also known as: head?



28
29
30
# File 'lib/cocoapods-core/dependency.rb', line 28

def head
  @head
end

#nameString



16
17
18
# File 'lib/cocoapods-core/dependency.rb', line 16

def name
  @name
end

#specific_versionVersion



96
97
98
# File 'lib/cocoapods-core/dependency.rb', line 96

def specific_version
  @specific_version
end

Class Method Details

.from_string(string) ⇒ Dependency

Note:

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.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/cocoapods-core/dependency.rb', line 312

def self.from_string(string)
  match_data = string.match(/(\S*)( (.*))?/)
  name = match_data[1]
  version = match_data[2]
  version = version.gsub(/[()]/,'') if version
  case version
  when nil || /from `(.*)(`|')/
    Dependency.new(name)
  when /HEAD/
    Dependency.new(name, :head)
  else
    version_requirements =  version.split(',') if version
    Dependency.new(name, version_requirements)
  end
end

Instance Method Details

#<=>(other) ⇒ Fixnum



205
206
207
# File 'lib/cocoapods-core/dependency.rb', line 205

def <=> other
  self.name <=> other.name
end

#==(other) ⇒ Bool Also known as: eql?



186
187
188
189
190
191
192
# File 'lib/cocoapods-core/dependency.rb', line 186

def ==(other)
  Dependency === other &&
    name == other.name &&
    requirement == other.requirement &&
    head? == other.head? &&
    external_source == other.external_source
end

#compatible?(other) ⇒ Bool

Note:

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.



172
173
174
175
176
177
178
179
180
# File 'lib/cocoapods-core/dependency.rb', line 172

def compatible?(other)
  return false unless name == other.name
  return false unless head? == other.head?
  return false unless external_source == other.external_source

  other.requirement.requirements.all? do | operator, version |
    self.requirement.satisfied_by? Version.new(version)
  end
end

#external?Bool



118
119
120
# File 'lib/cocoapods-core/dependency.rb', line 118

def external?
  !!@external_source
end

#hashObject

@return [Fixnum] The hash value based on the name and on the

requirements.


198
199
200
# File 'lib/cocoapods-core/dependency.rb', line 198

def hash
  name.hash ^ requirement.hash
end

#inspectString



330
331
332
333
# File 'lib/cocoapods-core/dependency.rb', line 330

def inspect
  "<#{self.class} name=#{self.name} requirements=#{requirement.to_s} " \
  "external_source=#{external_source||'nil'}>"
end

#local?Bool



124
125
126
# File 'lib/cocoapods-core/dependency.rb', line 124

def local?
  external_source && !!(external_source[:path] || external_source[:local])
end

#match?(name, version) ⇒ Bool

Checks whether the dependency would be satisfied by the specification with the given name and version.



256
257
258
259
260
# File 'lib/cocoapods-core/dependency.rb', line 256

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

Note:

If one of the decencies specifies an external source or is head, the resulting dependency preserves this attributes.

Merges the version requirements of the dependency with another one.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cocoapods-core/dependency.rb', line 220

def merge(other)
  unless name == other.name then
    raise ArgumentError, "#{self} and #{other} have different names"
  end
  default   = Requirement.default
  self_req  = self.requirement
  other_req = other.requirement

  if other_req == default
    dep = self.class.new(name, self_req)
  elsif self_req == default
    dep = self.class.new(name, other_req)
  else
    dep = self.class.new(name, self_req.as_list.concat(other_req.as_list))
  end

  dep.head = head? || other.head?
  if external_source || other.external_source
    self_external_source  = external_source || {}
    other_external_source = other.external_source || {}
    dep.external_source = self_external_source.merge(other_external_source)
  end
  dep
end

#requirementRequirement



105
106
107
108
# File 'lib/cocoapods-core/dependency.rb', line 105

def requirement
  return Requirement.new(Version.new(specific_version.version)) if specific_version
  @requirement
end

#root_nameString

Note:

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.



157
158
159
# File 'lib/cocoapods-core/dependency.rb', line 157

def root_name
  subspec_dependency? ? @name.split('/').first : @name
end

#subspec_dependency?Bool



112
113
114
# File 'lib/cocoapods-core/dependency.rb', line 112

def subspec_dependency?
  @name.include?('/')
end

#to_root_dependencyDependency

TODO:

This should not use ‘dup`. The `name` property should be an attr_reader.

Note:

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.



142
143
144
145
146
# File 'lib/cocoapods-core/dependency.rb', line 142

def to_root_dependency
  dep = dup
  dep.name = root_name
  dep
end

#to_sString

Note:

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.

Examples:

Output examples


"libPusher"
"libPusher (= 1.0)"
"libPusher (~> 1.0.1)"
"libPusher (> 1.0, < 2.0)"
"libPusher (HEAD)"
"libPusher (from `www.example.com')"
"libPusher (defined in Podfile)"
"RestKit/JSON"


285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/cocoapods-core/dependency.rb', line 285

def to_s
  version = ''
  if external?
    version << external_source_description(external_source)
  elsif head?
    version << 'HEAD'
  elsif requirement != Requirement.default
    version << requirement.to_s
  end
  result = @name.dup
  result << " (#{version})" unless version.empty?
  result
end