Class: Pod::Podfile::TargetDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/podfile/target_definition.rb

Overview

The TargetDefinition stores the information of a CocoaPods static library. The target definition can be linked with one or more targets of the user project.

Target definitions can be nested and by default inherit the dependencies of the parent.

Representations collapse

HASH_KEYS =

Returns The keys used by the hash representation of the target definition.

Returns:

  • (Array)

    The keys used by the hash representation of the target definition.

%w(
  name
  platform
  podspecs
  exclusive
  link_with
  link_with_first_target
  inhibit_warnings
  user_project_path
  build_configurations
  dependencies
  children
).freeze

Instance Attribute Summary collapse

Attributes collapse

Representations collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, internal_hash = nil) ⇒ TargetDefinition

Returns a new instance of TargetDefinition.

Parameters:

  • name (String, Symbol)

    @see name

  • parent (TargetDefinition)

    @see parent

  • options (Hash)

    a customizable set of options



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 27

def initialize(name, parent, internal_hash = nil)
  @internal_hash = internal_hash || {}
  @parent = parent
  @children = []

  unless internal_hash
    self.name = name
  end
  if parent.is_a?(TargetDefinition)
    parent.children << self
  end
end

Instance Attribute Details

#childrenArray<TargetDefinition>

Returns the children target definitions.

Returns:



42
43
44
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 42

def children
  @children
end

#parentTargetDefinition, Podfile (readonly)

Returns the parent target definition or the Podfile if the receiver is root.

Returns:



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

def parent
  @parent
end

Class Method Details

.from_hash(hash, parent) ⇒ TargetDefinition

Configures a new target definition from the given hash.

Parameters:

  • the (Hash)

    hash which contains the information of the Podfile.

Returns:



468
469
470
471
472
473
474
475
476
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 468

def self.from_hash(hash, parent)
  internal_hash = hash.dup
  children_hashes = internal_hash.delete('children') || []
  definition = TargetDefinition.new(nil, parent, internal_hash)
  children_hashes.each do |child_hash|
    TargetDefinition.from_hash(child_hash, definition)
  end
  definition
end

Instance Method Details

#build_configurationsHash{String => symbol}

Returns A hash where the keys are the name of the build configurations and the values a symbol that represents their type (‘:debug` or `:release`).

Returns:

  • (Hash{String => symbol})

    A hash where the keys are the name of the build configurations and the values a symbol that represents their type (‘:debug` or `:release`).



258
259
260
261
262
263
264
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 258

def build_configurations
  if root?
    get_hash_value('build_configurations')
  else
    get_hash_value('build_configurations') || parent.build_configurations
  end
end

#build_configurations=(hash) ⇒ Hash{String => Symbol}, void

Sets the build configurations for this target.

Returns:

  • (Hash{String => Symbol})

    hash A hash where the keys are the name of the build configurations and the values the type.

  • (void)


274
275
276
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 274

def build_configurations=(hash)
  set_hash_value('build_configurations', hash) unless hash.empty?
end

#dependenciesArray<Dependency>

Returns The list of the dependencies of the target definition including the inherited ones.

Returns:

  • (Array<Dependency>)

    The list of the dependencies of the target definition including the inherited ones.



77
78
79
80
81
82
83
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 77

def dependencies
  if exclusive? || parent.nil?
    non_inherited_dependencies
  else
    non_inherited_dependencies + parent.dependencies
  end
end

#empty?Bool

Returns Whether the target definition has at least one dependency, excluding inherited ones.

Returns:

  • (Bool)

    Whether the target definition has at least one dependency, excluding inherited ones.



95
96
97
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 95

def empty?
  non_inherited_dependencies.empty?
end

#exclusive=(flag) ⇒ void

This method returns an undefined value.

Sets whether the target definition is exclusive.

Parameters:

  • flag (Bool)

    Whether the definition is exclusive.



174
175
176
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 174

def exclusive=(flag)
  set_hash_value('exclusive', flag)
end

#exclusive?Bool

Note:

