Class: Pod::Installer::Xcode::PodsProjectGenerator::PodTargetIntegrator

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb

Overview

This class is responsible for integrating a pod target. This includes integrating the test targets included by each pod target.

Instance Attribute Summary collapse

Integration steps collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_installation_result, use_input_output_paths: true) ⇒ PodTargetIntegrator

Initialize a new instance

Parameters:

  • target_installation_result (TargetInstallationResult)

    @see #target_installation_result

  • use_input_output_paths (Boolean) (defaults to: true)

    @see #use_input_output_paths


23
24
25
26
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 23

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

Instance Attribute Details

#target_installation_resultTargetInstallationResult (readonly)

Returns the installation result of the target that should be integrated.

Returns:


11
12
13
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 11

def target_installation_result
  @target_installation_result
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


15
16
17
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 15

def use_input_output_paths
  @use_input_output_paths
end

Instance Method Details

#add_copy_dsyms_script_phase(native_target) ⇒ void (private)

This method returns an undefined value.

Adds a script phase that copies and strips dSYMs that are part of this target. Note this only deals with vendored dSYMs.

Parameters:

  • native_target (PBXNativeTarget)

    the native target for which to add the the copy dSYM files build phase.


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 206

def add_copy_dsyms_script_phase(native_target)
  script_path = "${PODS_ROOT}/#{target.copy_dsyms_script_path.relative_path_from(target.sandbox.root)}"
  dsym_paths = PodTargetInstaller.dsym_paths(target)
  bcsymbolmap_paths = PodTargetInstaller.bcsymbolmap_paths(target)

  if dsym_paths.empty? && bcsymbolmap_paths.empty?
    script_phase = native_target.shell_script_build_phases.find do |bp|
      bp.name && bp.name.end_with?(UserProjectIntegrator::TargetIntegrator::COPY_DSYM_FILES_PHASE_NAME)
    end
    native_target.build_phases.delete(script_phase) if script_phase.present?
    return
  end

  phase_name = UserProjectIntegrator::TargetIntegrator::BUILD_PHASE_PREFIX + UserProjectIntegrator::TargetIntegrator::COPY_DSYM_FILES_PHASE_NAME
  phase = UserProjectIntegrator::TargetIntegrator.create_or_update_shell_script_build_phase(native_target, phase_name)
  phase.shell_script = %("#{script_path}"\n)

  input_paths_by_config = {}
  output_paths_by_config = {}
  if use_input_output_paths?
    input_file_list_path = target.copy_dsyms_script_input_files_path
    input_file_list_relative_path = "${PODS_ROOT}/#{input_file_list_path.relative_path_from(target.sandbox.root)}"
    input_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(input_file_list_path, input_file_list_relative_path)
    input_paths = input_paths_by_config[input_paths_key] = []
    input_paths.concat([dsym_paths, *bcsymbolmap_paths].flatten.compact)

    output_file_list_path = target.copy_dsyms_script_output_files_path
    output_file_list_relative_path = "${PODS_ROOT}/#{output_file_list_path.relative_path_from(target.sandbox.root)}"
    output_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(output_file_list_path, output_file_list_relative_path)
    output_paths = output_paths_by_config[output_paths_key] = []

    dsym_output_paths = dsym_paths.map { |dsym_path| "${DWARF_DSYM_FOLDER_PATH}/#{File.basename(dsym_path)}" }
    bcsymbolmap_output_paths = bcsymbolmap_paths.map { |bcsymbolmap_path| "${DWARF_DSYM_FOLDER_PATH}/#{File.basename(bcsymbolmap_path)}" }
    output_paths.concat([dsym_output_paths, *bcsymbolmap_output_paths].flatten.compact)
  end

  UserProjectIntegrator::TargetIntegrator.set_input_output_paths(phase, input_paths_by_config, output_paths_by_config)
end

#add_copy_resources_script_phase(native_target, spec) ⇒ void (private)

This method returns an undefined value.

Find or create a 'Copy Pods Resources' build phase

Parameters:

  • native_target (PBXNativeTarget)

    the native target for which to add the copy resources script

  • spec (Pod::Specification)

    the specification to integrate


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 66

