Class: Pod::Project

Inherits:
Xcodeproj::Project
  • Object
show all
Defined in:
lib/cocoapods/project.rb

Overview

The Pods project.

Model class which provides helpers for working with the Pods project through the installation process.

Constant Summary

LEGACY_BUILD_ROOT =

-------------------------------------------------------------------------#

'${SRCROOT}/../build'
SPEC_SUBGROUPS =

Returns The names of the specification subgroups by key.

Returns:

  • (Hash)

    The names of the specification subgroups by key.

{
  :resources  => 'Resources',
  :frameworks => 'Frameworks',
}

Private helpers collapse

Instance Attribute Summary collapse

Legacy Xcode build root collapse

Pod Groups collapse

File references collapse

Private helpers collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, skip_initialization = false, object_version = Xcodeproj::Constants::DEFAULT_OBJECT_VERSION) ⇒ Project

Initialize a new instance

Parameters:

  • path (Pathname, String)

    @see path

  • skip_initialization (Bool) (defaults to: false)

    Whether the project should be initialized from scratch.

  • object_version (Int) (defaults to: Xcodeproj::Constants::DEFAULT_OBJECT_VERSION)

    Object version to use for serialization, defaults to Xcode 3.2 compatible.



19
20
21
22
23
24
25
26
27
28
# File 'lib/cocoapods/project.rb', line 19

def initialize(path, skip_initialization = false,
    object_version = Xcodeproj::Constants::DEFAULT_OBJECT_VERSION)
  super(path, skip_initialization, object_version)
  @support_files_group = new_group('Targets Support Files')
  @refs_by_absolute_path = {}
  @variant_groups_by_path_and_name = {}
  @pods = new_group('Pods')
  @development_pods = new_group('Development Pods')
  self.symroot = LEGACY_BUILD_ROOT
end

Instance Attribute Details

#development_podsPBXGroup (readonly)

Returns The group for Development Pods.

Returns:

  • (PBXGroup)

    The group for Development Pods.



41
42
43
# File 'lib/cocoapods/project.rb', line 41

def development_pods
  @development_pods
end

#podsPBXGroup (readonly)

Returns The group for the Pods.

Returns:

  • (PBXGroup)

    The group for the Pods.



37
38
39
# File 'lib/cocoapods/project.rb', line 37

def pods
  @pods
end

#refs_by_absolute_pathHash{String => PBXFileReference} (readonly, private)

Returns The file references grouped by absolute path.

Returns:

  • (Hash{String => PBXFileReference})

    The file references grouped by absolute path.



276
277
278
# File 'lib/cocoapods/project.rb', line 276

def refs_by_absolute_path
  @refs_by_absolute_path
end

#support_files_groupPBXGroup (readonly)

Returns The group for the support files of the aggregate targets.

Returns:

  • (PBXGroup)

    The group for the support files of the aggregate targets.



33
34
35
# File 'lib/cocoapods/project.rb', line 33

def support_files_group
  @support_files_group
end

#variant_groups_by_path_and_nameHash{[Pathname, String] => PBXVariantGroup} (readonly, private)

Returns The variant groups grouped by absolute path of parent dir and name.

Returns:

  • (Hash{[Pathname, String] => PBXVariantGroup})

    The variant groups grouped by absolute path of parent dir and name.



281
282
283
# File 'lib/cocoapods/project.rb', line 281

def variant_groups_by_path_and_name
  @variant_groups_by_path_and_name
end

Instance Method Details

#add_build_configuration(name, type) ⇒ XCBuildConfiguration

Note:

This method extends the original Xcodeproj implementation to include a preprocessor definition named after the build setting. This is done to support the TargetEnvironmentHeader specification of Pods available only on certain build configurations.

Adds a new build configuration to the project and populates it with default settings according to the provided type.

Parameters:

  • name (String)

    The name of the build configuration.

  • type (Symbol)

    The type of the build configuration used to populate the build settings, must be :debug or :release.

Returns:

  • (XCBuildConfiguration)

    The new build configuration.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/cocoapods/project.rb', line 243

def add_build_configuration(name, type)
  build_configuration = super
  settings = build_configuration.build_settings
  definitions = settings['GCC_PREPROCESSOR_DEFINITIONS'] || ['$(inherited)']
  defines = [defininition_for_build_configuration(name)]
  defines << 'DEBUG' if type == :debug
  defines.each do |define|
    value = "#{define}=1"
    unless definitions.include?(value)
      definitions.unshift(value)
    end
  end
  settings['GCC_PREPROCESSOR_DEFINITIONS'] = definitions
  build_configuration
end

#add_file_reference(absolute_path, group, reflect_file_system_structure = false) ⇒ PBXFileReference

Adds a file reference to given path as a child of the given group.

