diff --git a/scripts/cocoapods/__tests__/fabric-test.rb b/scripts/cocoapods/__tests__/fabric-test.rb index 5076c70e7085b1..9ded6bd22028e7 100644 --- a/scripts/cocoapods/__tests__/fabric-test.rb +++ b/scripts/cocoapods/__tests__/fabric-test.rb @@ -26,19 +26,18 @@ def test_setupFabric_installsPods prefix = "../.." # Act - setup_fabric!(prefix) + setup_fabric!(:react_native_path => prefix) # Assert check_installed_pods(prefix) end def check_installed_pods(prefix) - assert_equal($podInvocationCount, 6) + assert_equal($podInvocationCount, 5) check_pod("React-Fabric", :path => "#{prefix}/ReactCommon") check_pod("React-rncore", :path => "#{prefix}/ReactCommon") check_pod("React-graphics", :path => "#{prefix}/ReactCommon/react/renderer/graphics") - check_pod("React-jsc/Fabric", :path => "#{prefix}/ReactCommon/jsi") check_pod("React-RCTFabric", :path => "#{prefix}/React", :modular_headers => true) check_pod("RCT-Folly/Fabric", :podspec => "#{prefix}/third-party-podspecs/RCT-Folly.podspec") end diff --git a/scripts/cocoapods/__tests__/hermes-test.rb b/scripts/cocoapods/__tests__/hermes-test.rb deleted file mode 100644 index 64562ec5d22d7d..00000000000000 --- a/scripts/cocoapods/__tests__/hermes-test.rb +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -require "test/unit" -require_relative "../hermes.rb" -require_relative "./test_utils/podSpy.rb" -require_relative "./test_utils/PodMock.rb" -require_relative "./test_utils/Open3Mock.rb" - -class HermesTests < Test::Unit::TestCase - - :prefix - - def setup - @prefix = "../.." - end - - def teardown - Open3.reset() - Pod::Config.reset() - Pod::UI.reset() - podSpy_cleanUp() - end - - # ============================= # - # TEST - installHermesIfEnabled # - # ============================= # - def test_installHermesIfEnabled_whenHermesIsDisabled_doesNothing - # Arrange - - # Act - install_hermes_if_enabled(false, @prefix) - - # Assert - assert_equal($podInvocationCount, 0) - assert_equal($podInvocation, {}) - assert_equal(Pod::UI.collected_infoes, []) - assert_equal(Open3.collected_commands, []) - assert_equal(Open3.collected_dirs, []) - end - - def test_installHermesIfEnabled_whenHermesIsEnabledAndHermesScriptFails_abort - # Arrange - Pod::Config.instance.installation_root.set_installation_root("Pods/") - Open3.set_returned_status(1) - Open3.set_returned_text("This test\nshould fail") - - # Act - assert_raises { - install_hermes_if_enabled(true, @prefix) - } - - # Assert - assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"]) - assert_equal(Open3.collected_dirs, ["Pods/../.."]) - assert_equal(Pod::UI.collected_infoes, ["This test", "should fail"]) - assert_equal($podInvocationCount, 0) - assert_equal($podInvocation, {}) - end - - def test_installHermesIfEnabled_whenHermesIsEnabledAndHermesScriptSucceeds_installsPods - # Arrange - Pod::Config.instance.installation_root.set_installation_root("Pods/") - Open3.set_returned_status(0) - Open3.set_returned_text("This is\nthe text\nreturned by\nprepare-hermes-for-build") - - # Act - install_hermes_if_enabled(true, @prefix) - - # Assert - assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"]) - assert_equal(Open3.collected_dirs, ["Pods/../.."]) - assert_equal(Pod::UI.collected_infoes, [ - "This is", - "the text", - "returned by", - "prepare-hermes-for-build", - ]) - assert_equal($podInvocationCount, 3) - assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes") - assert_equal($podInvocation["libevent"][:version], "~> 2.1.12") - assert_equal($podInvocation["hermes-engine"][:podspec], "../../sdks/hermes/hermes-engine.podspec") - end -end diff --git a/scripts/cocoapods/__tests__/jsengine-test.rb b/scripts/cocoapods/__tests__/jsengine-test.rb new file mode 100644 index 00000000000000..51efcef5da565d --- /dev/null +++ b/scripts/cocoapods/__tests__/jsengine-test.rb @@ -0,0 +1,124 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +require "test/unit" +require_relative "../jsengine.rb" +require_relative "./test_utils/podSpy.rb" +require_relative "./test_utils/PodMock.rb" +require_relative "./test_utils/Open3Mock.rb" + +class JSEngineTests < Test::Unit::TestCase + + :react_native_path + + def setup + @react_native_path = "../.." + podSpy_cleanUp() + end + + def teardown + Open3.reset() + Pod::Config.reset() + Pod::UI.reset() + podSpy_cleanUp() + ENV['USE_HERMES'] = '1' + end + + # =============== # + # TEST - setupJsc # + # =============== # + def test_setupJsc_installsPods + # Arrange + fabric_enabled = false + + # Act + setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled) + + # Assert + assert_equal($podInvocationCount, 2) + assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsi") + end + + def test_setupJsc_installsPods_installsFabricSubspecWhenFabricEnabled + # Arrange + fabric_enabled = true + + # Act + setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled) + + # Assert + assert_equal($podInvocationCount, 3) + assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-jsc/Fabric"][:path], "../../ReactCommon/jsi") + end + + # ================== # + # TEST - setupHermes # + # ================== # + def test_setupHermes_whenHermesScriptFails_abort + # Arrange + fabric_enabled = false + Pod::Config.instance.installation_root.set_installation_root("Pods/") + Open3.set_returned_status(1) + Open3.set_returned_text("This test\nshould fail") + + # Act + assert_raises { + setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled) + } + + # Assert + assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"]) + assert_equal(Open3.collected_dirs, ["Pods/../.."]) + assert_equal(Pod::UI.collected_infoes, ["This test", "should fail"]) + assert_equal($podInvocationCount, 0) + assert_equal($podInvocation, {}) + end + + def test_setupHermes_whenHermesScriptSucceeds_installsPods + # Arrange + fabric_enabled = false + Pod::Config.instance.installation_root.set_installation_root("Pods/") + Open3.set_returned_status(0) + Open3.set_returned_text("This is\nthe text\nreturned by\nprepare-hermes-for-build") + + # Act + setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled) + + # Assert + assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"]) + assert_equal(Open3.collected_dirs, ["Pods/../.."]) + assert_equal(Pod::UI.collected_infoes, [ + "This is", + "the text", + "returned by", + "prepare-hermes-for-build", + ]) + assert_equal($podInvocationCount, 5) + assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes") + assert_equal($podInvocation["libevent"][:version], "~> 2.1.12") + assert_equal($podInvocation["hermes-engine"][:podspec], "../../sdks/hermes/hermes-engine.podspec") + end + + def test_setupHermes_installsPods_installsFabricSubspecWhenFabricEnabled + # Arrange + fabric_enabled = true + + # Act + setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled) + + # Assert + assert_equal($podInvocationCount, 6) + assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["React-jsc/Fabric"][:path], "../../ReactCommon/jsi") + assert_equal($podInvocation["hermes-engine"][:podspec], "../../sdks/hermes/hermes-engine.podspec") + assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes") + assert_equal($podInvocation["libevent"][:version], "~> 2.1.12") + end +end diff --git a/scripts/cocoapods/fabric.rb b/scripts/cocoapods/fabric.rb index bcdb2960df58e8..b694bb64d97210 100644 --- a/scripts/cocoapods/fabric.rb +++ b/scripts/cocoapods/fabric.rb @@ -7,11 +7,10 @@ # It sets up the Fabric dependencies. # # @parameter react_native_path: relative path to react-native -def setup_fabric!(react_native_path) +def setup_fabric!(react_native_path: "../node_modules/react-native") pod 'React-Fabric', :path => "#{react_native_path}/ReactCommon" pod 'React-rncore', :path => "#{react_native_path}/ReactCommon" pod 'React-graphics', :path => "#{react_native_path}/ReactCommon/react/renderer/graphics" - pod 'React-jsc/Fabric', :path => "#{react_native_path}/ReactCommon/jsi" pod 'React-RCTFabric', :path => "#{react_native_path}/React", :modular_headers => true pod 'RCT-Folly/Fabric', :podspec => "#{react_native_path}/third-party-podspecs/RCT-Folly.podspec" end diff --git a/scripts/cocoapods/hermes.rb b/scripts/cocoapods/hermes.rb deleted file mode 100644 index 747b744c87d7df..00000000000000 --- a/scripts/cocoapods/hermes.rb +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -def install_hermes_if_enabled(hermes_enabled, react_native_path) - unless hermes_enabled - return - end - - prepare_hermes = 'node scripts/hermes/prepare-hermes-for-build' - react_native_dir = Pod::Config.instance.installation_root.join(react_native_path) - prep_output, prep_status = Open3.capture2e(prepare_hermes, :chdir => react_native_dir) - prep_output.split("\n").each { |line| Pod::UI.info line } - abort unless prep_status == 0 - - pod 'React-hermes', :path => "#{react_native_path}/ReactCommon/hermes" - pod 'libevent', '~> 2.1.12' - pod 'hermes-engine', :podspec => "#{react_native_path}/sdks/hermes/hermes-engine.podspec" -end diff --git a/scripts/cocoapods/jsengine.rb b/scripts/cocoapods/jsengine.rb new file mode 100644 index 00000000000000..a07ff842806c5d --- /dev/null +++ b/scripts/cocoapods/jsengine.rb @@ -0,0 +1,40 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +require_relative './utils.rb' + +# It sets up the JavaScriptCore and JSI pods. +# +# @parameter react_native_path: relative path to react-native +# @parameter fabric_enabled: whether Fabirc is enabled +def setup_jsc!(react_native_path: "../node_modules/react-native", fabric_enabled: false) + pod 'React-jsi', :path => "#{react_native_path}/ReactCommon/jsi" + pod 'React-jsc', :path => "#{react_native_path}/ReactCommon/jsi" + if fabric_enabled + pod 'React-jsc/Fabric', :path => "#{react_native_path}/ReactCommon/jsi" + end +end + +# It sets up the Hermes and JSI pods. +# +# @parameter react_native_path: relative path to react-native +# @parameter fabric_enabled: whether Fabirc is enabled +def setup_hermes!(react_native_path: "../node_modules/react-native", fabric_enabled: false) + # The following captures the output of prepare_hermes for use in tests + prepare_hermes = 'node scripts/hermes/prepare-hermes-for-build' + react_native_dir = Pod::Config.instance.installation_root.join(react_native_path) + prep_output, prep_status = Open3.capture2e(prepare_hermes, :chdir => react_native_dir) + prep_output.split("\n").each { |line| Pod::UI.info line } + abort unless prep_status == 0 + + pod 'React-jsi', :path => "#{react_native_path}/ReactCommon/jsi" + pod 'React-jsc', :path => "#{react_native_path}/ReactCommon/jsi" + if fabric_enabled + pod 'React-jsc/Fabric', :path => "#{react_native_path}/ReactCommon/jsi" + end + pod 'hermes-engine', :podspec => "#{react_native_path}/sdks/hermes/hermes-engine.podspec" + pod 'React-hermes', :path => "#{react_native_path}/ReactCommon/hermes" + pod 'libevent', '~> 2.1.12' +end diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 983e7c5763d89b..995b6ca3e636e4 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -7,7 +7,7 @@ require 'open3' require 'pathname' require_relative './react_native_pods_utils/script_phases.rb' -require_relative './cocoapods/hermes.rb' +require_relative './cocoapods/jsengine.rb' require_relative './cocoapods/flipper.rb' require_relative './cocoapods/fabric.rb' require_relative './cocoapods/codegen.rb' @@ -95,11 +95,16 @@ def use_react_native! ( pod 'React-bridging', :path => "#{prefix}/ReactCommon" pod 'React-cxxreact', :path => "#{prefix}/ReactCommon/cxxreact" - pod 'React-jsi', :path => "#{prefix}/ReactCommon/jsi" - pod 'React-jsc', :path => "#{prefix}/ReactCommon/jsi" + + if hermes_enabled + setup_hermes!(:react_native_path => prefix, :fabric_enabled => fabric_enabled) + else + setup_jsc!(:react_native_path => prefix, :fabric_enabled => fabric_enabled) + end pod 'React-jsidynamic', :path => "#{prefix}/ReactCommon/jsi" pod 'React-jsiexecutor', :path => "#{prefix}/ReactCommon/jsiexecutor" pod 'React-jsinspector', :path => "#{prefix}/ReactCommon/jsinspector" + pod 'React-callinvoker', :path => "#{prefix}/ReactCommon/callinvoker" pod 'React-runtimeexecutor', :path => "#{prefix}/ReactCommon/runtimeexecutor" pod 'React-perflogger', :path => "#{prefix}/ReactCommon/reactperflogger" @@ -128,11 +133,9 @@ def use_react_native! ( if fabric_enabled checkAndGenerateEmptyThirdPartyProvider!(prefix, new_arch_enabled, $CODEGEN_OUTPUT_DIR) - setup_fabric!(prefix) + setup_fabric!(:react_native_path => prefix) end - install_hermes_if_enabled(hermes_enabled, prefix) - # CocoaPods `configurations` option ensures that the target is copied only for the specified configurations, # but those dependencies are still built. # Flipper doesn't currently compile for release https://github.com/facebook/react-native/issues/33764