def add_copy_resources_script_phase(native_target, spec)
  script_path = "${PODS_ROOT}/#{target.copy_resources_script_path_for_spec(spec).relative_path_from(target.sandbox.root)}"

  input_paths_by_config = {}
  output_paths_by_config = {}

  dependent_targets = if spec.test_specification?
                        target.dependent_targets_for_test_spec(spec)
                      else
                        target.dependent_targets_for_app_spec(spec)
                      end
  host_target_spec_names = target.app_host_dependent_targets_for_spec(spec).flat_map do |pt|
    pt.specs.map(&:name)
  end.uniq
  resource_paths = dependent_targets.flat_map do |dependent_target|
    spec_paths_to_include = dependent_target.library_specs.map(&:name)
    spec_paths_to_include -= host_target_spec_names
    spec_paths_to_include << spec.name if dependent_target == target
    dependent_target.resource_paths.values_at(*spec_paths_to_include).flatten.compact
  end.uniq

  if use_input_output_paths? && !resource_paths.empty?
    input_file_list_path = target.copy_resources_script_input_files_path_for_spec(spec)
    input_file_list_relative_path = "${PODS_ROOT}/#{input_file_list_path.relative_path_from(target.sandbox.root)}"
    input_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(input_file_list_path, input_file_list_relative_path)
    input_paths_by_config[input_paths_key] = [script_path] + resource_paths

    output_file_list_path = target.copy_resources_script_output_files_path_for_spec(spec)
    output_file_list_relative_path = "${PODS_ROOT}/#{output_file_list_path.relative_path_from(target.sandbox.root)}"
    output_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(output_file_list_path, output_file_list_relative_path)
    output_paths_by_config[output_paths_key] = UserProjectIntegrator::TargetIntegrator.resource_output_paths(resource_paths)
  end

  if resource_paths.empty?
    UserProjectIntegrator::TargetIntegrator.remove_copy_resources_script_phase_from_target(native_target)
  else
    UserProjectIntegrator::TargetIntegrator.create_or_update_copy_resources_script_phase_to_target(
      native_target, script_path, input_paths_by_config, output_paths_by_config)
  end
end

#add_copy_xcframeworks_script_phase(native_target) ⇒ void (private)

This method returns an undefined value.

Find or create a 'Prepare Artifacts' Run Script Build Phase

Parameters:

  • native_target (PBXNativeTarget)

    the native target for which to add the prepare artifacts script


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 165

def add_copy_xcframeworks_script_phase(native_target)
  script_path = "${PODS_ROOT}/#{target.copy_xcframeworks_script_path.relative_path_from(target.sandbox.root)}"

  input_paths_by_config = {}
  output_paths_by_config = {}

  xcframeworks = target.xcframeworks.values.flatten

  if use_input_output_paths? && !xcframeworks.empty?
    input_file_list_path = target.copy_xcframeworks_script_input_files_path
    input_file_list_relative_path = "${PODS_ROOT}/#{input_file_list_path.relative_path_from(target.sandbox.root)}"
    input_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(input_file_list_path, input_file_list_relative_path)
    input_paths = input_paths_by_config[input_paths_key] = [script_path]

    framework_paths = xcframeworks.map { |xcf| "${PODS_ROOT}/#{xcf.path.relative_path_from(target.sandbox.root)}" }
    input_paths.concat framework_paths

    output_file_list_path = target.copy_xcframeworks_script_output_files_path
    output_file_list_relative_path = "${PODS_ROOT}/#{output_file_list_path.relative_path_from(target.sandbox.root)}"
    output_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(output_file_list_path, output_file_list_relative_path)
    output_paths_by_config[output_paths_key] = xcframeworks.map do |xcf|
      "#{Target::BuildSettings::XCFRAMEWORKS_BUILD_DIR_VARIABLE}/#{xcf.name}"
    end
  end

  if xcframeworks.empty?
    UserProjectIntegrator::TargetIntegrator.remove_copy_xcframeworks_script_phase_from_target(native_target)
  else
    UserProjectIntegrator::TargetIntegrator.create_or_update_copy_xcframeworks_script_phase_to_target(
      native_target, script_path, input_paths_by_config, output_paths_by_config)
  end
end

#add_embed_frameworks_script_phase(native_target, spec) ⇒ void (private)

This method returns an undefined value.

Find or create a 'Embed Pods Frameworks' Run Script Build Phase

Parameters:

  • native_target (PBXNativeTarget)

    the native target for which to add the embed frameworks script

  • spec (Pod::Specification)

    the specification to integrate


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 117