A target is always ‘exclusive` if it is root.

Note:

A target is always ‘exclusive` if the `platform` does not match the parent’s ‘platform`.

Returns whether the target definition should inherit the dependencies of the parent.

Returns:

  • (Bool)

    whether is exclusive.



157
158
159
160
161
162
163
164
165
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 157

def exclusive?
  if root?
    true
  elsif get_hash_value('exclusive')
    true
  else
    platform && parent && parent.platform != platform
  end
end

#inhibit_all_warnings=(flag) ⇒ void

This method returns an undefined value.

Sets whether the target definition should inhibit the warnings during compilation for all pods.

Parameters:

  • flag (Bool)

    Whether the warnings should be suppressed.



303
304
305
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 303

def inhibit_all_warnings=(flag)
  inhibit_warnings_hash['all'] = flag
end

#inhibit_warnings_for_pod(pod_name) ⇒ void

This method returns an undefined value.

Inhibits warnings for a specific pod during compilation.

Parameters:

  • pod (String)

    name Whether the warnings should be suppressed.



314
315
316
317
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 314

def inhibit_warnings_for_pod(pod_name)
  inhibit_warnings_hash['for_pods'] ||= []
  inhibit_warnings_hash['for_pods'] << pod_name
end

#inhibits_warnings_for_pod?(pod_name) ⇒ Bool

Whether the target definition should inhibit warnings for a single pod. If inhibit_all_warnings is true, it will return true for any asked pod.

Returns:

  • (Bool)

    whether the target definition should inhibit warnings for a single pod. If inhibit_all_warnings is true, it will return true for any asked pod.



284
285
286
287
288
289
290
291
292
293
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 284

def inhibits_warnings_for_pod?(pod_name)
  if inhibit_warnings_hash['all']
    true
  elsif !root? && parent.inhibits_warnings_for_pod?(pod_name)
    true
  else
    inhibit_warnings_hash['for_pods'] ||= []
    inhibit_warnings_hash['for_pods'].include? pod_name
  end
end

#inspectString

Returns A string representation suitable for debug.

Returns:

  • (String)

    A string representation suitable for debug.



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

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

#labelString Also known as: to_s

Returns The label of the target definition according to its name.

Returns:

  • (String)

    The label of the target definition according to its name.



102
103
104
105
106
107
108
109
110
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 102

def label
  if root? && name == "Pods"
    "Pods"
  elsif exclusive? || parent.nil?
    "Pods-#{name}"
  else
    "#{parent.label}-#{name}"
  end
end

Returns The list of the names of the Xcode targets with which this target definition should be linked with.

Returns:

  • (Array<String>)

    The list of the names of the Xcode targets with which this target definition should be linked with.



183
184
185
186
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 183

def link_with
  value = get_hash_value('link_with')
  value unless value.nil? || value.empty?
end

This method returns an undefined value.

Sets the client targets that should be integrated by this definition.

Parameters:

  • targets (Array<String>)

    The list of the targets names.



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

def link_with=(targets)
  set_hash_value('link_with', Array(targets).map(&:to_s))
end
Note:

This option is ignored if #link_with is set.

This method returns an undefined value.

Sets whether the target definition should link with the first target of the project.

Parameters:

  • flag (Bool)

    Whether the definition should link with the first target.



222
223
224
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 222

def link_with_first_target=(flag)
  set_hash_value('link_with_first_target', flag)
end
Note:

This option is ignored if #link_with is set.

Returns whether the target definition should link with the first target of the project.

Returns:

  • (Bool)

    whether is exclusive.



208
209
210
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 208

def link_with_first_target?
  get_hash_value('link_with_first_target') unless link_with
end

#nameString

Returns the path of the project this target definition should link with.

Returns:

  • (String)

    the path of the project this target definition should link with.



129
130
131
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 129

def name
  get_hash_value('name')
end

#name=(name) ⇒ void

This method returns an undefined value.

Sets the path of the user project this target definition should link with.

Parameters:

  • path (String)

    The path of the project.



141
142
143
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 141

def name=(name)
  set_hash_value('name', name)
end

#non_inherited_dependenciesArray

Returns The list of the dependencies of the target definition, excluding inherited ones.

Returns:

  • (Array)

    The list of the dependencies of the target definition, excluding inherited ones.



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

def non_inherited_dependencies
  pod_dependencies.concat(podspec_dependencies)
end

#platformPlatform

Note:

If no deployment target has been specified a default value is provided.

Returns the platform of the target definition.

Returns:

  • (Platform)

    the platform of the target definition.



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

def platform
  name_or_hash = get_hash_value('platform')
  if name_or_hash
    if name_or_hash.is_a?(Hash)
      name = name_or_hash.keys.first.to_sym
      target = name_or_hash.values.first
    else
      name = name_or_hash.to_sym
    end
    target ||= (name == :ios ? '4.3' : '10.6')
    Platform.new(name, target)
  else
    parent.platform unless root?
  end
end

#podfilePodfile

Returns The podfile that contains the specification for this target definition.

Returns:

  • (Podfile)

    The podfile that contains the specification for this target definition.



70
71
72
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 70

def podfile
  root.parent
end

#recursive_childrenArray<TargetDefinition>

Returns the targets definition descending from this one.

Returns:

  • (Array<TargetDefinition>)

    the targets definition descending from this one.



47
48
49
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 47

def recursive_children
  (children + children.map(&:recursive_children)).flatten
end

#rootTargetDefinition

Returns The root target definition.

Returns:



59
60
61
62
63
64
65
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 59

def root
  if root?
    self
  else
    parent.root
  end
end

#root?Bool

Returns Whether the target definition is root.

Returns:

  • (Bool)

    Whether the target definition is root.



53
54
55
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 53

def root?
  parent.is_a?(Podfile) || parent.nil?
end

#set_platform(name, target = nil) ⇒ void

This method returns an undefined value.

Sets the platform of the target definition.

Parameters:

  • name (Symbol)

    The name of the platform.

  • target (String) (defaults to: nil)

    The deployment target of the platform.

Raises:

  • When the name of the platform is unsupported.



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 354

def set_platform(name, target = nil)
  unless [:ios, :osx].include?(name)
    raise StandardError, "Unsupported platform `#{name}`. Platform " \
      "must be `:ios` or `:osx`."
  end

  if target
    value = { name.to_s => target }
  else
    value = name.to_s
  end
  set_hash_value('platform', value)
