Class: Pod::Installer::UserProjectIntegrator::TargetIntegrator

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/installer/user_project_integrator/target_integrator.rb,
lib/cocoapods/installer/user_project_integrator/target_integrator/xcconfig_integrator.rb

Overview

This class is responsible for integrating the library generated by a TargetDefinition with its destination project.

Defined Under Namespace

Classes: XCConfigIntegrator, XCFileListConfigKey

Constant Summary collapse

BUILD_PHASE_PREFIX =

Returns the string to use as prefix for every build phase added to the user project.

Returns:

  • (String)

    the string to use as prefix for every build phase added to the user project

'[CP] '.freeze
USER_BUILD_PHASE_PREFIX =

Returns the string to use as prefix for every build phase declared by the user within a podfile or podspec.

Returns:

  • (String)

    the string to use as prefix for every build phase declared by the user within a podfile or podspec.

'[CP-User] '.freeze
CHECK_MANIFEST_PHASE_NAME =

Returns the name of the check manifest phase.

Returns:

  • (String)

    the name of the check manifest phase

'Check Pods Manifest.lock'.freeze
EMBED_FRAMEWORK_TARGET_TYPES =
Note:

This does not include :app_extension or :watch_extension because

frameworks are embedded in the output directory / product bundle.

these types must have their frameworks embedded in their host targets. For messages extensions, this only applies if it's embedded in a messages application.

Returns:

  • (Array<Symbol>)

    the symbol types, which require that the pod

[:application, :unit_test_bundle, :ui_test_bundle, :watch2_extension, :messages_application].freeze
EMBED_FRAMEWORK_PHASE_NAME =

Returns the name of the embed frameworks phase.

Returns:

  • (String)

    the name of the embed frameworks phase

'Embed Pods Frameworks'.freeze
PREPARE_ARTIFACTS_PHASE_NAME =

Returns the name of the embed frameworks phase.

Returns:

  • (String)

    the name of the embed frameworks phase

'Prepare Artifacts'.freeze
COPY_PODS_RESOURCES_PHASE_NAME =

Returns the name of the copy resources phase.

Returns:

  • (String)

    the name of the copy resources phase

'Copy Pods Resources'.freeze
MAX_INPUT_OUTPUT_PATHS =

Returns the maximum number of input and output paths to use for a script phase.

Returns:

  • (Integer)

    the maximum number of input and output paths to use for a script phase

1000
ARTIFACT_LIST_FILE =

Returns the path to the artifact lists file used in the prepare & embed scripts.

Returns:

  • (String)

    the path to the artifact lists file used in the prepare & embed scripts

'${BUILT_PRODUCTS_DIR}/cocoapods-artifacts-${CONFIGURATION}.txt'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, use_input_output_paths: true) ⇒ TargetIntegrator

Init a new TargetIntegrator

Parameters:

  • target (AggregateTarget)

    @see #target

  • use_input_output_paths (Boolean) (defaults to: true)

    @see #use_input_output_paths


70
71
72
73
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 70

def initialize(target, use_input_output_paths: true)
  @target = target
  @use_input_output_paths = use_input_output_paths
end

Instance Attribute Details

#targetAggregateTarget (readonly)

Returns the target that should be integrated.

Returns:


58
59
60
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 58

def target
  @target
end

#use_input_output_pathsBoolean (readonly) Also known as: use_input_output_paths?

Returns whether to use input/output paths for build phase scripts.

Returns:

  • (Boolean)

    whether to use input/output paths for build phase scripts


62
63
64
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 62

def use_input_output_paths
  @use_input_output_paths
end

Class Method Details

.create_or_update_copy_resources_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {}) ⇒ void

This method returns an undefined value.

Adds a shell script build phase responsible to copy the resources generated by the TargetDefinition to the bundle of the product of the targets.

Parameters:

  • native_target (PBXNativeTarget)

    The native target to add the script phase into.

  • script_path (String)

    The script path to execute as part of this script phase.

  • input_paths_by_config (Hash<Array, String>) (defaults to: {})

    The input paths (if any) to include for this script phase.

  • output_paths_by_config (Hash<Array, String>) (defaults to: {})

    The output paths (if any) to include for this script phase.


212
213
214
215
216
217
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 212

def create_or_update_copy_resources_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {})
  phase_name = COPY_PODS_RESOURCES_PHASE_NAME
  phase = TargetIntegrator.create_or_update_shell_script_build_phase(native_target, BUILD_PHASE_PREFIX + phase_name)
  phase.shell_script = %("#{script_path}"\n)
  TargetIntegrator.set_input_output_paths(phase, input_paths_by_config, output_paths_by_config)
end