def add_embed_frameworks_script_phase(native_target, spec)
  script_path = "${PODS_ROOT}/#{target.embed_frameworks_script_path_for_spec(spec).relative_path_from(target.sandbox.root)}"

  input_paths_by_config = {}
  output_paths_by_config = {}

  dependent_targets = if spec.test_specification?
                        target.dependent_targets_for_test_spec(spec)
                      else
                        target.dependent_targets_for_app_spec(spec)
                      end
  host_target_spec_names = target.app_host_dependent_targets_for_spec(spec).flat_map do |pt|
    pt.specs.map(&:name)
  end.uniq
  framework_paths = dependent_targets.flat_map do |dependent_target|
    spec_paths_to_include = dependent_target.library_specs.map(&:name)
    spec_paths_to_include -= host_target_spec_names
    spec_paths_to_include << spec.name if dependent_target == target
    dependent_target.framework_paths.values_at(*spec_paths_to_include).flatten.compact
  end.uniq

  if use_input_output_paths? && !framework_paths.empty?
    input_file_list_path = target.embed_frameworks_script_input_files_path_for_spec(spec)
    input_file_list_relative_path = "${PODS_ROOT}/#{input_file_list_path.relative_path_from(target.sandbox.root)}"
    input_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(input_file_list_path, input_file_list_relative_path)
    input_paths_by_config[input_paths_key] = [script_path] + UserProjectIntegrator::TargetIntegrator.embed_frameworks_input_paths(framework_paths, [])

    output_file_list_path = target.embed_frameworks_script_output_files_path_for_spec(spec)
    output_file_list_relative_path = "${PODS_ROOT}/#{output_file_list_path.relative_path_from(target.sandbox.root)}"
    output_paths_key = UserProjectIntegrator::TargetIntegrator::XCFileListConfigKey.new(output_file_list_path, output_file_list_relative_path)
    output_paths_by_config[output_paths_key] = UserProjectIntegrator::TargetIntegrator.embed_frameworks_output_paths(framework_paths, [])
  end

  if framework_paths.empty?
    UserProjectIntegrator::TargetIntegrator.remove_embed_frameworks_script_phase_from_target(native_target)
  else
    UserProjectIntegrator::TargetIntegrator.create_or_update_embed_frameworks_script_phase_to_target(
      native_target, script_path, input_paths_by_config, output_paths_by_config)
  end
end

#inspectString

Returns a string representation suitable for debugging.

Returns:

  • (String)

    a string representation suitable for debugging.


47
48
49
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 47

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

#integrate!void

This method returns an undefined value.

Integrates the pod target.


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 32

def integrate!
  UI.section(integration_message) do
    target_installation_result.non_library_specs_by_native_target.each do |native_target, spec|
      add_embed_frameworks_script_phase(native_target, spec)
      add_copy_resources_script_phase(native_target, spec)
      UserProjectIntegrator::TargetIntegrator.create_or_update_user_script_phases(script_phases_for_specs(spec), native_target)
    end
    add_copy_dsyms_script_phase(target_installation_result.native_target)
    add_copy_xcframeworks_script_phase(target_installation_result.native_target)
    UserProjectIntegrator::TargetIntegrator.create_or_update_user_script_phases(script_phases_for_specs(target.library_specs), target_installation_result.native_target)
  end
end

#integration_messageString (private)

Returns the message that should be displayed for the target integration.

Returns:

  • (String)

    the message that should be displayed for the target integration.


248
249
250
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 248

def integration_message
  "Integrating target `#{target.name}`"
end

#script_phases_for_specs(specs) ⇒ Array<Hash<Symbol=>String>] an array of all combined script phases from the specs. (private)

Returns ArrayString>] an array of all combined script phases from the specs.

Parameters:

Returns:

  • (Array<Hash<Symbol=>String>] an array of all combined script phases from the specs.)

    ArrayString>] an array of all combined script phases from the specs.


263
264
265
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 263

def script_phases_for_specs(specs)
  Array(specs).flat_map { |spec| spec.consumer(target.platform).script_phases }
end

#targetPodTarget (private)

Returns the target part of the installation result.

Returns:

  • (PodTarget)

    the target part of the installation result.


254
255
256
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_integrator.rb', line 254

def target
  target_installation_result.target
end