| 1 | const fs = require("node:fs"); |
| 2 | const path = require("node:path"); |
| 3 | |
| 4 | const { withDangerousMod } = require("expo/config-plugins"); |
| 5 | |
| 6 | const MARKER = "# t3code: repair cached CocoaPods UUID allocation before SPM integration"; |
| 7 | const UUID_REPAIR = `${MARKER} |
| 8 | pods_project = installer.pods_project |
| 9 | existing_uuids = pods_project.objects.map(&:uuid) |
| 10 | uuid_prefix = pods_project.instance_variable_get(:@uuid_prefix)[0, 6] |
| 11 | sequential_uuid = /\\A#{Regexp.escape(uuid_prefix)}([0-9A-F]{7})0\\z/ |
| 12 | highest_index = existing_uuids.filter_map do |uuid| |
| 13 | match = sequential_uuid.match(uuid) |
| 14 | match && match[1].to_i(16) |
| 15 | end.max || -1 |
| 16 | |
| 17 | # Pod::Project generates sequential UUIDs without collision checks because CocoaPods |
| 18 | # normally creates the project in this process. EAS can restore Pods.xcodeproj from |
| 19 | # cache, leaving the allocator behind the loaded objects. React Native's SPM support |
| 20 | # then reuses an existing UUID and silently corrupts the project graph. |
| 21 | next_index = highest_index + 1 |
| 22 | pods_project.instance_variable_set(:@generated_uuids, Array.new(next_index)) |
| 23 | pods_project.instance_variable_set(:@available_uuids, []) |
| 24 | pods_project.generate_available_uuid_list(1_000) |
| 25 | Pod::UI.puts "T3Code: reset CocoaPods UUID allocator at #{next_index} (#{existing_uuids.length} existing objects)" |
| 26 | `; |
| 27 | |
| 28 | module.exports = function withIosCocoaPodsUuidCache(config) { |
| 29 | return withDangerousMod(config, [ |
| 30 | "ios", |
| 31 | (nextConfig) => { |
| 32 | const podfilePath = path.join(nextConfig.modRequest.platformProjectRoot, "Podfile"); |
| 33 | const podfile = fs.readFileSync(podfilePath, "utf8"); |
| 34 | |
| 35 | if (podfile.includes(MARKER)) { |
| 36 | return nextConfig; |
| 37 | } |
| 38 | |
| 39 | const postInstallStart = "post_install do |installer|\n"; |
| 40 | if (!podfile.includes(postInstallStart)) { |
| 41 | throw new Error("Unable to repair CocoaPods UUID allocation: post_install is missing."); |
| 42 | } |
| 43 | |
| 44 | fs.writeFileSync( |
| 45 | podfilePath, |
| 46 | podfile.replace(postInstallStart, `${postInstallStart}${UUID_REPAIR}`), |
| 47 | "utf8", |
| 48 | ); |
| 49 | return nextConfig; |
| 50 | }, |
| 51 | ]); |
| 52 | }; |