.create_or_update_embed_frameworks_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {}) ⇒ void

This method returns an undefined value.

Adds a shell script build phase responsible to copy (embed) the frameworks generated by the TargetDefinition to the bundle of the product of the targets.

Parameters:

  • native_target (PBXNativeTarget)

    The native target to add the script phase into.

  • script_path (String)

    The script path to execute as part of this script phase.

  • input_paths_by_config (Hash<Array, String>) (defaults to: {})

    The input paths (if any) to include for this script phase.

  • output_paths_by_config (Hash<Array, String>) (defaults to: {})

    The output paths (if any) to include for this script phase.


140
141
142
143
144
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 140

def create_or_update_embed_frameworks_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {})
  phase = TargetIntegrator.create_or_update_shell_script_build_phase(native_target, BUILD_PHASE_PREFIX + EMBED_FRAMEWORK_PHASE_NAME)
  phase.shell_script = %("#{script_path}"\n)
  TargetIntegrator.set_input_output_paths(phase, input_paths_by_config, output_paths_by_config)
end

.create_or_update_prepare_artifacts_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {}) ⇒ void

This method returns an undefined value.

Adds a shell script build phase responsible to copy xcframeworks slices generated by the TargetDefinition to the build directory.

Parameters:

  • native_target (PBXNativeTarget)

    The native target to add the script phase into.

  • script_path (String)

    The script path to execute as part of this script phase.

  • input_paths_by_config (Hash<Array, String>) (defaults to: {})

    The input paths (if any) to include for this script phase.

  • output_paths_by_config (Hash<Array, String>) (defaults to: {})

    The output paths (if any) to include for this script phase.


163
164
165
166
167
168
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 163

def create_or_update_prepare_artifacts_script_phase_to_target(native_target, script_path, input_paths_by_config = {}, output_paths_by_config = {})
  phase = TargetIntegrator.create_or_update_shell_script_build_phase(native_target, BUILD_PHASE_PREFIX + PREPARE_ARTIFACTS_PHASE_NAME)
  phase.shell_script = %("#{script_path}"\n)
  reorder_script_phase(native_target, phase, :before_compile)
  TargetIntegrator.set_input_output_paths(phase, input_paths_by_config, output_paths_by_config)
end

.create_or_update_shell_script_build_phase(native_target, script_phase_name, show_env_vars_in_log = '0') ⇒ void

This method returns an undefined value.

Creates or update a shell script build phase for the given target.

Parameters:

  • native_target (PBXNativeTarget)

    The native target to add the script phase into.

  • script_phase_name (String)

    The name of the script phase to use.

  • show_env_vars_in_log (String) (defaults to: '0')

    The value to set for show environment variables in the log during execution of this script phase or `nil` for not setting the value at all.


244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 244

def create_or_update_shell_script_build_phase(native_target, script_phase_name, show_env_vars_in_log = '0')
  build_phases = native_target.build_phases.grep(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
  build_phases.find { |phase| phase.name && phase.name.end_with?(script_phase_name) }.tap { |p| p.name = script_phase_name if p } ||
    native_target.project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase).tap do |phase|
      UI.message("Adding Build Phase '#{script_phase_name}' to project.") do
        phase.name = script_phase_name
        unless show_env_vars_in_log.nil?
          phase.show_env_vars_in_log = show_env_vars_in_log
        end
        native_target.build_phases << phase
      end
    end
end

.create_or_update_user_script_phases(script_phases, native_target) ⇒ void

This method returns an undefined value.

Updates all target script phases for the current target, including creating or updating, deleting and re-ordering.


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 263

def create_or_update_user_script_phases(script_phases, native_target)
  script_phase_names = script_phases.map { |k| k[:name] }
  # Delete script phases no longer present in the target.
  native_target_script_phases = native_target.shell_script_build_phases.select { |bp| !bp.name.nil? && bp.name.start_with?(USER_BUILD_PHASE_PREFIX) }
  native_target_script_phases.each do |script_phase|
    script_phase_name_without_prefix = script_phase.name.sub(USER_BUILD_PHASE_PREFIX, '')
    unless script_phase_names.include?(script_phase_name_without_prefix)
      native_target.build_phases.delete(script_phase)
    end
  end
  # Create or update the ones that are expected to be.
  script_phases.each do |script_phase|
    name_with_prefix = USER_BUILD_PHASE_PREFIX + script_phase[:name]
    phase = TargetIntegrator.create_or_update_shell_script_build_phase(native_target, name_with_prefix, nil)
    phase.shell_script = script_phase[:script]
    phase.shell_path = script_phase[:shell_path] || '/bin/sh'
    phase.input_paths = script_phase[:input_files]
    phase.output_paths = script_phase[:output_files]
    phase.input_file_list_paths = script_phase[:input_file_lists]
    phase.output_file_list_paths = script_phase[:output_file_lists]
    phase.dependency_file = script_phase[:dependency_file]
    # At least with Xcode 10 `showEnvVarsInLog` is *NOT* set to any value even if it's checked and it only
    # gets set to '0' if the user has explicitly disabled this.
    if (show_env_vars_in_log = script_phase.fetch(:show_env_vars_in_log, '1')) == '0'
      phase.show_env_vars_in_log = show_env_vars_in_log
    end

    execution_position = script_phase[:execution_position]
    reorder_script_phase(native_target, phase, execution_position)
  end
