Class: Pod::Specification

Inherits:
Object
  • Object
show all
Includes:
DSL, DSL::Deprecations, JSONSupport, RootAttributesAccessors
Defined in:
lib/cocoapods-core/specification.rb,
lib/cocoapods-core/specification/dsl.rb,
lib/cocoapods-core/specification/set.rb,
lib/cocoapods-core/specification/json.rb,
lib/cocoapods-core/specification/linter.rb,
lib/cocoapods-core/specification/consumer.rb,
lib/cocoapods-core/specification/dsl/attribute.rb,
lib/cocoapods-core/specification/linter/result.rb,
lib/cocoapods-core/specification/set/presenter.rb,
lib/cocoapods-core/specification/linter/analyzer.rb,
lib/cocoapods-core/specification/dsl/deprecations.rb,
lib/cocoapods-core/specification/dsl/platform_proxy.rb,
lib/cocoapods-core/specification/dsl/attribute_support.rb,
lib/cocoapods-core/specification/root_attribute_accessors.rb

Overview

The Specification provides a DSL to describe a Pod. A pod is defined as a library originating from a source. A specification can support detailed attributes for modules of code through subspecs.

Usually it is stored in files with ‘podspec` extension.

Defined Under Namespace

Modules: DSL, JSONSupport Classes: Consumer, Linter, Set

Constant Summary

Constants included from DSL

DSL::LICENSE_KEYS, DSL::PLATFORMS, DSL::SOURCE_KEYS

Instance Attribute Summary collapse

Hierarchy collapse

Dependencies & Subspecs collapse

DSL helpers collapse

DSL attribute writers collapse

File representation collapse

Validation collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSONSupport

#safe_to_hash?, #to_hash, #to_json, #to_pretty_json

Methods included from DSL::Deprecations

#preferred_dependency=, #xcconfig=

Methods included from DSL

#authors=, #cocoapods_version=, #compiler_flags=, #default_subspecs=, #dependency, #deployment_target=, #deprecated=, #deprecated_in_favor_of=, #description=, #docset_url=, #documentation_url=, #exclude_files=, #frameworks=, #header_dir=, #header_mappings_dir=, #homepage=, #ios, #libraries=, #license=, #module_map=, #module_name=, #name=, #osx, #platform=, #pod_target_xcconfig=, #prefix_header_contents=, #prefix_header_file=, #prepare_command=, #preserve_paths=, #private_header_files=, #public_header_files=, #requires_arc=, #resource_bundles=, #resources=, #screenshots=, #social_media_url=, #source=, #source_files=, #subspec, #summary=, #user_target_xcconfig=, #vendored_frameworks=, #vendored_libraries=, #version=, #watchos, #weak_frameworks=

Methods included from DSL::AttributeSupport

#attribute, #root_attribute

Constructor Details

#initialize(parent = nil, name = nil) {|_self| ... } ⇒ Specification

Returns a new instance of Specification.

Parameters:

  • parent (Specification) (defaults to: nil)

    @see parent

  • name (String) (defaults to: nil)

    the name of the specification.

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
40
41
# File 'lib/cocoapods-core/specification.rb', line 33

def initialize(parent = nil, name = nil)
  @attributes_hash = {}
  @subspecs = []
  @consumers = {}
  @parent = parent
  attributes_hash['name'] = name

  yield self if block_given?
end

Instance Attribute Details

#attributes_hashHash

Returns the hash that stores the information of the attributes of the specification.

Returns:

  • (Hash)

    the hash that stores the information of the attributes of the specification.



46
47
48
# File 'lib/cocoapods-core/specification.rb', line 46

def attributes_hash
  @attributes_hash
end

#parentSpecification (readonly)

Returns the parent of the specification unless the specification is a root.

Returns:

  • (Specification)

    the parent of the specification unless the specification is a root.



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

def parent
  @parent
end

#subspecsArray<Specification>

Returns The subspecs of the specification.

Returns:



50
51
52
# File 'lib/cocoapods-core/specification.rb', line 50

def subspecs
  @subspecs
end

Class Method Details

.from_file(path, subspec_name = nil) ⇒ Specification

Loads a specification form the given path.

Parameters:

  • path (Pathname, String)

    the path of the ‘podspec` file.

  • subspec_name (String) (defaults to: nil)

    the name of the specification that should be returned. If it is nil returns the root specification.

