1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5
6# Monkeypatch of `Pod::Lockfile` to ensure automatic update of dependencies integrated with a local podspec when their version changed.
7# This is necessary because local podspec dependencies must be otherwise manually updated.
8module LocalPodspecPatch
9    # Returns local podspecs whose versions differ from the one in the `react-native` package.
10    def self.pods_to_update(react_native_path: "../node_modules/react-native")
11        @@local_podspecs = Dir.glob("#{react_native_path}/third-party-podspecs/*").map { |file| File.basename(file, ".podspec") }
12        @@local_podspecs = @@local_podspecs.select do |podspec_name|
13
14            # Read local podspec to determine the cached version
15            local_podspec_path = File.join(
16                Dir.pwd, "Pods/Local Podspecs/#{podspec_name}.podspec.json"
17            )
18
19            # Local podspec cannot be outdated if it does not exist, yet
20            next unless File.exist?(local_podspec_path)
21
22            local_podspec = File.read(local_podspec_path)
23            local_podspec_json = JSON.parse(local_podspec)
24            local_version = local_podspec_json["version"]
25
26            # Read the version from a podspec from the `react-native` package
27            podspec_path = "#{react_native_path}/third-party-podspecs/#{podspec_name}.podspec"
28            current_podspec = Pod::Specification.from_file(podspec_path)
29            current_version = current_podspec.version.to_s
30            current_version != local_version
31      end
32      @@local_podspecs
33    end
34
35    # Patched `detect_changes_with_podfile` method
36    def detect_changes_with_podfile(podfile)
37      Pod::UI.puts "Invoke detect_changes_with_podfile patched method".red
38      changes = super(podfile)
39      return patch_detect_changes_with_podfile(changes)
40    end
41
42    def patch_detect_changes_with_podfile(changes)
43        @@local_podspecs.each do |local_podspec|
44            next unless changes[:unchanged].include?(local_podspec)
45
46            changes[:unchanged].delete(local_podspec)
47            changes[:changed] << local_podspec
48        end
49        changes
50    end
51end
52