end

.framework_output_paths(framework_input_paths) ⇒ Array<String>

Returns the framework output paths for the given input paths

Parameters:

  • framework_input_paths (Array<Xcode::FrameworkPaths>)

    The framework input paths to map to output paths.

Returns:

  • (Array<String>)

    The framework output paths


375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 375

def framework_output_paths(framework_input_paths)
  framework_input_paths.flat_map do |framework_path|
    framework_output_path = "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/#{File.basename(framework_path.source_path)}"
    dsym_output_path = if (dsym_input_path = framework_path.dsym_path)
                         "${DWARF_DSYM_FOLDER_PATH}/#{File.basename(dsym_input_path)}"
                       end
    bcsymbol_output_paths = unless framework_path.bcsymbolmap_paths.nil?
                              framework_path.bcsymbolmap_paths.map do |bcsymbolmap_path|
                                "${BUILT_PRODUCTS_DIR}/#{File.basename(bcsymbolmap_path)}"
                              end
                            end
    [framework_output_path, dsym_output_path, *bcsymbol_output_paths]
  end.compact.uniq
end

.input_output_paths_use_filelist?(object) ⇒ Boolean

Returns Whether input & output paths for the given object should be stored in a file list file.

Parameters:

  • object (Xcodeproj::Project::Object::AbstractObject)

Returns:

  • (Boolean)

    Whether input & output paths for the given object should be stored in a file list file.


85
86
87
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 85

def input_output_paths_use_filelist?(object)
  object.project.object_version.to_i >= 50
end

.output_extension_for_resource(input_extension) ⇒ String

Returns an extension in the target that corresponds to the resource's input extension.

Parameters:

  • input_extension (String)

    The input extension to map to.

Returns:

  • (String)

    The output extension.


338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 338

def output_extension_for_resource(input_extension)
  case input_extension
  when '.storyboard'        then '.storyboardc'
  when '.xib'               then '.nib'
  when '.framework'         then '.framework'
  when '.xcdatamodel'       then '.mom'
  when '.xcdatamodeld'      then '.momd'
  when '.xcmappingmodel'    then '.cdm'
  when '.xcassets'          then '.car'
  else                      input_extension
  end
end

.remove_copy_resources_script_phase_from_target(native_target) ⇒ Object

Delete a 'Copy Pods Resources' script phase if present

Parameters:

  • native_target (PBXNativeTarget)

    The native target to remove the script phase from.


224
225
226
227
228
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 224

def remove_copy_resources_script_phase_from_target(native_target)
  build_phase = native_target.shell_script_build_phases.find { |bp| bp.name && bp.name.end_with?(COPY_PODS_RESOURCES_PHASE_NAME) }
  return unless build_phase.present?
  native_target.build_phases.delete(build_phase)
end

.remove_embed_frameworks_script_phase_from_target(native_target) ⇒ Object

Delete a 'Embed Pods Frameworks' Copy Files Build Phase if present

Parameters:

  • native_target (PBXNativeTarget)

    The native target to remove the script phase from.


175
176
177
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 175

def remove_embed_frameworks_script_phase_from_target(native_target)
  remove_script_phase_from_target(native_target, EMBED_FRAMEWORK_PHASE_NAME)
end

.remove_prepare_artifacts_script_phase_from_target(native_target) ⇒ Object

Delete a 'Embed Pods Frameworks' Copy Files Build Phase if present

Parameters:

  • native_target (PBXNativeTarget)

    The native target to remove the script phase from.


184
185
186
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 184

def remove_prepare_artifacts_script_phase_from_target(native_target)
  remove_script_phase_from_target(native_target, PREPARE_ARTIFACTS_PHASE_NAME)
end

.remove_script_phase_from_target(native_target, phase_name) ⇒ Object


188
189
190
191
192
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 188

def remove_script_phase_from_target(native_target, phase_name)
  build_phase = native_target.shell_script_build_phases.find { |bp| bp.name && bp.name.end_with?(phase_name) }
  return unless build_phase.present?
  native_target.build_phases.delete(build_phase)
