Class: Pod::Podfile

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

Overview

The Podfile is a specification that describes the dependencies of the targets of an Xcode project.

It supports its own DSL and generally is stored in files named CocoaPods.podfile or Podfile.

The Podfile creates a hierarchy of target definitions that that store the information of necessary to generate the CocoaPods libraries.

Defined Under Namespace

Modules: DSL Classes: StandardError, TargetDefinition

Representations collapse

HASH_KEYS =

Returns The keys used by the hash representation of the Podfile.

Returns:

  • (Array)

    The keys used by the hash representation of the Podfile.

%w(
  target_definitions
  workspace
  sources
  generate_bridge_support
  set_arc_compatibility_flag
  plugins
).freeze

Working with a podfile collapse

Private helpers collapse

Instance Attribute Summary collapse

Working with a podfile collapse

Attributes collapse

Hooks collapse

Representations collapse

Class methods collapse

Private helpers collapse

Deprecations collapse

Instance Method Summary collapse

Methods included from DSL

#generate_bridge_support!, #inhibit_all_warnings!, #link_with, #platform, #plugin, #pod, #podspec, #post_install, #pre_install, #set_arc_compatibility_flag!, #source, #target, #use_frameworks!, #workspace, #xcodeproj

Constructor Details

#initialize(defined_in_file = nil, internal_hash = {}, &block) ⇒ Podfile

Returns a new instance of Podfile

Examples:

Creating a Podfile.


platform :ios, "6.0"
target :my_app do
  pod "AFNetworking", "~> 1.0"
end

Parameters:

  • defined_in_file (Pathname) (defaults to: nil)

    the path of the podfile.

  • block (Proc)

    an optional block that configures the podfile through the DSL.


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cocoapods-core/podfile.rb', line 43

def initialize(defined_in_file = nil, internal_hash = {}, &block)
  self.defined_in_file = defined_in_file
  @internal_hash = internal_hash
  if block
    default_target_def = TargetDefinition.new('Pods', self)
    default_target_def.link_with_first_target = true
    @root_target_definitions = [default_target_def]
    @current_target_definition = default_target_def
    instance_eval(&block)
  else
    @root_target_definitions = []
  end
end

Instance Attribute Details

#current_target_definitionTargetDefinition (private)

Returns The current target definition to which the DSL commands apply.

Returns:

  • (TargetDefinition)

    The current target definition to which the DSL commands apply.


366
367
368
# File 'lib/cocoapods-core/podfile.rb', line 366

def current_target_definition
  @current_target_definition
end

#defined_in_filePathname

Returns the path where the podfile was loaded from. It is nil if the podfile was generated programmatically.

Returns:

  • (Pathname)

    the path where the podfile was loaded from. It is nil if the podfile was generated programmatically.


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

def defined_in_file
  @defined_in_file
end

#internal_hashHash (private)

Returns The hash which store the attributes of the Podfile.

Returns:

  • (Hash)

    The hash which store the attributes of the Podfile.


326
327
328
# File 'lib/cocoapods-core/podfile.rb', line 326

def internal_hash
  @internal_hash
end

#root_target_definitionsArray<TargetDefinition>

Returns The root target definition.

Returns:


83
84
85
# File 'lib/cocoapods-core/podfile.rb', line 83

def root_target_definitions
  @root_target_definitions
end

Class Method Details

.from_file(path) ⇒ Podfile

Initializes a podfile from the file with the given path.

Parameters:

  • path (Pathname)

    the path from where the podfile should be loaded.

Returns:

  • (Podfile)

    the generated podfile.


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cocoapods-core/podfile.rb', line 219

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

  case path.extname
  when '', '.podfile'
    Podfile.from_ruby(path)
  when '.yaml'
    Podfile.from_yaml(path)
  else
    raise Informative, "Unsupported Podfile format `#{path}`."
  end
end

.from_hash(hash, path = nil) ⇒ Podfile

Configures a new Podfile from the given hash.

Parameters:

  • hash (Hash)

    The hash which contains the information of the Podfile.

  • path (Pathname) (defaults to: nil)

    The path from which the Podfile is loaded.

Returns:


