Skip to content

Commit

Permalink
CocoaPods: Consolidate Hermes and JSC Engine configuration in jsengin…
Browse files Browse the repository at this point in the history
…e.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
  • Loading branch information
hramos authored and OlimpiaZurek committed May 22, 2023
1 parent 1e5a5cb commit 508dbcf
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 117 deletions.
5 changes: 2 additions & 3 deletions scripts/cocoapods/__tests__/fabric-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 0 additions & 86 deletions scripts/cocoapods/__tests__/hermes-test.rb

This file was deleted.

124 changes: 124 additions & 0 deletions scripts/cocoapods/__tests__/jsengine-test.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions scripts/cocoapods/fabric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 0 additions & 20 deletions scripts/cocoapods/hermes.rb

This file was deleted.

40 changes: 40 additions & 0 deletions scripts/cocoapods/jsengine.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 9 additions & 6 deletions scripts/react_native_pods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 508dbcf

Please sign in to comment.