end

#store_pod(name, *requirements) ⇒ void

TODO:

This needs urgently a rename.

Note:

The dependencies are stored as an array. To simplify the YAML representation if they have requirements they are represented as a Hash, otherwise only the String of the name is added to the array.

This method returns an undefined value.

Stores the dependency for a Pod with the given name.

Parameters:

  • name (String)

    The name of the Pod

  • requirements (Array<String, Hash>)

    The requirements and the options of the dependency.



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 387

def store_pod(name, *requirements)
  parse_inhibit_warnings(name, requirements)

  if requirements && !requirements.empty?
    pod = { name => requirements }
  else
    pod = name
  end

  get_hash_value('dependencies', []) << pod
end

#store_podspec(options = nil) ⇒ void

TODO:

This needs urgently a rename.

Note:

The storage of this information is optimized for YAML readability.

This method returns an undefined value.

Stores the podspec whose dependencies should be included by the target.

Parameters:

  • options (Hash) (defaults to: nil)

    The options used to find the podspec (either by name or by path). If nil the podspec is auto-detected (i.e. the first one in the folder of the Podfile)



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 416

def store_podspec(options = nil)
  if options
    unless options.keys.all? { |key| [:name, :path].include?(key) }
      raise StandardError, "Unrecognized options for the podspec " \
        "method `#{options}`"
    end
    get_hash_value('podspecs', []) << options
  else
    get_hash_value('podspecs', []) << { :autodetect => true }
  end
end

#to_hashHash

Returns The hash representation of the target definition.

Returns:

  • (Hash)

    The hash representation of the target definition.



453
454
455
456
457
458
459
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 453

def to_hash
  hash = internal_hash.dup
  unless children.empty?
    hash['children'] = children.map(&:to_hash)
  end
  hash
end

#user_project_pathString

Returns the path of the project this target definition should link with.

Returns:

  • (String)

    the path of the project this target definition should link with.



231
232
233
234
235
236
237
238
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 231

def user_project_path
  path = get_hash_value('user_project_path')
  if path
    File.extname(path) == '.xcodeproj' ? path : "#{path}.xcodeproj"
  else
    parent.user_project_path unless root?
  end
end

#user_project_path=(path) ⇒ void

This method returns an undefined value.

Sets the path of the user project this target definition should link with.

Parameters:

  • path (String)

    The path of the project.



248
249
250
# File 'lib/cocoapods-core/podfile/target_definition.rb', line 248

def user_project_path=(path)
  set_hash_value('user_project_path', path)
end