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
This commit is contained in:
Riccardo Cipolleschi
2023-09-13 03:14:00 -07:00
committed by Facebook GitHub Bot
parent 67b3622ee1
commit 31ac73acbc
2 changed files with 41 additions and 0 deletions
@@ -8,10 +8,12 @@ require_relative "../new_architecture.rb"
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 @@ class NewArchitectureTests < Test::Unit::TestCase
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
# ================ #
@@ -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 @@ class NewArchitectureHelper
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 `<major>.<minor>.<patch>[-<prerelease>[.-]k]
# where