From 31ac73acbc992c47f9f5e88b4c98fac4a8778406 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 13 Sep 2023 03:14:00 -0700 Subject: [PATCH] Create Helper to read the package.json from Cocoapods (#39390) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39390 This diff introduce an helper to read the content of the React Native package of json from ruby. ## Changelog: [iOS][Added] - Add helper to read the package.json from the cocoapods script. Reviewed By: dmytrorykun Differential Revision: D49146946 fbshipit-source-id: cd25052d743a61fcfabbe197b701188ec824709e --- .../__tests__/new_architecture-test.rb | 30 +++++++++++++++++++ .../scripts/cocoapods/new_architecture.rb | 11 +++++++ 2 files changed, 41 insertions(+) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index d5db796e7cabcf..7a14ef4a993cee 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -8,10 +8,12 @@ require_relative "./test_utils/InstallerMock.rb" require_relative "./test_utils/PodMock.rb" require_relative "./test_utils/SpecMock.rb" +require_relative "./test_utils/FileMock.rb" class NewArchitectureTests < Test::Unit::TestCase def teardown Pod::UI.reset() + FileMock.reset() end # ============================= # @@ -266,6 +268,34 @@ def test_isNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue assert_equal("1", isEnabled) end + + # =================================== # + # Test - Extract React Native Version # + # =================================== # + def test_extractReactNativeVersion_whenFileDoesNotExists_raiseError() + react_native_path = './node_modules/react-native/' + + exception = assert_raise(RuntimeError) do + NewArchitectureHelper.extract_react_native_version(react_native_path, :file_manager => FileMock) + end + + assert_equal("Couldn't find the React Native package.json file at ./node_modules/react-native/package.json", exception.message) + end + + def test_extractReactNativeVersion_whenFileExists_returnTheRightVersion() + react_native_path = "./node_modules/react-native/" + full_path = File.join(react_native_path, "package.json") + json = "{\"version\": \"1.0.0-prealpha.0\"}" + FileMock.mocked_existing_files([full_path]) + FileMock.files_to_read({ + full_path => json + }) + + version = NewArchitectureHelper.extract_react_native_version(react_native_path, :file_manager => FileMock) + + assert_equal("1.0.0-prealpha.0", version) + end + end # ================ # diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index b98682ab20e1c2..3ae547ed0264f2 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -3,6 +3,8 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +require 'json' + class NewArchitectureHelper @@shared_flags = "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1" @@ -159,6 +161,15 @@ def self.folly_compiler_flags return @@folly_compiler_flags end + def self.extract_react_native_version(react_native_path, file_manager: File, json_parser: JSON) + package_json_file = File.join(react_native_path, "package.json") + if !file_manager.exist?(package_json_file) + raise "Couldn't find the React Native package.json file at #{package_json_file}" + end + package = json_parser.parse(file_manager.read(package_json_file)) + return package["version"] + end + def self.is_new_arch_enabled(new_arch_enabled, react_native_version) # Regex that identify a version with the syntax `..[-[.-]k] # where