307
308
309
310
311
312
313
314
315
316
# File 'lib/cocoapods-core/podfile.rb', line 307

def self.from_hash(hash, path = nil)
  internal_hash = hash.dup
  target_definitions = internal_hash.delete('target_definitions') || []
  podfile = Podfile.new(path, internal_hash)
  target_definitions.each do |definition_hash|
    definition = TargetDefinition.from_hash(definition_hash, podfile)
    podfile.root_target_definitions << definition
  end
  podfile
end

.from_ruby(path, contents = nil) ⇒ Podfile

Configures a new Podfile from the given ruby string.

Parameters:

  • path (Pathname)

    The path from which the Podfile is loaded.

  • contents (String) (defaults to: nil)

    The ruby string which will configure the podfile with the DSL.

Returns:


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/cocoapods-core/podfile.rb', line 245

def self.from_ruby(path, contents = nil)
  contents ||= File.open(path, 'r:utf-8') { |f| f.read }

  # Work around for Rubinius incomplete encoding in 1.9 mode
  if contents.respond_to?(:encoding) && contents.encoding.name != 'UTF-8'
    contents.encode!('UTF-8')
  end

  if contents.tr!('“”‘’‛', %(""'''))
    # Changes have been made
    CoreUI.warn "Your #{path.basename} has had smart quotes sanitised. " \
                'To avoid issues in the future, you should not use ' \
                'TextEdit for editing it. If you are not using TextEdit, ' \
                'you should turn off smart quotes in your editor of choice.'
  end

  podfile = Podfile.new(path) do
    # rubocop:disable Lint/RescueException
    begin
      # rubocop:disable Eval
      eval(contents, nil, path.to_s)
      # rubocop:enable Eval
    rescue Exception => e
      message = "Invalid `#{path.basename}` file: #{e.message}"
      raise DSLError.new(message, path, e.backtrace, contents)
    end
    # rubocop:enable Lint/RescueException
  end
  podfile
end

.from_yaml(path) ⇒ Podfile

Configures a new Podfile from the given YAML representation.

Parameters:

  • yaml (String)

    The YAML encoded hash which contains the information of the Podfile.

  • path (Pathname)

    The path from which the Podfile is loaded.

Returns:


287
288
289
290
291
292
293
294
295
# File 'lib/cocoapods-core/podfile.rb', line 287

def self.from_yaml(path)
  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
  hash = YAMLHelper.load_string(string)
  from_hash(hash, path)
end

Instance Method Details

#dependenciesArray<Dependency>

Returns the dependencies of the all the target definitions.

Returns:

  • (Array<Dependency>)

    the dependencies of the all the target definitions.


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

def dependencies
  target_definition_list.map(&:dependencies).flatten.uniq
end

#dependency(name = nil, *requirements, &block) ⇒ Object

Deprecated.

Deprecated in favour of the more succinct Pod::Podfile::DSL#pod. Remove for CocoaPods 1.0.


376
377
378
379
# File 'lib/cocoapods-core/podfile.rb', line 376

def dependency(name = nil, *requirements, &block)
  CoreUI.warn "[DEPRECATED] `dependency' is deprecated (use `pod')"
  pod(name, *requirements, &block)
end

#generate_bridge_support?Bool

Returns whether the podfile should generate a BridgeSupport metadata document.

Returns:

  • (Bool)

    whether the podfile should generate a BridgeSupport metadata document.


126
127
128
# File 'lib/cocoapods-core/podfile.rb', line 126

def generate_bridge_support?
  get_hash_value('generate_bridge_support')
end

#get_hash_value(key) ⇒ Object (private)

Returns the value for the given key in the internal hash of the Podfile.

Parameters:

  • key (String)

    The key for which the value is needed.

Returns:

  • (Object)

    The value for the key.

Raises:

  • If the key is not recognized.


356
357
358
359
360
361
# File 'lib/cocoapods-core/podfile.rb', line 356

def get_hash_value(key)
  unless HASH_KEYS.include?(key)
    raise StandardError, "Unsupported hash key `#{key}`"
  end
  internal_hash[key]
end

#pluginsHash<String, Hash>

Returns The plugins, keyed by name.

Returns:

  • (Hash<String, Hash>)

    The plugins, keyed by name.


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

def plugins
  get_hash_value('plugins') || {}
end

#post_install!(installer) ⇒ Bool

Calls the post install callback if defined.

Parameters:

  • installer (Pod::Installer)

    the installer that is performing the installation.

Returns:

  • (Bool)

    whether a post install callback was specified and it was called.


168
169
170
171
172
173
174
175
# File 'lib/cocoapods-core/podfile.rb', line 168

def post_install!(installer)
  if @post_install_callback
    @post_install_callback.call(installer)
    true
  else
    false
  end
end

#pre_install!(installer) ⇒ Bool

Calls the pre install callback if defined.

Parameters:

  • installer (Pod::Installer)

    the installer that is performing the installation.

Returns:

  • (Bool)

    whether a pre install callback was specified and it was called.


151
152
153
154
155
156
157
158
# File 'lib/cocoapods-core/podfile.rb', line 151

def pre_install!(installer)
  if @pre_install_callback
    @pre_install_callback.call(installer)
    true
  else
    false
  end
end

#set_arc_compatibility_flag?Bool

Returns whether the -fobjc-arc flag should be added to the OTHER_LD_FLAGS.

Returns:

  • (Bool)

    whether the -fobjc-arc flag should be added to the OTHER_LD_FLAGS.


133
134
135
# File 'lib/cocoapods-core/podfile.rb', line 133

def set_arc_compatibility_flag?
  get_hash_value('set_arc_compatibility_flag')
end

#set_hash_value(key, value) ⇒ void (private)

This method returns an undefined value.

Set a value in the internal hash of the Podfile for the given key.

Parameters:

  • key (String)

    The key for which to store the value.

  • value (Object)

    The value to store.

Raises:

  • If the key is not recognized.


340
341
342
343
344
345
# File 'lib/cocoapods-core/podfile.rb', line 340

def set_hash_value(key, value)
  unless HASH_KEYS.include?(key)
    raise StandardError, "Unsupported hash key `#{key}`"
  end
  internal_hash[key] = value
end

#sourcesArray<String>

Returns The name of the sources.

Returns:

  • (Array<String>)

    The name of the sources.


100
101
102
# File 'lib/cocoapods-core/podfile.rb', line 100

def sources
  get_hash_value('sources') || []
end

#target_definition_listObject


77
78
79
# File 'lib/cocoapods-core/podfile.rb', line 77

def target_definition_list
  root_target_definitions.map { |td| [td, td.recursive_children] }.flatten
end

#target_definitionsHash{Symbol,String => TargetDefinition}

Returns the target definitions of the podfile stored by their name.

Returns:

  • (Hash{Symbol,String => TargetDefinition})

    the target definitions of the podfile stored by their name.


73
74
75
# File 'lib/cocoapods-core/podfile.rb', line 73

def target_definitions
  Hash[target_definition_list.map { |td| [td.name, td] }]
end

#to_hashHash

Returns The hash representation of the Podfile.

Returns:

  • (Hash)

    The hash representation of the Podfile.


196
197
198
199
200
201
# File 'lib/cocoapods-core/podfile.rb', line 196

def to_hash
  hash = {}
  hash['target_definitions'] = root_target_definitions.map(&:to_hash)
  hash.merge!(internal_hash)
  hash
end

#to_sString

Returns a string useful to represent the Podfile in a message presented to the user.

Returns:

  • (String)

    a string useful to represent the Podfile in a message presented to the user.


60
61
62
# File 'lib/cocoapods-core/podfile.rb', line 60

def to_s
  'Podfile'
end

#to_yamlString

Returns The YAML representation of the Podfile.

Returns:

  • (String)

    The YAML representation of the Podfile.


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

def to_yaml
  to_hash.to_yaml
end

#workspace_pathString

Returns the path of the workspace if specified by the user.

Returns:

  • (String)

    the path of the workspace if specified by the user.


112
113
114
115
116
117
118
119
120
121
# File 'lib/cocoapods-core/podfile.rb', line 112

def workspace_path
  path = get_hash_value('workspace')
  if path
    if File.extname(path) == '.xcworkspace'
      path
    else
      "#{path}.xcworkspace"
    end
  end
end