end

.reorder_script_phase(native_target, script_phase, execution_position) ⇒ Object


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 295

def reorder_script_phase(native_target, script_phase, execution_position)
  return if execution_position == :any
  compile_build_phase_index = native_target.build_phases.index do |bp|
    bp.is_a?(Xcodeproj::Project::Object::PBXSourcesBuildPhase)
  end
  unless compile_build_phase_index.nil?
    script_phase_index = native_target.build_phases.index do |bp|
      bp.is_a?(Xcodeproj::Project::Object::PBXShellScriptBuildPhase) && !bp.name.nil? && bp.name == script_phase.name
    end
    if (execution_position == :before_compile && script_phase_index > compile_build_phase_index) ||
      (execution_position == :after_compile && script_phase_index < compile_build_phase_index)
      native_target.build_phases.move_from(script_phase_index, compile_build_phase_index)
    end
  end
end

.resource_output_paths(resource_input_paths) ⇒ Array<String>

Returns the resource output paths for all given input paths.

Parameters:

  • resource_input_paths (Array<String>)

    The input paths to map to.

Returns:

  • (Array<String>)

    The resource output paths.


358
359
360
361
362
363
364
365
366
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 358

def resource_output_paths(resource_input_paths)
  resource_input_paths.map do |resource_input_path|
    base_path = '${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}'
    extname = File.extname(resource_input_path)
    basename = extname == '.xcassets' ? 'Assets' : File.basename(resource_input_path)
    output_extension = TargetIntegrator.output_extension_for_resource(extname)
    File.join(base_path, File.basename(basename, extname) + output_extension)
  end.uniq
end

.set_input_output_paths(phase, input_paths_by_config, output_paths_by_config) ⇒ Void

Sets the input & output paths for the given script build phase.

Parameters:

  • phase (Xcodeproj::Project::Object::PBXShellScriptBuildPhase)

    The phase to set input & output paths on.

  • input_paths_by_config (Hash)

Returns:

  • (Void)

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 97

def set_input_output_paths(phase, input_paths_by_config, output_paths_by_config)
  if input_output_paths_use_filelist?(phase)
    [input_paths_by_config, output_paths_by_config].each do |hash|
      hash.each do |file_list, files|
        generator = Generator::FileList.new(files)
        Xcode::PodsProjectGenerator::TargetInstallerHelper.update_changed_file(generator, file_list.file_list_path)
      end
    end

    phase.input_paths = nil
    phase.output_paths = nil
    phase.input_file_list_paths = input_paths_by_config.each_key.map(&:file_list_relative_path).uniq
    phase.output_file_list_paths = output_paths_by_config.each_key.map(&:file_list_relative_path).uniq
  else
    input_paths = input_paths_by_config.values.flatten(1).uniq
    output_paths = output_paths_by_config.values.flatten(1).uniq
    TargetIntegrator.validate_input_output_path_limit(input_paths, output_paths)

    phase.input_paths = input_paths
    phase.output_paths = output_paths
    phase.input_file_list_paths = nil
    phase.output_file_list_paths = nil
  end
end

.validate_input_output_path_limit(input_paths, output_paths) ⇒ void

This method returns an undefined value.

Script phases can have a limited number of input and output paths due to each one being exported to `env`. A large number can cause a build failure because of limitations in `env`. See issue github.com/CocoaPods/CocoaPods/issues/7362.

Parameters:

  • input_paths (Array<String>)

    The input paths to trim.

  • output_paths (Array<String>)

    The output paths to trim.


323
324
325
326
327
328
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 323

def validate_input_output_path_limit(input_paths, output_paths)
  if (input_paths.count + output_paths.count) > MAX_INPUT_OUTPUT_PATHS
    input_paths.clear
    output_paths.clear
  end
end

Instance Method Details

#inspectString

Returns a string representation suitable for debugging.

Returns:

  • (String)

    a string representation suitable for debugging.


413
414
415
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 413

def inspect
  "#<#{self.class} for target `#{target.label}'>"
end

#integrate!void

This method returns an undefined value.

Integrates the user project targets. Only the targets that do *not* already have the Pods library in their frameworks build phase are processed.


397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/cocoapods/installer/user_project_integrator/target_integrator.rb', line 397

def integrate!
  UI.section(integration_message) do
    XCConfigIntegrator.integrate(target, native_targets)

    add_pods_library
    add_embed_frameworks_script_phase
    add_prepare_artifacts_script_phase
    remove_embed_frameworks_script_phase_from_embedded_targets
    add_copy_resources_script_phase
    add_check_manifest_lock_script_phase
    add_user_script_phases
  end
end