Skip to content

Commit

Permalink
Create Helper to read the package.json from Cocoapods (#39390)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Sep 13, 2023
1 parent 67b3622 commit 31ac73a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

# ============================= #
Expand Down Expand Up @@ -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

# ================ #
Expand Down
11 changes: 11 additions & 0 deletions packages/react-native/scripts/cocoapods/new_architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 `<major>.<minor>.<patch>[-<prerelease>[.-]k]
# where
Expand Down

0 comments on commit 31ac73a

Please sign in to comment.