Class: PodBuilder::Podfile

Inherits:
Object
  • Object
show all
Defined in:
lib/pod_builder/podfile.rb

Constant Summary collapse

PODBUILDER_LOCK_ACTION =
["raise \"\\n🚨  Do not launch 'pod install' manually, use `pod_builder` instead!\\n\" if !File.exist?('pod_builder.lock')"].freeze

Class Method Summary collapse

Class Method Details

.from_podfile_items(items, analyzer, build_configuration, install_using_frameworks, build_catalyst, build_xcframeworks) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
106
107
108
109
110
111
112
113
114
115
116
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pod_builder/podfile.rb', line 7

def self.from_podfile_items(items, analyzer, build_configuration, install_using_frameworks, build_catalyst, build_xcframeworks)
  raise "\n\nno items\n".red unless items.count > 0

  # Xcode 14 requires a development team to be specified for the compilation to succeed
  development_team = Configuration::development_team
  if development_team.empty?
    project_path = "#{PodBuilder.project_path}/#{Configuration::project_name}.xcodeproj"
    development_team = `grep -rh 'DEVELOPMENT_TEAM' '#{project_path}' | sort | uniq`.strip
    development_team_match = development_team.match(/DEVELOPMENT_TEAM = (.+);/)
    if development_team.split("\n").count != 1 || development_team_match&.size != 2
      raise "\n\nFailed getting 'DEVELOPMENT_TEAM' build setting, please add your development team to #{PodBuilder::basepath(Configuration.configuration_filename)} as per documentation".red
    end
    development_team = development_team_match[1]
  end
  # Xcode 15 requires xcframeworks to be signed
  code_sign_identity = Configuration.build_settings["CODE_SIGN_IDENTITY"] || ""
  if code_sign_identity.empty?
    project_path = "#{PodBuilder.project_path}/#{Configuration::project_name}.xcodeproj"
    code_sign_identity = `grep -rh 'CODE_SIGN_IDENTITY' '#{project_path}' | grep "Apple Distribution:" | sort | uniq`.strip
    code_sign_identity_match = code_sign_identity.match(/CODE_SIGN_IDENTITY = \"(.+)\";/)
    if code_sign_identity.split("\n").count != 1 || code_sign_identity_match&.size != 2
      raise "\n\nFailed getting 'CODE_SIGN_IDENTITY' build setting, please add your code sign identity to #{PodBuilder::basepath(Configuration.configuration_filename)} as per documentation".red
    end
    code_sign_identity = code_sign_identity_match[1]
  end

  sources = analyzer.sources

  cwd = File.dirname(File.expand_path(__FILE__))
  podfile = File.read("#{cwd}/templates/build_podfile.template")

  platform = analyzer.instance_variable_get("@result").targets.first.platform

  podfile.sub!("%%%use_frameworks%%%", install_using_frameworks ? "use_frameworks!" : "use_modular_headers!")
  podfile.sub!("%%%uses_frameworks%%%", install_using_frameworks ? "true" : "false")
  podfile.sub!("%%%build_xcframeworks%%%", build_xcframeworks ? "true" : "false")
  podfile.sub!("%%%build_catalyst%%%", build_catalyst ? "true" : "false")

  podfile.sub!("%%%platform_name%%%", platform.name.to_s)
  podfile.sub!("%%%deployment_version%%%", platform.deployment_target.version)

  podfile.sub!("%%%sources%%%", sources.map { |x| "source '#{x.url}'" }.join("\n"))

  podfile.sub!("%%%build_configuration%%%", build_configuration.capitalize)
  podfile.sub!("%%%keep_swiftmodules%%%", Configuration.keep_swiftmodules ? "true" : "false")

  podfile.sub!("%%%development_team%%%", development_team)

  podfile.sub!("%%%code_sign_identity%%%", code_sign_identity)

  podfile_build_settings = ""

  git_rootpath = PodBuilder::git_rootpath

  pod_dependencies = {}

  items.each do |item|
    build_settings = Configuration.build_settings.dup

    item_build_settings = Configuration.build_settings_overrides[item.name].dup || {}

    # These settings need to be set as is to properly build frameworks
    build_settings["SWIFT_COMPILATION_MODE"] = "wholemodule"
    build_settings["ONLY_ACTIVE_ARCH"] = "NO"
    build_settings["DEBUG_INFORMATION_FORMAT"] = "dwarf-with-dsym"

    # https://thi.imhttps://thi.im/posts/swift-serialize-debugging-options/
    build_settings["SWIFT_SERIALIZE_DEBUGGING_OPTIONS"] = "NO"

    if Configuration.react_native_project && item.name.include?("Folly")
      build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "9.0" # https://github.com/facebook/flipper/issues/834#issuecomment-899725463
    else
      build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = platform.deployment_target.version # Fix compilation warnings on Xcode 12
    end

    # Ignore deprecation warnings
    build_settings["GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS"] = "NO"

    # Improve compile speed
    build_settings["COMPILER_INDEX_STORE_ENABLE"] = "NO"
    build_settings["SWIFT_INDEX_STORE_ENABLE"] = "NO"
    build_settings["MTL_ENABLE_INDEX_STORE"] = "NO"

    if Configuration.build_system == "Legacy"
      build_settings["BUILD_LIBRARY_FOR_DISTRIBUTION"] = "NO"
    elsif Configuration.library_evolution_support || item.build_xcframework
      build_settings["BUILD_LIBRARY_FOR_DISTRIBUTION"] = "YES"
    end

    build_settings["SWIFT_VERSION"] = item_build_settings["SWIFT_VERSION"] || item.swift_version || project_swift_version(analyzer)

    if build_settings["ENABLE_BITCODE"] == "YES"
      build_settings["BITCODE_GENERATION_MODE"] = "bitcode"
    end

    # Don't store .pcm info in binary, see https://forums.swift.org/t/swift-behavior-of-gmodules-and-dsyms/23211/3
    build_settings["CLANG_ENABLE_MODULE_DEBUGGING"] = "NO"
    other_swift_flags_override = " $(inherited) -Xfrontend -no-clang-module-breadcrumbs -Xfrontend -no-serialize-debugging-options"
    other_c_flags_override = " $(inherited)"

    if Configuration.generate_coverage
      other_swift_flags_override += " -profile-coverage-mapping -profile-generate"
      other_c_flags_override += " -fprofile-instr-generate -fcoverage-mapping"
    end

    if Configuration.remap_coverage_to_project_root
      # Remap coverage path from /tmp/pod_builder/Pods/ItemName -> path relative to project git_root
      replacements = []
      if path = item.path
        replacements << ["#{Configuration.build_path}/Pods/#{item.root_name}", PodBuilder::basepath(path)]
        replacements << ["#{Configuration.build_path}/Pods/#{File.basename(path)}", PodBuilder::basepath(path)]
      else
        replacements << ["#{Configuration.build_path}/Pods/#{item.root_name}", PodBuilder::project_path("Pods/#{item.root_name}")]
      end
      replacements.uniq!

      replacements.each do |from, to|
        to = "./" + Pathname.new(to).relative_path_from(Pathname.new(git_rootpath)).to_s

        if from.start_with?("/tmp")
          other_swift_flags_override += " -coverage-prefix-map \"/private#{from}/\"=\"#{to}/\""
          other_c_flags_override += " -fcoverage-prefix-map=\"/private#{from}/\"=\"#{to}/\""
        else
          other_swift_flags_override += " -coverage-prefix-map \"#{from}/\"=\"#{to}/\""
          other_c_flags_override += " -fcoverage-prefix-map=\"#{from}/\"=\"#{to}/\""
        end
      end
    end

    item_build_settings.each do |k, v|
      # Do not allow to override above settings which are mandatory for a correct compilation
      if build_settings[k].nil?
        build_settings[k] = v
      end
    end

    # All the below settings should be merged with global (Configuration.build_settings) or per pod build_settings (Configuration.build_settings_overrides)
    build_settings["OTHER_SWIFT_FLAGS"] = build_settings.fetch("OTHER_SWIFT_FLAGS", "") + other_swift_flags_override
    build_settings["OTHER_CFLAGS"] = build_settings.fetch("OTHER_CFLAGS", "") + other_c_flags_override

    podfile_build_settings += "set_build_settings(\"#{item.root_name}\", #{build_settings.to_s}, installer)\n  "

    dependency_names = item.dependency_names.map { |x|
      if x.split("/").first == item.root_name
        next nil # remove dependency to parent spec
      end
      if overridded_module_name = Configuration.spec_overrides.fetch(x, {})["module_name"] # this might no longer be needed after
        next overridded_module_name
      end
    }.compact

    if dependency_names.count > 0
      pod_dependencies[item.root_name] = dependency_names
    end
  end

  podfile.sub!("%%%build_settings%%%", podfile_build_settings)

  podfile.sub!("%%%build_system%%%", Configuration.build_system)

  podfile.sub!("%%%pods%%%", "\"#{items.map(&:name).join('", "')}\"")

  podfile.sub!("%%%pods_dependencies%%%", pod_dependencies.to_s)

  podfile.sub!("%%%targets%%%", items.map(&:entry).join("\n  "))

  return podfile
end

.installObject



324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/pod_builder/podfile.rb', line 324

def self.install
  puts "Running pod install".yellow

  Dir.chdir(PodBuilder::project_path) do
    bundler_prefix = Configuration.use_bundler ? "bundle exec " : ""

    if Configuration.react_native_project
      system("#{bundler_prefix}pod deintegrate;")
    end

    system("#{bundler_prefix}pod install;")
  end
end

.install_using_frameworks(analyzer) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/pod_builder/podfile.rb', line 471

def self.install_using_frameworks(analyzer)
  target_settings = analyzer.podfile.target_definition_list.map(&:uses_frameworks?).uniq
  if target_settings.count == 1
    return target_settings.first
  elsif target_settings.count > 1
    raise "\n\n'use_frameworks!' should be declared only once at Podfile root level (not nested in targets)\n".red
  else
    raise "\n\nFailed detecting use_frameworks!\n".red
  end

  return true
end

.pod_definition_in(line, include_commented) ⇒ Object



343
344
345
346
347
348
349
350
351
352
# File 'lib/pod_builder/podfile.rb', line 343

def self.pod_definition_in(line, include_commented)
  stripped_line = strip_line(line)
  matches = stripped_line.match(/(^pod')(.*?)(')/)

  if matches&.size == 4 && (include_commented || !stripped_line.start_with?("#"))
    return matches[2]
  else
    return nil
  end
end

.resolve_pod_names(names, all_buildable_items) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
# File 'lib/pod_builder/podfile.rb', line 439

def self.resolve_pod_names(names, all_buildable_items)
  resolved_names = []

  names.each do |name|
    if item = all_buildable_items.detect { |t| t.root_name.downcase == name.downcase }
      resolved_names.push(item.root_name)
    end
  end

  return resolved_names.uniq
end

.resolve_pod_names_from_podfile(names) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/pod_builder/podfile.rb', line 451

def self.resolve_pod_names_from_podfile(names)
  resolved_names = []

  # resolve potentially wrong pod name case
  podfile_path = PodBuilder::basepath("Podfile")
  content = File.read(podfile_path)

  current_section = ""
  content.each_line do |line|
    matches = line.gsub("\"", "'").match(/pod '(.*?)'/)
    if matches&.size == 2
      if resolved_name = names.detect { |t| matches[1].split("/").first.downcase == t.downcase }
        resolved_names.push(matches[1].split("/").first)
      end
    end
  end

  resolved_names.uniq
end

.restore_file_sanity_checkObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/pod_builder/podfile.rb', line 381

def self.restore_file_sanity_check
  unless Configuration.restore_enabled
    return nil
  end

  puts "Checking Podfile.restore".yellow

  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  unless File.exist?(podfile_restore_path)
    return
  end

  error = nil

  begin
    File.rename(PodBuilder::basepath("Podfile"), PodBuilder::basepath("Podfile.tmp2"))
    File.rename(podfile_restore_path, PodBuilder::basepath("Podfile"))

    Analyze.installer_at(PodBuilder::basepath, false)
  rescue Exception => e
    error = e.to_s
  ensure
    File.rename(PodBuilder::basepath("Podfile"), podfile_restore_path)
    File.rename(PodBuilder::basepath("Podfile.tmp2"), PodBuilder::basepath("Podfile"))
  end

  if !error.nil?
    FileUtils.rm(podfile_restore_path)
  end

  return error
end

.restore_podfile_clean(pod_items) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/pod_builder/podfile.rb', line 354

def self.restore_podfile_clean(pod_items)
  unless Configuration.restore_enabled
    return
  end

  # remove pods that are no longer listed in pod_items
  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  unless File.exist?(podfile_restore_path)
    return
  end

  restore_content = File.read(podfile_restore_path)

  cleaned_lines = []
  restore_content.each_line do |line|
    if pod_name = pod_definition_in(line, false)
      if pod_items.map(&:name).include?(pod_name)
        cleaned_lines.push(line)
      end
    else
      cleaned_lines.push(line)
    end
  end

  File.write(podfile_restore_path, cleaned_lines.join)
end

.sanity_checkObject



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/pod_builder/podfile.rb', line 414

def self.sanity_check
  podfile_path = PodBuilder::basepath("Podfile")
  unless File.exist?(podfile_path)
    return
  end

  content = File.read(podfile_path)

  content.each_line do |line|
    stripped_line = strip_line(line)
    unless !stripped_line.start_with?("#")
      next
    end

    if stripped_line.match(/(pod')(.*?)(')/) != nil
      starting_def_found = stripped_line.start_with?("def") && (line.match("\s*def\s") != nil)
      raise "\n\nUnsupported single line def/pod. `def` and `pod` shouldn't be on the same line, please modify the following line:\n#{line}\n".red if starting_def_found
    end
  end

  unless content.include?("PodBuilder::Configuration::load")
    raise "\n\nUnsupported PodBuilder/Podfile found!\n\nStarting from version 5.x Podfile should contain the following lines:\nrequire 'pod_builder/core'\nPodBuilder::Configuration::load\n\nPlease manually add them to the top of your Podfile\n".red
  end
end

.strip_line(line) ⇒ Object



338
339
340
341
# File 'lib/pod_builder/podfile.rb', line 338

def self.strip_line(line)
  stripped_line = line.strip
  return stripped_line.gsub("\"", "'").gsub(" ", "").gsub("\t", "").gsub("\n", "")
end

.write_prebuilt(all_buildable_items, analyzer) ⇒ Object



238
239
240
241
242
243
244
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/pod_builder/podfile.rb', line 238

def self.write_prebuilt(all_buildable_items, analyzer)
  if Configuration.react_native_project
    return write_prebuilt_react_native(all_buildable_items, analyzer)
  end

  puts "Updating Application Podfile".yellow

  explicit_deps = analyzer.explicit_pods()
  explicit_deps.map! { |t| all_buildable_items.detect { |x| x.name == t.name } }
  explicit_deps.uniq!
  podbuilder_podfile_path = PodBuilder::basepath("Podfile")
  rel_path = Pathname.new(podbuilder_podfile_path).relative_path_from(Pathname.new(PodBuilder::project_path)).to_s

  podfile_content = File.read(podbuilder_podfile_path)

  exclude_lines = Podfile::PODBUILDER_LOCK_ACTION.map { |x| strip_line(x) }

  prebuilt_lines = ["# Autogenerated by PodBuilder (https://github.com/Subito-it/PodBuilder)\n", "# Any change to this file should be done on #{rel_path}\n", "\n"]
  podfile_content.each_line do |line|
    stripped_line = strip_line(line)
    if exclude_lines.include?(stripped_line)
      next
    end

    if pod_name = pod_definition_in(line, true)
      if podfile_item = all_buildable_items.detect { |x| x.name == pod_name }
        marker = podfile_item.prebuilt_marker()

        non_explicit_dependencies = podfile_item.recursive_dependencies(all_buildable_items) - explicit_deps
        non_explicit_dependencies_root_names = non_explicit_dependencies.map(&:root_name).uniq.filter { |t| t != podfile_item.root_name }
        non_explicit_dependencies = non_explicit_dependencies_root_names.map { |x|
          if item = all_buildable_items.detect { |t| x == t.name }
            item
          else
            item = all_buildable_items.detect { |t| x == t.root_name }
          end
        }.compact

        non_explicit_dependencies.each do |dep|
          dep_item = all_buildable_items.detect { |x| x.name == dep.name }

          if File.exist?(dep_item.prebuilt_podspec_path) && !dep_item.is_prebuilt
            pod_name = dep_item.prebuilt_entry(false, false)
            prebuilt_lines.push("#{line.detect_indentation}#{pod_name}#{marker}\n")
          end

          explicit_deps.push(dep)
        end

        if File.exist?(podfile_item.prebuilt_podspec_path) && !podfile_item.is_prebuilt
          prebuilt_lines.push("#{line.detect_indentation}#{podfile_item.prebuilt_entry}\n")
          next
        end
      end
    end

    prebuilt_lines.push(line)
  end

  podfile_content = prebuilt_lines.join

  podfile_content = Podfile.update_path_entries(podfile_content, Podfile.method(:podfile_path_transform))
  podfile_content = Podfile.update_project_entries(podfile_content, Podfile.method(:podfile_path_transform))
  podfile_content = Podfile.update_require_entries(podfile_content, Podfile.method(:podfile_path_transform))

  project_podfile_path = PodBuilder::project_path("Podfile")
  File.write(project_podfile_path, podfile_content)
end

.write_prebuilt_react_native(all_buildable_items, analyzer) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/pod_builder/podfile.rb', line 307

def self.write_prebuilt_react_native(all_buildable_items, analyzer)
  puts "Updating Application Podfile".yellow

  podbuilder_podfile_path = PodBuilder::basepath("Podfile")
  rel_path = Pathname.new(podbuilder_podfile_path).relative_path_from(Pathname.new(PodBuilder::project_path)).to_s

  podfile_content = ["# Autogenerated by PodBuilder (https://github.com/Subito-it/PodBuilder)\n", "# Any change to this file should be done on #{rel_path}\n", "\n"].join
  podfile_content += analyzer.podfile.pb_to_s(all_buildable_items)

  podfile_content = Podfile.update_path_entries(podfile_content, PodfileCP.method(:podfile_path_transform))
  podfile_content = Podfile.update_project_entries(podfile_content, Podfile.method(:podfile_path_transform))
  podfile_content = Podfile.update_require_entries(podfile_content, Podfile.method(:podfile_path_transform))

  project_podfile_path = PodBuilder::project_path("Podfile")
  File.write(project_podfile_path, podfile_content)
end

.write_restorable(updated_pods, podfile_items, analyzer) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
# File 'lib/pod_builder/podfile.rb', line 176

def self.write_restorable(updated_pods, podfile_items, analyzer)
  unless Configuration.restore_enabled && (podfile_items.count + updated_pods.count) > 0
    return
  end

  puts "Writing Restore Podfile".yellow

  podfile_items = podfile_items.dup
  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  podfile_path = PodBuilder::basepath("Podfile")

  if File.exist?(podfile_restore_path)
    restore_podfile_items = podfile_items_at(podfile_restore_path, include_prebuilt = true)

    podfile_items.map! { |podfile_item|
      if updated_pod = updated_pods.detect { |x| x.name == podfile_item.name }
        updated_pod
      elsif updated_pods.any? { |x| podfile_item.root_name == x.root_name } == false && # podfile_item shouldn't be among those being updated (including root specification)
            restored_pod = restore_podfile_items.detect { |x| x.name == podfile_item.name }
        restored_pod
      else
        podfile_item
      end
    }
  end

  result_targets = analyzer.instance_variable_get("@result").targets.map(&:name)
  podfile_content = ["# Autogenerated by PodBuilder (https://github.com/Subito-it/PodBuilder)", "# Please don't modify this file", "\n"]
  podfile_content += analyzer.podfile.sources.map { |x| "source '#{x}'" }
  podfile_content += ["", "use_frameworks!", ""]

  # multiple platforms not (yet) supported
  # https://github.com/CocoaPods/Rome/issues/37
  platform = analyzer.instance_variable_get("@result").targets.first.platform
  podfile_content += ["platform :#{platform.name}, '#{platform.deployment_target.version}'", ""]

  analyzer.instance_variable_get("@result").specs_by_target.each do |target, specifications|
    target_name = target.name.to_s

    unless result_targets.select { |x| x.end_with?(target_name) }.count > 0
      next
    end

    podfile_content.push("target '#{target_name}' do")

    if project_path = target.user_project_path
      podfile_content.push("\tproject '#{project_path}'")
    end

    specifications.each do |spec|
      item = podfile_items.detect { |x| x.name == spec.name }
      if podfile_items.map(&:name).include?(spec.name)
        podfile_content.push("\t#{item.entry}")
      end
    end

    podfile_content.push("end\n")
  end

  File.write(podfile_restore_path, podfile_content.join("\n"))
end