From 34fafb2b881751cdd998d7d5ef486d536607c9ce Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 14 Sep 2022 04:21:02 -0700 Subject: [PATCH] Simplify furthermore App migration Summary: This Diff adds a couple of APIs to the `react_native_pods.rb` file to simplify further the migration to the New Architecture for the iOS app. Specifically, it aims to simplify [this](https://reactnative.dev/docs/next/new-architecture-app-intro#ios---build-the-project) steps by offering a RN-managed min version and creating the .xcode.env file for the user if it is missing. ## Changelog [iOS][Added] - Add new API to simplify app migration Reviewed By: cortinico Differential Revision: D39469599 fbshipit-source-id: f0323e86c83c2731671fcd5bb4288071304bb43b --- packages/rn-tester/Podfile | 6 ++-- scripts/cocoapods/__tests__/utils-test.rb | 36 +++++++++++++++++++++++ scripts/cocoapods/utils.rb | 8 +++++ scripts/react_native_pods.rb | 17 +++++++++++ template/ios/Podfile | 4 +-- 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/packages/rn-tester/Podfile b/packages/rn-tester/Podfile index 5f1a116f147..386a55c6e09 100644 --- a/packages/rn-tester/Podfile +++ b/packages/rn-tester/Podfile @@ -1,11 +1,9 @@ require_relative '../../scripts/react_native_pods' source 'https://cdn.cocoapods.org/' -platform :ios, '12.4' +platform :ios, min_ios_version_supported -# Temporary solution to suppress duplicated GUID error. -# Can be removed once we move to generate files outside pod install. -install! 'cocoapods', :deterministic_uuids => false +prepare_react_native_project! USE_FRAMEWORKS = ENV['USE_FRAMEWORKS'] == '1' IN_CI = ENV['CI'] == 'true' diff --git a/scripts/cocoapods/__tests__/utils-test.rb b/scripts/cocoapods/__tests__/utils-test.rb index 0cd6757ebc9..5d23f65a0a6 100644 --- a/scripts/cocoapods/__tests__/utils-test.rb +++ b/scripts/cocoapods/__tests__/utils-test.rb @@ -10,14 +10,27 @@ require_relative "./test_utils/PodMock.rb" require_relative "./test_utils/InstallerMock.rb" require_relative "./test_utils/EnvironmentMock.rb" require_relative "./test_utils/SysctlCheckerMock.rb" +require_relative "./test_utils/FileMock.rb" +require_relative "./test_utils/systemUtils.rb" +require_relative "./test_utils/PathnameMock.rb" class UtilsTests < Test::Unit::TestCase + def setup + @base_path = "~/app/ios" + Pathname.pwd!(@base_path) + File.enable_testing_mode! + end + def teardown + File.reset() Pod::UI.reset() + Pathname.reset() + Pod::Config.reset() SysctlChecker.reset() Environment.reset() ENV['RCT_NEW_ARCH_ENABLED'] = '0' ENV['USE_HERMES'] = '1' + system_reset_commands end # ======================= # @@ -443,6 +456,29 @@ class UtilsTests < Test::Unit::TestCase assert_equal(pods_projects_mock.save_invocation_count, 1) assert_equal(Pod::UI.collected_messages, ["Setting REACT_NATIVE build settings"]) end + + # =================================== # + # Test - Prepare React Native Project # + # =================================== # + def test_createXcodeEnvIfMissing_whenItIsPresent_doNothing + # Arrange + File.mocked_existing_files("/.xcode.env") + # Act + ReactNativePodsUtils.create_xcode_env_if_missing + # Assert + assert_equal(File.exist_invocation_params, ["/.xcode.env"]) + assert_equal($collected_commands, []) + end + + def test_createXcodeEnvIfMissing_whenItIsNotPresent_createsIt + # Arrange + + # Act + ReactNativePodsUtils.create_xcode_env_if_missing + # Assert + assert_equal(File.exist_invocation_params, ["/.xcode.env"]) + assert_equal($collected_commands, ["echo 'export NODE_BINARY=$(command -v node)' > /.xcode.env"]) + end end def prepare_empty_user_project_mock diff --git a/scripts/cocoapods/utils.rb b/scripts/cocoapods/utils.rb index 7b5cc32bb45..0f80b42226e 100644 --- a/scripts/cocoapods/utils.rb +++ b/scripts/cocoapods/utils.rb @@ -152,5 +152,13 @@ class ReactNativePodsUtils end end + def self.create_xcode_env_if_missing + relative_path = Pod::Config.instance.installation_root.relative_path_from(Pathname.pwd) + file_path = File.join(relative_path, '.xcode.env') + if File.exist?(file_path) + return + end + system("echo 'export NODE_BINARY=$(command -v node)' > #{file_path}") + end end diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 4cf084a7c69..5922794f6d8 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -23,6 +23,23 @@ $FOLLY_VERSION = '2021.07.22.00' $START_TIME = Time.now.to_i +# This function returns the min iOS version supported by React Native +# By using this function, you won't have to manualy change your Podfile +# when we change the minimum version supported by the framework. +def min_ios_version_supported + return '12.4' +end + +# This function prepares the project for React Native, before processing +# all the target exposed by the framework. +def prepare_react_native_project! + # Temporary solution to suppress duplicated GUID error. + # Can be removed once we move to generate files outside pod install. + install! 'cocoapods', :deterministic_uuids => false + + ReactNativePodsUtils.create_xcode_env_if_missing +end + # Function that setup all the react native dependencies #  # Parameters diff --git a/template/ios/Podfile b/template/ios/Podfile index 006397252e5..579715789c9 100644 --- a/template/ios/Podfile +++ b/template/ios/Podfile @@ -1,8 +1,8 @@ require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/react-native/scripts/native_modules' -platform :ios, '12.4' -install! 'cocoapods', :deterministic_uuids => false +platform :ios, min_ios_version_supported +prepare_react_native_project! flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled