Class: Pod::Specification::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/specification/consumer.rb

Overview

Allows to conveniently access a Specification programmatically.

It takes care of:

  • standardizing the attributes

  • handling multi-platform values

  • handle default values

  • handle automatic container wrapping of values

  • handle inherited values

This class allows to store the values of the attributes in the Specification as specified in the DSL. The benefits is reduced reliance on meta programming to access the attributes and the possibility of serializing a specification back exactly as defined in a file.

Instance Attribute Summary collapse

Regular attributes collapse

Test Support collapse

File patterns collapse

Preparing Values collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, platform) ⇒ Consumer

Returns a new instance of Consumer.

Parameters:

  • spec (Specification)

    @see spec

  • platform (Symbol, Platform)

    The platform for which the specification needs to be consumed.



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

def initialize(spec, platform)
  @spec = spec
  @platform_name = platform.is_a?(Symbol) ? platform : platform.name

  unless spec.supported_on_platform?(platform)
    raise StandardError, "#{self} is not compatible with #{platform}."
  end
end

Instance Attribute Details

#platform_nameSymbol (readonly)

Returns The name of the platform for which the specification needs to be consumed.

Returns:

  • (Symbol)

    The name of the platform for which the specification needs to be consumed.



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

def platform_name
  @platform_name
end

#specSpecification (readonly)

Returns The specification to consume.

Returns:



23
24
25
# File 'lib/cocoapods-core/specification/consumer.rb', line 23

def spec
  @spec
end

Class Method Details

.spec_attr_accessor(name) ⇒ Object

Creates a method to access the contents of the attribute.

Parameters:

  • name (Symbol)

    the name of the attribute.



51
52
53
54
55
# File 'lib/cocoapods-core/specification/consumer.rb', line 51

def self.spec_attr_accessor(name)
  define_method(name) do
    value_for_attribute(name)
  end
end

Instance Method Details

#_prepare_prefix_header_contents(value) ⇒ String

Converts the prefix header to a string if specified as an array.

Parameters:

  • value. (String, Array)

    The value of the attribute as specified by the user.

Returns:

  • (String)

    the prefix header.



372
373
374
375
376
377
# File 'lib/cocoapods-core/specification/consumer.rb', line 372

def _prepare_prefix_header_contents(value)
  if value
    value = value.join("\n") if value.is_a?(Array)
    value.strip_heredoc.chomp
  end
end

#_prepare_resource_bundles(value) ⇒ Hash

Ensures that the file patterns of the resource bundles are contained in an array.

Parameters:

  • value. (String, Array, Hash)

    The value of the attribute as specified by the user.

Returns:

  • (Hash)

    the resources.



434
435
436
437
438
439
440
441
442
# File 'lib/cocoapods-core/specification/consumer.rb', line 434

def _prepare_resource_bundles(value)
  result = {}
  if value
    value.each do |key, patterns|
      result[key] = [*patterns].compact
    end
  end
  result
end

#_prepare_scheme(value) ⇒ Hash

Converts the a scheme where keys are strings into symbols.

Parameters:

  • value. (Hash)

    The value of the attribute as specified by the user.

