Skip to content

Commit

Permalink
Fix functions that turn on the New Architecture (#40770)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #40770

## Changelog:
[Internal] - Fix functions that turn on the New Architecture

Reviewed By: cortinico

Differential Revision: D50084427

fbshipit-source-id: aae0c77585929eacf8e890321e27e5049e53775e
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Oct 10, 2023
1 parent b1356fe commit 5c2ec55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ internal object ProjectUtils {
return false
}

val major = matchResult.groupValues[1].toInt()
return major > 0 && major < 1000
val prerelease = matchResult.groupValues[4].toString()
return prerelease.contains("prealpha")
}

internal fun Project.shouldWarnIfNewArchFlagIsSetInPrealpha(extension: ReactExtension): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ class ProjectUtilsTest {
}

@Test
fun isNewArchEnabled_withRNVersion1_returnTrue() {
fun isNewArchEnabled_withRNVersionPrealpha_returnTrue() {
val project = createProject()
val extension = TestReactExtension(project)
File(tempFolder.root, "package.json").apply {
writeText(
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-202310916"
}
"""
.trimIndent())
Expand Down Expand Up @@ -322,7 +322,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToFalseAndOnMajor_returnTrue() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToFalseAndOnPrealpha_returnTrue() {
val project = createProject()
project.extensions.extraProperties.set("newArchEnabled", "false")
val extension = TestReactExtension(project)
Expand All @@ -331,7 +331,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -341,7 +341,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToFalseAndOnMajor_returnTrue() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToFalseAndOnPrealpha_returnTrue() {
val project = createProject()
project.extensions.extraProperties.set("react.newArchEnabled", "false")
val extension = TestReactExtension(project)
Expand All @@ -350,7 +350,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -360,7 +360,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToFalseAndOnMajor_returnTrue() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToFalseAndOnPrealpha_returnTrue() {
val project = createProject()
project.extensions.extraProperties.set("newArchEnabled", "false")
project.extensions.extraProperties.set("react.newArchEnabled", "false")
Expand All @@ -370,7 +370,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -380,7 +380,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("newArchEnabled", "true")
val extension = TestReactExtension(project)
Expand All @@ -389,7 +389,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -399,7 +399,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("react.newArchEnabled", "true")
val extension = TestReactExtension(project)
Expand All @@ -408,7 +408,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -418,7 +418,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("newArchEnabled", "true")
project.extensions.extraProperties.set("react.newArchEnabled", "true")
Expand All @@ -428,7 +428,7 @@ class ProjectUtilsTest {
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -438,15 +438,15 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndOnPrealpha_returnFalse() {
val project = createProject()
val extension = TestReactExtension(project)
File(tempFolder.root, "package.json").apply {
writeText(
// language=json
"""
{
"version": "1.2.3"
"version": "0.0.0-prealpha-2023100915"
}
"""
.trimIndent())
Expand All @@ -456,7 +456,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndNotOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNewArchIsSetToTrueAndNotOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("newxArchEnabled", "true")
val extension = TestReactExtension(project)
Expand All @@ -475,7 +475,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndNotOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenScopedNewArchIsSetToTrueAndNotOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("react.newxArchEnabled", "true")
val extension = TestReactExtension(project)
Expand All @@ -494,7 +494,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndNotOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenBothAreSetToTrueAndNotOnPrealpha_returnFalse() {
val project = createProject()
project.extensions.extraProperties.set("newArchEnabled", "true")
project.extensions.extraProperties.set("react.newxArchEnabled", "true")
Expand All @@ -514,7 +514,7 @@ class ProjectUtilsTest {
}

@Test
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndNotOnMajor_returnFalse() {
fun shouldWarnIfNewArchFlagIsSetInPrealpha_whenNoneAreSetAndNotOnPrealpha_returnFalse() {
val project = createProject()
val extension = TestReactExtension(project)
File(tempFolder.root, "package.json").apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def test_computeNewArchEnabled_whenOnStableAndFlagFalseAndEnv0_returnFalseWithNo
end

def test_computeNewArchEnabled_whenOn100AndFlagTrueAndEnvNil_returnTrueWithNoWarning
version = '1.0.0-prealpha.0'
version = '0.0.0-prealpha.0'
new_arch_enabled = true
ENV['RCT_NEW_ARCH_ENABLED'] = nil

Expand All @@ -295,7 +295,7 @@ def test_computeNewArchEnabled_whenOn100AndFlagTrueAndEnvNil_returnTrueWithNoWar
end

def test_computeNewArchEnabled_whenOn100AndFlagTrueAndEnv1_returnTrueWithWarning
version = '1.0.0-prealpha.0'
version = '0.0.0-prealpha.0'
new_arch_enabled = true
ENV['RCT_NEW_ARCH_ENABLED'] = "1"

Expand All @@ -308,7 +308,7 @@ def test_computeNewArchEnabled_whenOn100AndFlagTrueAndEnv1_returnTrueWithWarning


def test_computeNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalseAndEnv0_returnTrueWithWarning
version = '1.0.0-prealpha.0'
version = '0.0.0-prealpha.0'
new_arch_enabled = false
ENV['RCT_NEW_ARCH_ENABLED'] = "0"

Expand All @@ -320,7 +320,7 @@ def test_computeNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalseAndEnv0_retu
end

def test_computeNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue
version = '1.0.0-prealpha-0'
version = '0.0.0-prealpha-0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
Expand All @@ -329,7 +329,7 @@ def test_computeNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue
end

def test_computeNewArchEnabled_whenOn100PrealphaOnlyWordsAndFlagFalse_returnTrue
version = '1.0.0-prealpha0'
version = '0.0.0-prealpha0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
Expand All @@ -343,7 +343,8 @@ def test_computeNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue

isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
assert_equal("0", isEnabled)
assert_equal([], Pod::UI.collected_warns);
end

# =================================== #
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/scripts/cocoapods/new_architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ def self.compute_new_arch_enabled(new_arch_enabled, react_native_version)

if match_data = react_native_version.match(version_regex)

major = match_data[1].to_i
prerelease = match_data[4].to_s

# 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
if prerelease.include?("prealpha")
if ENV['RCT_NEW_ARCH_ENABLED'] != nil && !@@NewArchWarningEmitted
warning_message = "[New Architecture] Starting from version 1.0.0-prealpha the value of the " \
"RCT_NEW_ARCH_ENABLED flag is ignored and the New Architecture is enabled by default."
Expand Down

0 comments on commit 5c2ec55

Please sign in to comment.