From 22e4bdf10a5a1817d9dd4641b3c2ceef13867556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Thu, 20 Oct 2022 14:14:23 -0700 Subject: [PATCH] CocoaPods: Consolidate Hermes and JSC Engine configuration in jsengine.rb Summary: Move JSC and Hermes CocoaPods configuration to scripts/cocoapods/jsengine.rb. This file provides a setup_jsc! function and a setup_hermes! function that will configure the necessary pod dependencies for each of the supported JavaScript engines. Currently, the JSI and JSC pods are installed in both cases. This will likely change in upcoming changes to how Hermes and JSC are configured. Changelog: [internal] Reviewed By: dmytrorykun Differential Revision: D40459234 fbshipit-source-id: d6c89c7f650b1efcce5622594db7fd726eafc2bc --- scripts/cocoapods/__tests__/fabric-test.rb | 5 +- scripts/cocoapods/__tests__/hermes-test.rb | 86 ------------- scripts/cocoapods/__tests__/jsengine-test.rb | 124 +++++++++++++++++++ scripts/cocoapods/fabric.rb | 3 +- scripts/cocoapods/hermes.rb | 20 --- scripts/cocoapods/jsengine.rb | 40 ++++++ scripts/react_native_pods.rb | 15 ++- 7 files changed, 176 insertions(+), 117 deletions(-) delete mode 100644 scripts/cocoapods/__tests__/hermes-test.rb create mode 100644 scripts/cocoapods/__tests__/jsengine-test.rb delete mode 100644 scripts/cocoapods/hermes.rb create mode 100644 scripts/cocoapods/jsengine.rb diff --git a/scripts/cocoapods/__tests__/fabric-test.rb b/scripts/cocoapods/__tests__/fabric-test.rb index 5076c70e708..9ded6bd2202 100644 --- a/scripts/cocoapods/__tests__/fabric-test.rb +++ b/scripts/cocoapods/__tests__/fabric-test.rb @@ -26,19 +26,18 @@ class FabricTest < Test::Unit::TestCase 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 64562ec5d22..00000000000 --- 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 00000000000..51efcef5da5 --- /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 bcdb2960df5..b694bb64d97 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 747b744c87d..00000000000 --- 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 00000000000..a07ff842806 --- /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 983e7c5763d..995b6ca3e63 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -7,7 +7,7 @@ require 'json' 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