Returns:

  • (Hash)

    the scheme with symbols as keys instead of strings or ‘nil` if the value is not a hash.



422
423
424
# File 'lib/cocoapods-core/specification/consumer.rb', line 422

def _prepare_scheme(value)
  Specification.convert_keys_to_symbol(value, :recursive => false) if value && value.is_a?(Hash)
end

#_prepare_script_phases(value) ⇒ Array<Hash{Symbol=>String}>

Converts the array of hashes (script phases) where keys are strings into symbols.

Parameters:

  • value. (Array<Hash{String=>String}>)

    The value of the attribute as specified by the user.

Returns:

  • (Array<Hash{Symbol=>String}>)

    the script phases array with symbols for each hash instead of strings.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/cocoapods-core/specification/consumer.rb', line 399

def _prepare_script_phases(value)
  if value
    value.map do |script_phase|
      if script_phase.is_a?(Hash)
        phase = Specification.convert_keys_to_symbol(script_phase)
        phase[:execution_position] = if phase.key?(:execution_position)
                                       phase[:execution_position].to_sym
                                     else
                                       :any
                                     end
        phase
      end
    end.compact
  end
end

#_prepare_test_type(value) ⇒ Symbol

Converts the test type value from a string to a symbol.

Parameters:

  • value. (String, Symbol)

    The value of the test type attributed as specified by the user.

Returns:

  • (Symbol)

    the test type as a symbol.



386
387
388
389
390
# File 'lib/cocoapods-core/specification/consumer.rb', line 386

def _prepare_test_type(value)
  if value
    value.to_sym
  end
end

#compiler_flagsArray<String>

Returns the list of compiler flags needed by the specification files.

Returns:

  • (Array<String>)

    the list of compiler flags needed by the specification files.



95
# File 'lib/cocoapods-core/specification/consumer.rb', line 95

spec_attr_accessor :compiler_flags

#dependenciesArray<Dependency>

Returns the dependencies on other Pods.

Returns:

  • (Array<Dependency>)

    the dependencies on other Pods.



212
213
214
215
216
217
# File 'lib/cocoapods-core/specification/consumer.rb', line 212

def dependencies
  value = value_for_attribute(:dependencies)
  value.map do |name, requirements|
    Dependency.new(name, requirements)
  end
end

#exclude_filesArray<String>

Returns The file patterns that the Pod should ignore.

Returns:

  • (Array<String>)

    The file patterns that the Pod should ignore.



201
# File 'lib/cocoapods-core/specification/consumer.rb', line 201

spec_attr_accessor :exclude_files

#frameworksArray<String>

Returns A list of frameworks that the user’s target needs to link against.

Returns:

  • (Array<String>)

    A list of frameworks that the user’s target needs to link against



80
# File 'lib/cocoapods-core/specification/consumer.rb', line 80

spec_attr_accessor :frameworks

#header_dirString

Returns the headers directory.

Returns:

  • (String)

    the headers directory.



131
# File 'lib/cocoapods-core/specification/consumer.rb', line 131

spec_attr_accessor :header_dir

#header_mappings_dirString

Returns the directory from where to preserve the headers namespacing.

Returns:

  • (String)

    the directory from where to preserve the headers namespacing.



136
# File 'lib/cocoapods-core/specification/consumer.rb', line 136

spec_attr_accessor :header_mappings_dir

#librariesArray<String>

Returns A list of libraries that the user’s target needs to link against.

Returns:

  • (Array<String>)

    A list of libraries that the user’s target needs to link against



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

spec_attr_accessor :libraries

#merge_values(attr, existing_value, new_value) ⇒ String, ...

Merges the values of an attribute, either because the attribute is multi platform or because it is inherited.

Parameters:

  • attr (Specification::DSL::Attribute)

    the attribute for which that value is needed.

  • existing_value (String, Array, Hash)

    the current value (the value of the parent or non-multiplatform value).

  • new_value (String, Array, Hash)

    the value to append (the value of the spec or the multi-platform value).

Returns:

  • (String, Array, Hash)

    The merged value.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cocoapods-core/specification/consumer.rb', line 301

def merge_values(attr, existing_value, new_value)
  return existing_value if new_value.nil?
  return new_value if existing_value.nil?

  if attr.types.include?(TrueClass)
    new_value.nil? ? existing_value : new_value
  elsif attr.container == Array
    r = [*existing_value] + [*new_value]
    r.compact
  elsif attr.container == Hash
    existing_value.merge(new_value) do |_, old, new|
      if new.is_a?(Array) || old.is_a?(Array)
        r = [*old] + [*new]
        r.compact
      else
        old + ' ' + new
      end
    end
  else
    new_value
  end
end

#module_mapString

Returns the path of the module map file.

Returns:

  • (String)

    the path of the module map file.



127
# File 'lib/cocoapods-core/specification/consumer.rb', line 127

spec_attr_accessor :module_map

#module_nameString

Returns the module name.

Returns:

  • (String)

    the module name.



123
# File 'lib/cocoapods-core/specification/consumer.rb', line 123

spec_attr_accessor :module_name

#nameString

Returns The name of the specification.

Returns:

  • (String)

    The name of the specification.



69
# File 'lib/cocoapods-core/specification/consumer.rb', line 69

spec_attr_accessor :name

#pod_target_xcconfigHash{String => String}

Returns the xcconfig flags for the current specification for the pod target.

Returns:

  • (Hash{String => String})

    the xcconfig flags for the current specification for the pod target.



100
101
102
103
# File 'lib/cocoapods-core/specification/consumer.rb', line 100

def pod_target_xcconfig
  attr = Specification::DSL.attributes[:pod_target_xcconfig]
  merge_values(attr, value_for_attribute(:xcconfig), value_for_attribute(:pod_target_xcconfig))
end

#prefix_header_contentsString

Returns The contents of the prefix header.

Returns:

  • (String)

    The contents of the prefix header.



115
# File 'lib/cocoapods-core/specification/consumer.rb', line 115

spec_attr_accessor :prefix_header_contents

#prefix_header_fileString

Returns The path of the prefix header file.

Returns:

  • (String)

    The path of the prefix header file.



119
# File 'lib/cocoapods-core/specification/consumer.rb', line 119

spec_attr_accessor :prefix_header_file

#prepare_hook_name(attr) ⇒ String

Note:

The hook is called after the value has been wrapped in an array (if needed according to the container) but before validation.

Returns the name of the prepare hook for this attribute.

Returns:

  • (String)

    the name of the prepare hook for this attribute.



361
362
363
# File 'lib/cocoapods-core/specification/consumer.rb', line 361

def prepare_hook_name(attr)
  "_prepare_#{attr.name}"
end

#prepare_value(attr, value) ⇒ Object

Note:

Only array containers are wrapped. To automatically wrap values for attributes with hash containers a prepare hook should be used.

Wraps a value in an Array if needed and calls the prepare hook to allow further customization of a value before storing it in the instance variable.

Returns:

  • (Object)

    the customized value of the original one if no prepare hook was defined.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/cocoapods-core/specification/consumer.rb', line 335

def prepare_value(attr, value)
  if attr.container == Array
    value = if value.is_a?(Hash)
              [value]
            else
              [*value].compact
            end
  end

  hook_name = prepare_hook_name(attr)
  if self.respond_to?(hook_name, true)
    send(hook_name, value)
  else
    value
  end
end

#preserve_pathsArray<String>

Returns The paths that should be not cleaned.

Returns:

  • (Array<String>)

    The paths that should be not cleaned.



206
# File 'lib/cocoapods-core/specification/consumer.rb', line 206

spec_attr_accessor :preserve_paths

#private_header_filesArray<String>

Returns the private headers of the Pod.

Returns:

  • (Array<String>)

    the private headers of the Pod.



165
# File 'lib/cocoapods-core/specification/consumer.rb', line 165

spec_attr_accessor :private_header_files

#public_header_filesArray<String>

Returns the public headers of the Pod.

Returns:

  • (Array<String>)

    the public headers of the Pod.



161
# File 'lib/cocoapods-core/specification/consumer.rb', line 161

spec_attr_accessor :public_header_files

#raw_value_for_attribute(the_spec, attr) ⇒ String, ...

Returns the value of a given attribute taking into account multi platform values.

Parameters:

Returns:

  • (String, Array, Hash)

    The value for an attribute.



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

def raw_value_for_attribute(the_spec, attr)
  value = the_spec.attributes_hash[attr.name.to_s]
  value = prepare_value(attr, value)

  if attr.multi_platform?
    if platform_hash = the_spec.attributes_hash[platform_name.to_s]
      platform_value = platform_hash[attr.name.to_s]
      platform_value = prepare_value(attr, platform_value)
      value = merge_values(attr, value, platform_value)
    end
  end
  value
end

#requires_app_hostBool Also known as: requires_app_host?

Returns Whether this test specification requires an app host.

Returns:

  • (Bool)

    Whether this test specification requires an app host.



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

spec_attr_accessor :requires_app_host

#requires_arcBool Also known as: requires_arc?

Returns Whether the source files of the specification require to be compiled with ARC.

Returns:

  • (Bool)

    Whether the source files of the specification require to be compiled with ARC.



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

spec_attr_accessor :requires_arc

#resource_bundlesHash{String=>String}

Returns ] hash where the keys are the names of the resource bundles and the values are their relative file patterns.

Returns:

  • (Hash{String=>String})

    ] hash where the keys are the names of the resource bundles and the values are their relative file patterns.



181
# File 'lib/cocoapods-core/specification/consumer.rb', line 181

spec_attr_accessor :resource_bundles

#resourcesArray<String>

Returns A hash where the key represents the paths of the resources to copy and the values the paths of the resources that should be copied.

Returns:

  • (Array<String>)

    A hash where the key represents the paths of the resources to copy and the values the paths of the resources that should be copied.



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

spec_attr_accessor :resources

#schemeHash

Returns A hash that contains the scheme configuration.

Returns:

  • (Hash)

    A hash that contains the scheme configuration.



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

spec_attr_accessor :scheme

#script_phasesArray<Hash{Symbol=>String}>

Returns An array of hashes where each hash represents a script phase.

Returns:

  • (Array<Hash{Symbol=>String}>)

    An array of hashes where each hash represents a script phase.



186
# File 'lib/cocoapods-core/specification/consumer.rb', line 186

spec_attr_accessor :script_phases

#source_filesArray<String>

Returns the source files of the Pod.

Returns:

  • (Array<String>)

    the source files of the Pod.



157
# File 'lib/cocoapods-core/specification/consumer.rb', line 157

spec_attr_accessor :source_files

#test_typeSymbol

Returns the test type supported by this specification.

Returns:

  • (Symbol)

    the test type supported by this specification.



149
# File 'lib/cocoapods-core/specification/consumer.rb', line 149

spec_attr_accessor :test_type

#user_target_xcconfigHash{String => String}

Returns the xcconfig flags for the current specification for the user target.

Returns:

  • (Hash{String => String})

    the xcconfig flags for the current specification for the user target.



108
109
110
111
# File 'lib/cocoapods-core/specification/consumer.rb', line 108

def user_target_xcconfig
  attr = Specification::DSL.attributes[:user_target_xcconfig]
  merge_values(attr, value_for_attribute(:xcconfig), value_for_attribute(:user_target_xcconfig))
end

#value_for_attribute(attr_name) ⇒ String, ...

Returns the value for the attribute with the given name for the specification. It takes into account inheritance, multi-platform attributes and default values.

Parameters:

  • attr_name (Symbol)

    The name of the attribute.

Returns:

  • (String, Array, Hash)

    the value for the attribute.



232
233
234
235
236
237
238
# File 'lib/cocoapods-core/specification/consumer.rb', line 232

def value_for_attribute(attr_name)
  attr = Specification::DSL.attributes[attr_name]
  value = value_with_inheritance(spec, attr)
  value = attr.default(platform_name) if value.nil?
  value = attr.container.new if value.nil? && attr.container
  value
end

#value_with_inheritance(the_spec, attr) ⇒ String, ...

Returns the value of a given attribute taking into account inheritance.

Parameters:

Returns:

  • (String, Array, Hash)

    the value for the attribute.



250
251
252
253
254
255
256
257
258
# File 'lib/cocoapods-core/specification/consumer.rb', line 250

def value_with_inheritance(the_spec, attr)
  value = raw_value_for_attribute(the_spec, attr)
  if the_spec.root? || !attr.inherited?
    return value
  end

  parent_value = value_with_inheritance(the_spec.parent, attr)
  merge_values(attr, parent_value, value)
end

#vendored_frameworksArray<String>

Returns The paths of the framework bundles shipped with the Pod.

Returns:

  • (Array<String>)

    The paths of the framework bundles shipped with the Pod.



170
# File 'lib/cocoapods-core/specification/consumer.rb', line 170

spec_attr_accessor :vendored_frameworks

#vendored_librariesArray<String>

Returns The paths of the libraries shipped with the Pod.

Returns:

  • (Array<String>)

    The paths of the libraries shipped with the Pod.



175
# File 'lib/cocoapods-core/specification/consumer.rb', line 175

spec_attr_accessor :vendored_libraries

#weak_frameworksArray<String>

Returns A list of frameworks that the user’s target needs to weakly link against.

Returns:

  • (Array<String>)

    A list of frameworks that the user’s target needs to weakly link against



85
# File 'lib/cocoapods-core/specification/consumer.rb', line 85

spec_attr_accessor :weak_frameworks