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.

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')

    Parameters:

    • name (String)

      the name of the Pod.

    • requirements (Array, Version, String, Requirement)

      an array specifying the version requirements of the dependency.

  • #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'})

    Parameters:

    • name (String)

      the name of the Pod.

    • external_source (Hash)

      a hash describing the external source.

  • #initialize(name, is_head) ⇒ Dependency

    Examples:

    Initialization with the head option

    
    Dependency.new('RestKit', :head)

    Parameters:

    • name (String)

      the name of the Pod.

    • is_head (Symbol)

      a symbol that can be :head or nil.


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

def initialize(name = nil, *requirements)
  if requirements.last.is_a?(Hash)
    external_source = requirements.pop.select { |_, v| !v.nil? }
    @external_source = external_source unless external_source.empty?
    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}

Returns a hash describing the external source where the pod should be fetched. The external source has to provide its own Specification file.

Returns:

  • (Hash{Symbol=>String})

    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

#headBool Also known as: head?

Returns whether the dependency should use the podspec with the highest know version but force the downloader to checkout the head of the source repository.

Returns:

  • (Bool)

    whether the dependency should use the podspec with the highest know version but force the downloader to checkout the head of the source repository.


26
27
28
# File 'lib/cocoapods-core/dependency.rb', line 26

def head
  @head
end

#nameString

Returns The name of the Pod described by this dependency.

Returns:

  • (String)

    The name of the Pod described by this dependency.


14
15
16
# File 'lib/cocoapods-core/dependency.rb', line 14

def name
  @name
end

#specific_versionVersion

Returns whether the dependency points to a specific version.

Returns:

  • (Version)

    whether the dependency points to a specific version.


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

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.

Parameters:

  • string (String)

    The string that describes the dependency generated from #to_s.

Returns:

  • (Dependency)

    the dependency described by the string.


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/cocoapods-core/dependency.rb', line 327

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)
  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

Returns How the dependency should be sorted respect to another one according to its name.

Returns:

  • (Fixnum)

    How the dependency should be sorted respect to another one according to its name.


211
212
213
# File 'lib/cocoapods-core/dependency.rb', line 211

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Bool 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.

Returns:

  • (Bool)

    whether the dependency is equal to another taking into account the loaded specification, the head options and the external source.


192
193
194
195
196
197
198
# File 'lib/cocoapods-core/dependency.rb', line 192

def ==(other)
  self.class == other.class &&
    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.

Parameters:

Returns:

  • (Bool)

    whether the dependency is compatible with the given one.


178
179
180
181
182
183
184
185
186
# File 'lib/cocoapods-core/dependency.rb', line 178

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|
    requirement.satisfied_by? Version.new(version)
  end
end

#external?Bool

Returns whether the dependency points to an external source.

Returns:

  • (Bool)

    whether the dependency points to an external source.


122
123
124
# File 'lib/cocoapods-core/dependency.rb', line 122

def external?
  !@external_source.nil?
end

#external_source_description(source) ⇒ String (private)

TODO:

Improve the description for Mercurial and Subversion.

Creates a string representation of the external source suitable for UI.

Examples:

Output examples


"from `www.example.com/libPusher.git', tag `v0.0.1'"
"from `www.example.com/libPusher.podspec'"
"from `~/path/to/libPusher'"

Returns:

  • (String)

    the description of the external source.


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/cocoapods-core/dependency.rb', line 366

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]}`"
  elsif source.key?(:local)
    desc = "`#{source[:local]}`"
  else
    desc = "`#{source}`"
  end
  "from #{desc}"
end

#hashObject

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


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

def hash
  name.hash ^ requirement.hash
end

#inspectString

Returns a string representation suitable for debugging.

Returns:

  • (String)

    a string representation suitable for debugging.


345
346
347
348
# File 'lib/cocoapods-core/dependency.rb', line 345

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

#local?Bool

Returns whether the dependency points to a local path.

Returns:

  • (Bool)

    whether the dependency points to a local path.


128
129
130
131
132
# File 'lib/cocoapods-core/dependency.rb', line 128

def local?
  if external_source
    external_source[:path] || external_source[:local]
  end
end

#match?(name, version) ⇒ Bool

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

Parameters:

  • The (String)

    proposed name.

  • version (String, Version)

    The proposed version.

Returns:

  • (Bool)

    Whether the dependency is satisfied.


271
272
273
274
275
# File 'lib/cocoapods-core/dependency.rb', line 271

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.

Parameters:

  • other (Dependency)

    the other dependency to merge with.

Returns:

  • (Dependency)

    a dependency (not necessary a new instance) that includes also the version requirements of the given one.


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/cocoapods-core/dependency.rb', line 226

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

  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

#prerelease?Bool

Whether the dependency has any pre-release requirements

Returns:

  • (Bool)

    Whether the dependency has any pre-release requirements


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

def prerelease?
  @prerelease ||= requirement.requirements.
    any? { |r| Version.new(r[1].version).prerelease? }
end

#requirementRequirement

TODO:

The specific version is stripped from head information because because its string representation would not parse. It would be better to add something like Version#display_string.

Returns the requirement of this dependency (a set of one or more version restrictions).

Returns:

  • (Requirement)

    the requirement of this dependency (a set of one or more version restrictions).


106
107
108
109
110
111
112
# File 'lib/cocoapods-core/dependency.rb', line 106

def requirement
  if specific_version
    Requirement.new(Version.new(specific_version.version))
  else
    @requirement
  end
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.

Returns:

  • (String)

    the name of the Pod.


163
164
165
# File 'lib/cocoapods-core/dependency.rb', line 163

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

#subspec_dependency?Bool

Returns whether the dependency points to a subspec.

Returns:

  • (Bool)

    whether the dependency points to a subspec.


116
117
118
# File 'lib/cocoapods-core/dependency.rb', line 116

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.

Returns:

  • (Dependency)

    a dependency with the same versions requirements that is guaranteed to point to a top level specification.


148
149
150
151
152
# File 'lib/cocoapods-core/dependency.rb', line 148

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"

Returns:

  • (String)

    the representation of the dependency.


300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/cocoapods-core/dependency.rb', line 300

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