Parameters:

  • absolute_path (Array<Pathname,String>)

    The path of the file.

  • group (PBXGroup)

    The group for the new file reference.

  • reflect_file_system_structure (Bool) (defaults to: false)

    Whether group structure should reflect the file system structure. If yes, where needed, intermediate groups are created, similar to how mkdir -p operates.

Returns:

  • (PBXFileReference)

    The new file reference.



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cocoapods/project.rb', line 182

def add_file_reference(absolute_path, group, reflect_file_system_structure = false)
  file_path_name = absolute_path.is_a?(Pathname) ? absolute_path : Pathname.new(absolute_path)
  group = group_for_path_in_group(file_path_name, group, reflect_file_system_structure)

  if ref = reference_for_path(file_path_name.realpath)
    ref
  else
    ref = group.new_file(file_path_name.realpath)
    @refs_by_absolute_path[absolute_path.to_s] = ref
  end
end

#add_pod_group(pod_name, path, development = false, absolute = false) ⇒ PBXGroup

Creates a new group for the Pod with the given name and configures its path.

Parameters:

  • pod_name (String)

    The name of the Pod.

  • path (#to_s)

    The path to the root of the Pod.

  • development (Bool) (defaults to: false)

    Wether the group should be added to the Development Pods group.

  • absolute (Bool) (defaults to: false)

    Wether the path of the group should be set as absolute.

Returns:

  • (PBXGroup)

    The new group.



84
85
86
87
88
89
90
91
92
# File 'lib/cocoapods/project.rb', line 84

def add_pod_group(pod_name, path, development = false, absolute = false)
  raise '[BUG]' if pod_group(pod_name)

  parent_group = development ? development_pods : pods
  source_tree = absolute ? :absolute : :group

  group = parent_group.new_group(pod_name, path, source_tree)
  group
end

#add_podfile(podfile_path) ⇒ PBXFileReference

Adds a file reference to the Podfile.

Parameters:

  • podfile_path (#to_s)

    The path of the Podfile.

Returns:

  • (PBXFileReference)

    The new file reference.



217
218
219
220
221
222
223
# File 'lib/cocoapods/project.rb', line 217

def add_podfile(podfile_path)
  new_file(podfile_path, :project).tap do |podfile_ref|
    podfile_ref.xc_language_specification_identifier = 'xcode.lang.ruby'
    podfile_ref.explicit_file_type = 'text.script.ruby'
    podfile_ref.last_known_file_type = 'text'
  end
end

#defininition_for_build_configuration(name) ⇒ String

Returns The preprocessor definition to set for the configuration.

Parameters:

  • name (String)

    The name of the build configuration.

Returns:

  • (String)

    The preprocessor definition to set for the configuration.



264
265
266
# File 'lib/cocoapods/project.rb', line 264

def defininition_for_build_configuration(name)
  "POD_CONFIGURATION_#{name.underscore}".gsub(/[^a-zA-Z0-9_]/, '_').upcase
end

#group_for_path_in_group(absolute_pathname, group, reflect_file_system_structure) ⇒ PBXGroup (private)

Returns the group for an absolute file path in another group. Creates subgroups to reflect the file system structure if reflect_file_system_structure is set to true. Makes a variant group if the path points to a localized file inside a *.lproj directory. To support Apple Base Internationalization, the same variant group is returned for interface files and strings files with the same name.

Parameters:

  • absolute_pathname (Pathname)

    The pathname of the file to get the group for.

  • group (PBXGroup)

    The parent group used as the base of the relative path.

  • reflect_file_system_structure (Bool)

    Whether group structure should reflect the file system structure. If yes, where needed, intermediate groups are created, similar to how mkdir -p operates.

Returns:

  • (PBXGroup)

    The appropriate group for the filepath. Can be PBXVariantGroup, if the file is localized.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/cocoapods/project.rb', line 305

def group_for_path_in_group(absolute_pathname, group, reflect_file_system_structure)
  unless absolute_pathname.absolute?
    raise ArgumentError, "Paths must be absolute #{absolute_pathname}"
  end

  relative_pathname = absolute_pathname.relative_path_from(group.real_path)
  relative_dir = relative_pathname.dirname
  lproj_regex = /\.lproj/i

  # Add subgroups for directories, but treat .lproj as a file
  if reflect_file_system_structure
    relative_dir.each_filename do|name|
      break if name.to_s =~ lproj_regex
      next if name == '.'
      group = group[name] || group.new_group(name, name)
    end
  end

  # Turn files inside .lproj directories into a variant group
  if relative_dir.basename.to_s =~ lproj_regex
    group_name = variant_group_name(absolute_pathname)
    lproj_parent_dir = absolute_pathname.dirname.dirname
    group = @variant_groups_by_path_and_name[[lproj_parent_dir, group_name]] ||
      group.new_variant_group(group_name, lproj_parent_dir)
    @variant_groups_by_path_and_name[[lproj_parent_dir, group_name]] ||= group
  end

  group
end

#group_for_spec(spec_name, subgroup_key = nil) ⇒ PBXGroup

Returns the group for the specification with the give name creating it if needed.

Parameters:

  • spec_name (String)

    The full name of the specification.

Returns:

  • (PBXGroup)

    The group.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cocoapods/project.rb', line 126

def group_for_spec(spec_name, subgroup_key = nil)
  pod_name = Specification.root_name(spec_name)
  group = pod_group(pod_name)
  raise "[Bug] Unable to locate group for Pod named `#{pod_name}`" unless group
  if spec_name != pod_name
    subspecs_names = spec_name.gsub(pod_name + '/', '').split('/')
    subspecs_names.each do |name|
      group = group[name] || group.new_group(name)
    end
  end

  if subgroup_key
    subgroup_name = SPEC_SUBGROUPS[subgroup_key]
    raise ArgumentError, "Unrecognized subgroup key `#{subgroup_key}`" unless subgroup_name
    group = group[subgroup_name] || group.new_group(subgroup_name)
  end

  group
end

#pod_group(pod_name) ⇒ PBXGroup

Returns the group for the Pod with the given name.

Parameters:

  • pod_name (String)

    The name of the Pod.

Returns:

  • (PBXGroup)

    The group.



107
108
109
# File 'lib/cocoapods/project.rb', line 107

def pod_group(pod_name)
  pod_groups.find { |group| group.name == pod_name }
end

#pod_groupsArray<PBXGroup>

Returns all the group of the Pods.

Returns:

  • (Array<PBXGroup>)

    Returns all the group of the Pods.



96
97
98
# File 'lib/cocoapods/project.rb', line 96

def pod_groups
  pods.children.objects + development_pods.children.objects
end

#pod_support_files_group(pod_name, dir) ⇒ PBXGroup

Returns the support files group for the Pod with the given name.

Parameters:

  • pod_name (String)

    The name of the Pod.

Returns:

  • (PBXGroup)

    The group.



153
154
155
156
157
158
159
160
# File 'lib/cocoapods/project.rb', line 153

def pod_support_files_group(pod_name, dir)
  group = pod_group(pod_name)
  support_files_group = group['Support Files']
  unless support_files_group
    support_files_group = group.new_group('Support Files', dir)
  end
  support_files_group
end

#reference_for_path(absolute_path) ⇒ PBXFileReference, Nil

Returns the file reference for the given absolute path.

Parameters:

  • absolute_path (#to_s)

    The absolute path of the file whose reference is needed.

Returns:

  • (PBXFileReference)

    The file reference.

  • (Nil)

    If no file reference could be found.



202
203
204
205
206
207
208
# File 'lib/cocoapods/project.rb', line 202

def reference_for_path(absolute_path)
  unless Pathname.new(absolute_path).absolute?
    raise ArgumentError, "Paths must be absolute #{absolute_path}"
  end

  refs_by_absolute_path[absolute_path.to_s]
end

#symroot=(symroot) ⇒ void

This method returns an undefined value.

Parameters:

  • symroot (String)

    The build root that is used when Xcode is configured to not use the workspace’s build root. Defaults to ${SRCROOT}/../build.



56
57
58
59
60
# File 'lib/cocoapods/project.rb', line 56

def symroot=(symroot)
  root_object.build_configuration_list.build_configurations.each do |config|
    config.build_settings['SYMROOT'] = symroot
  end
end

#variant_group_name(path) ⇒ String (private)

Returns the name to be used for a the variant group for a file at a given path. The path must be localized (within an *.lproj directory).

Parameters:

  • The (Pathname)

    localized path to get a variant group name for.

Returns:

  • (String)

    The variant group name.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/cocoapods/project.rb', line 342

def variant_group_name(path)
  unless path.to_s.downcase.include?('.lproj/')
    raise ArgumentError, 'Only localized resources can be added to variant groups.'
  end

  # When using Base Internationalization for XIBs and Storyboards a strings
  # file is generated with the same name as the XIB/Storyboard in each .lproj
  # directory:
  #   Base.lproj/MyViewController.xib
  #   fr.lproj/MyViewController.strings
  #
  # In this scenario we want the variant group to be the same as the XIB or Storyboard.
  #
  # Base Internationalization: https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourUserInterface/InternationalizingYourUserInterface.html
  if path.extname.downcase == '.strings'
    %w(.xib .storyboard).each do |extension|
      possible_interface_file = path.dirname.dirname + 'Base.lproj' + path.basename.sub_ext(extension)
      return possible_interface_file.basename.to_s if possible_interface_file.exist?
    end
  end

  path.basename.to_s
end