Returns:

Raises:

  • If the file doesn’t return a Pods::Specification after evaluation.



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/cocoapods-core/specification.rb', line 527

def self.from_file(path, subspec_name = nil)
  path = Pathname.new(path)
  unless path.exist?
    raise Informative, "No podspec exists at path `#{path}`."
  end

  string = File.open(path, 'r:utf-8') { |f| f.read }
  # Work around for Rubinius incomplete encoding in 1.9 mode
  if string.respond_to?(:encoding) && string.encoding.name != 'UTF-8'
    string.encode!('UTF-8')
  end

  from_string(string, path, subspec_name)
end

.from_hash(hash, parent = nil) ⇒ Specification

Configures a new specification from the given hash.

Parameters:

  • the (Hash)

    hash which contains the information of the specification.

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-core/specification/json.rb', line 62

def self.from_hash(hash, parent = nil)
  spec = Spec.new(parent)
  attributes_hash = hash.dup
  subspecs = attributes_hash.delete('subspecs')
  spec.attributes_hash = attributes_hash
  if subspecs
    spec.subspecs = subspecs.map do |s_hash|
      Specification.from_hash(s_hash, spec)
    end
  end
  spec
end

.from_json(json) ⇒ Specification

Configures a new specification from the given JSON representation.

Parameters:

  • the (String)

    JSON encoded hash which contains the information of the specification.

Returns:



49
50
51
52
53
# File 'lib/cocoapods-core/specification/json.rb', line 49

def self.from_json(json)
  require 'json'
  hash = JSON.parse(json)
  from_hash(hash)
end

.from_string(spec_contents, path, subspec_name = nil) ⇒ Specification

Loads a specification with the given string. The specification is evaluated in the context of ‘path`.

Parameters:

  • spec_contents (String)

    A string describing a specification.

  • path (Pathname, String)

    @see from_file

  • subspec_name (String) (defaults to: nil)

    @see from_file

Returns:



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/cocoapods-core/specification.rb', line 553

def self.from_string(spec_contents, path, subspec_name = nil)
  path = Pathname.new(path)
  spec = nil
  Dir.chdir(path.parent.directory? ? path.parent : Dir.pwd) do
    case path.extname
    when '.podspec'
      spec = ::Pod._eval_podspec(spec_contents, path)
      unless spec.is_a?(Specification)
        raise Informative, "Invalid podspec file at path `#{path}`."
      end
    when '.json'
      spec = Specification.from_json(spec_contents)
    else
      raise Informative, "Unsupported specification format `#{path.extname}`."
    end
  end

  spec.defined_in_file = path
  spec.subspec_by_name(subspec_name)
end

.name_and_version_from_string(string_representation) ⇒ Array<String, Version>

Returns the name and the version of a pod.

Examples:

Input examples


"libPusher (1.0)"
"libPusher (HEAD based on 1.0)"
"RestKit/JSON (1.0)"

Parameters:

Returns:

  • (Array<String, Version>)

    the name and the version of a pod.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cocoapods-core/specification.rb', line 124

