From b8f44089706e17f19ca98e6bd3dec8dde13d2df0 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 12 Sep 2023 06:04:28 -0700 Subject: [PATCH] Create helper to enforce the New Arch enabled for double released (#39387) Summary: This change creates an helper function and tests to set the right value for the new arch enabled flag based on the react native version ## Changelog: [iOS][Added] - add helper to set New Arch enabled flag based on RN version. Reviewed By: dmytrorykun Differential Revision: D49145515 --- .../__tests__/new_architecture-test.rb | 85 +++++++++++++++++++ .../scripts/cocoapods/new_architecture.rb | 23 +++++ 2 files changed, 108 insertions(+) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index f2570238226a01..d5db796e7cabcf 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -181,6 +181,91 @@ def test_installModulesDependencies_whenNewArchDisabledAndSearchPathsAndCompiler ] ) end + + # ========================== # + # Test - Is New Arch Enabled # + # ========================== # + + def test_isNewArchEnabled_whenOnMainAndFlagTrue_returnTrue + version = '1000.0.0' + new_arch_enabled = true + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOnMainAndFlagFalse_returnFalse + version = '1000.0.0' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("0", isEnabled) + end + + def test_isNewArchEnabled_whenOnStableAndFlagTrue_returnTrue + version = '0.73.0' + new_arch_enabled = true + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOnStableAndFlagFalse_returnFalse + version = '0.73.0' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("0", isEnabled) + end + + def test_isNewArchEnabled_whenOn100AndFlagTrue_returnTrue + version = '1.0.0-prealpha.0' + new_arch_enabled = true + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalse_returnTrue + version = '1.0.0-prealpha.0' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue + version = '1.0.0-prealpha-0' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOn100PrealphaOnlyWordsAndFlagFalse_returnTrue + version = '1.0.0-prealpha0' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end + + def test_isNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue + version = '3.2.1' + new_arch_enabled = false + + isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version) + + assert_equal("1", isEnabled) + end end # ================ # diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index 956582032070fa..b98682ab20e1c2 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -158,4 +158,27 @@ def self.install_modules_dependencies(spec, new_arch_enabled, folly_version) def self.folly_compiler_flags return @@folly_compiler_flags end + + def self.is_new_arch_enabled(new_arch_enabled, react_native_version) + # Regex that identify a version with the syntax `..[-[.-]k] + # where + # - major is a number + # - minor is a number + # - patch is a number + # - prerelease is a string (can include numbers) + # - k is a number + version_regex = /^(\d+)\.(\d+)\.(\d+)(?:-(\w+(?:[-.]\d+)?))?$/ + + if match_data = react_native_version.match(version_regex) + + major = match_data[1].to_i + + # We want to enforce the new architecture for 1.0.0 and greater, + # but not for 1000 as version 1000 is currently main. + if major > 0 && major < 1000 + return "1" + end + end + return new_arch_enabled ? "1" : "0" + end end