def self.name_and_version_from_string(string_representation)
  match_data = string_representation.match(/\A((?:\s?[^\s(])+)(?: \((.+)\))?\Z/)
  unless match_data
    raise Informative, 'Invalid string representation for a ' \
      "specification: `#{string_representation}`. " \
      'The string representation should include the name and ' \
      'optionally the version of the Pod.'
  end
  name = match_data[1]
  vers = Version.new(match_data[2])
  [name, vers]
end

.root_name(full_name) ⇒ String

Returns the root name of a specification.

Parameters:

  • the (String)

    name of a specification or of a subspec.

Returns:

  • (String)

    the root name



143
144
145
# File 'lib/cocoapods-core/specification.rb', line 143

def self.root_name(full_name)
  full_name.split('/').first
end

Instance Method Details

#==(other) ⇒ Bool

TODO:

Not sure if comparing only the name and the version is the way to go. This is used by the installer to group specifications by root spec.

Checks if a specification is equal to the given one according its name and to its version.

Parameters:

Returns:

  • (Bool)

    Whether the specifications are equal.



64
65
66
67
68
69
70
# File 'lib/cocoapods-core/specification.rb', line 64

def ==(other)
  # TODO
  # self.class === other &&
  #   attributes_hash == other.attributes_hash &&
  #   subspecs == other.subspecs &&
  to_s == other.to_s
end

#all_dependencies(platform = nil) ⇒ Array<Dependency>

Returns all the dependencies of the specification.

Returns:

  • (Array<Dependency>)

    all the dependencies of the specification.



307
308
309
# File 'lib/cocoapods-core/specification.rb', line 307

def all_dependencies(platform = nil)
  dependencies(platform) + subspec_dependencies(platform)
end

#available_platformsArray<Platform>

Note:

If no platform is specified, this method returns all known platforms.

Returns The platforms that the Pod is supported on.

Returns:

  • (Array<Platform>)

    The platforms that the Pod is supported on.



364
365
366
367
368
# File 'lib/cocoapods-core/specification.rb', line 364

def available_platforms
  names = supported_platform_names
  names = PLATFORMS if names.empty?
  names.map { |name| Platform.new(name, deployment_target(name)) }
end

#checksumString, Nil

Returns:

  • (String)

    The SHA1 digest of the file in which the specification is defined.

  • (Nil)

    If the specification is not defined in a file.



497
498
499
500
501
502
503
504
# File 'lib/cocoapods-core/specification.rb', line 497

def checksum
  require 'digest'
  unless defined_in_file.nil?
    checksum = Digest::SHA1.hexdigest(File.read(defined_in_file))
    checksum = checksum.encode('UTF-8') if checksum.respond_to?(:encode)
    checksum
  end
end

#consumer(platform) ⇒ Specification::Consumer

Returns a consumer to access the multi-platform attributes.

Parameters:

  • platform (String, Symbol, Platform)

    he platform of the consumer

Returns:



318
319
320
321
# File 'lib/cocoapods-core/specification.rb', line 318

def consumer(platform)
  platform = platform.to_sym
  @consumers[platform] ||= Consumer.new(self, platform)
end

#default_subspecsArray

Returns the name of the default subspecs if provided.

Returns:

  • (Array)

    the name of the default subspecs if provided.



258
259
260
261
# File 'lib/cocoapods-core/specification.rb', line 258

def default_subspecs
  # TODO: remove singular form and update the JSON specs.
  Array(attributes_hash['default_subspecs'] || attributes_hash['default_subspec'])
end

#defined_in_fileString

Returns the path where the specification is defined, if loaded from a file.

Returns:

  • (String)

    the path where the specification is defined, if loaded from a file.



509
510
511
# File 'lib/cocoapods-core/specification.rb', line 509

def defined_in_file
  root? ? @defined_in_file : root.defined_in_file
end

#dependencies(platform = nil) ⇒ Array<Dependency>

Note:

External dependencies are inherited by subspecs

Returns the dependencies on other Pods or subspecs of other Pods.

Parameters:

  • all_platforms (Bool)

    whether the dependencies should be returned for all platforms instead of the active one.

Returns:

  • (Array<Dependency>)

    the dependencies on other Pods.



295
296
297
298
299
300
301
302
303
# File 'lib/cocoapods-core/specification.rb', line 295

def dependencies(platform = nil)
  if platform
    consumer(platform).dependencies || []
  else
    available_platforms.map do |spec_platform|
      consumer(spec_platform).dependencies
    end.flatten.uniq
  end
end

#deployment_target(platform_name) ⇒ String, Nil

Returns the deployment target for the specified platform.

Parameters:

  • platform_name (String)

    the symbolic name of the platform.

Returns:

  • (String)

    the deployment target

  • (Nil)

    if not deployment target was specified for the platform.



378
379
380
381
382
# File 'lib/cocoapods-core/specification.rb', line 378

def deployment_target(platform_name)
  result = platform_hash[platform_name.to_s]
  result ||= parent.deployment_target(platform_name) if parent
  result
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



74
75
76
# File 'lib/cocoapods-core/specification.rb', line 74

def eql?(other)
  self == other
end

#hashFixnum

Note:

This function must have the property that a.eql?(b) implies a.hash == b.hash.

Note:

This method is used by the Hash class.

Return the hash value for this specification according to its attributes hash.

Returns:

  • (Fixnum)

    The hash value.



88
89
90
# File 'lib/cocoapods-core/specification.rb', line 88

def hash
  to_s.hash
end

#inspectString

Returns A string suitable for debugging.

Returns:

  • (String)

    A string suitable for debugging.



107
108
109
# File 'lib/cocoapods-core/specification.rb', line 107

def inspect
  "#<#{self.class.name} name=#{name.inspect}>"
end

#local?Bool

Returns whether the specification should use a directory as it source.

Returns:

  • (Bool)

    whether the specification should use a directory as it source.



332
333
334
335
336
# File 'lib/cocoapods-core/specification.rb', line 332

def local?
  return true if source[:path]
  return true if source[:local]
  false
end

#module_nameString

Returns the module name of a specification

Returns:

  • (String)

    the module name



151
152
153
154
155
# File 'lib/cocoapods-core/specification.rb', line 151

def module_name
  attributes_hash['module_name'] ||
    c99ext_identifier(attributes_hash['header_dir']) ||
    c99ext_identifier(attributes_hash['name'])
end

#recursive_subspecsArray<Specifications>

Returns the recursive list of all the subspecs of a specification.

Returns:

  • (Array<Specifications>)

    the recursive list of all the subspecs of a specification.



208
209
210
211
212
213
214
215
# File 'lib/cocoapods-core/specification.rb', line 208

def recursive_subspecs
  mapper = lambda do |spec|
    spec.subspecs.map do |subspec|
      [subspec, *mapper.call(subspec)]
    end.flatten
  end
  mapper.call(self)
end

#rootSpecification

Returns The root specification or itself if it is root.

Returns:

  • (Specification)

    The root specification or itself if it is root.



183
184
185
# File 'lib/cocoapods-core/specification.rb', line 183

def root
  parent ? parent.root : self
end

#root?Bool

Returns whether the specification is root.

Returns:

  • (Bool)

    whether the specification is root.



189
190
191
# File 'lib/cocoapods-core/specification.rb', line 189

def root?
  parent.nil?
end

#store_attribute(name, value, platform_name = nil) ⇒ Object

Note:

If the provides value is Hash the keys are converted to a string.

Sets the value for the attribute with the given name.

Parameters:

  • name (Symbol)

    the name of the attribute.

  • value (Object)

    the value to store.

  • platform. (Symbol)

    If provided the attribute is stored only for the given platform.

Returns:

  • void



441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/cocoapods-core/specification.rb', line 441

def store_attribute(name, value, platform_name = nil)
  name = name.to_s
  value = convert_keys_to_string(value) if value.is_a?(Hash)
  value = value.strip_heredoc.strip if value.respond_to?(:strip_heredoc)
  if platform_name
    platform_name = platform_name.to_s
    attributes_hash[platform_name] ||= {}
    attributes_hash[platform_name][name] = value
  else
    attributes_hash[name] = value
  end
end

#subspec?Bool

Returns whether the specification is a subspec.

Returns:

  • (Bool)

    whether the specification is a subspec.



195
196
197
# File 'lib/cocoapods-core/specification.rb', line 195

def subspec?
  !parent.nil?
end

#subspec_by_name(relative_name, raise_if_missing = true) ⇒ Specification

Returns the subspec with the given name or the receiver if the name is nil or equal to the name of the receiver.

Examples:

Retrieving a subspec


s.subspec_by_name('Pod/subspec').name #=> 'subspec'

Parameters:

  • relative_name (String)

    the relative name of the subspecs starting from the receiver including the name of the receiver.

  • raise_if_missing (Boolean) (defaults to: true)

    whether an exception should be raised if no specification named ‘relative_name` is found.

Returns:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cocoapods-core/specification.rb', line 234

def subspec_by_name(relative_name, raise_if_missing = true)
  if relative_name.nil? || relative_name == base_name
    self
  elsif relative_name.downcase == base_name.downcase
    raise Informative, "Trying to access a `#{relative_name}` " \
      "specification from `#{base_name}`, which has a different case."
  else
    remainder = relative_name[base_name.size + 1..-1]
    subspec_name = remainder.split('/').shift
    subspec = subspecs.find { |s| s.base_name == subspec_name }
    unless subspec
      if raise_if_missing
        raise Informative, 'Unable to find a specification named ' \
          "`#{relative_name}` in `#{name} (#{version})`."
      else
        return nil
      end
    end
    subspec.subspec_by_name(remainder, raise_if_missing)
  end
end

#subspec_dependencies(platform = nil) ⇒ Array<Dependency>

Note:

A specification has a dependency on either the #default_subspecs or each of its children subspecs that are compatible with its platform.

Returns the dependencies on subspecs.

Returns:

  • (Array<Dependency>)

    the dependencies on subspecs.



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/cocoapods-core/specification.rb', line 271

def subspec_dependencies(platform = nil)
  if default_subspecs.empty?
    specs = subspecs.compact
  else
    specs = default_subspecs.map do |subspec_name|
      root.subspec_by_name("#{name}/#{subspec_name}")
    end
  end
  if platform
    specs = specs.select { |s| s.supported_on_platform?(platform) }
  end
  specs.map { |s| Dependency.new(s.name, version) }
end

#supported_on_platform?(platform) ⇒ Bool #supported_on_platform?(symbolic_name, deployment_target) ⇒ Bool

Returns whether the specification is supported in the given platform.

Overloads:

  • #supported_on_platform?(platform) ⇒ Bool

    Parameters:

    • platform (Platform)

      the platform which is checked for support.

  • #supported_on_platform?(symbolic_name, deployment_target) ⇒ Bool

    Parameters:

    • symbolic_name (Symbol)

      the name of the platform which is checked for support.

    • deployment_target (String)

      the deployment target which is checked for support.

Returns:

  • (Bool)

    whether the specification is supported in the given platform.



354
355
356
357
# File 'lib/cocoapods-core/specification.rb', line 354

def supported_on_platform?(*platform)
  platform = Platform.new(*platform)
  available_platforms.any? { |available| platform.supports?(available) }
end

#to_sString

Returns A string suitable for representing the specification in clients.

Returns:

  • (String)

    A string suitable for representing the specification in clients.



95
96
97
98
99
100
101
102
103
# File 'lib/cocoapods-core/specification.rb', line 95

def to_s
  if name && !version.version.empty?
    "#{name} (#{version})"
  elsif name
    name
  else
    'No-name'
  end
end

#validate_cocoapods_versionObject

Validates the cocoapods_version in the specification against the current version of Core. It will raise an Informative error if the version is not satisfied.



595
596
597
598
599
600
# File 'lib/cocoapods-core/specification.rb', line 595

def validate_cocoapods_version
  unless cocoapods_version.satisfied_by?(Version.create(CORE_VERSION))
    raise Informative, "`#{name}` requires CocoaPods version `#{cocoapods_version}`, " \
                       "which is not satisified by your current version, `#{CORE_VERSION}`."
  end
end