From 58a4c2fe360d97a5de39a70be4b98175d2e51137 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 3 Jul 2025 02:27:08 -0700 Subject: [PATCH] RNGP - Add support for `exclusiveEnterpriseRepository` (#52378) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52378 This adds a Gradle property called `exclusiveEnterpriseRepository` that users can set in their `android/gradle.properties` as such: ```diff hermesEnabled=true +exclusiveEnterpriseRepository=https://my.internal.proxy.net/ ``` This will remove all the existing Maven repositories and only use the internal mirror they have. Changelog: [Android] [Added] - RNGP - Add support for `exclusiveEnterpriseRepository` to specify an internal Maven mirror. Reviewed By: mdvacca Differential Revision: D77667573 fbshipit-source-id: 835004d2ae7aa4e250b6f7a88a41918b573f5bd5 --- .../facebook/react/utils/DependencyUtils.kt | 27 +++++++++++ .../com/facebook/react/utils/PropertyUtils.kt | 8 +++- .../react/utils/DependencyUtilsTest.kt | 45 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt index beee98a2d04..bbaef974695 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt @@ -8,12 +8,14 @@ package com.facebook.react.utils import com.facebook.react.utils.PropertyUtils.DEFAULT_INTERNAL_PUBLISHING_GROUP +import com.facebook.react.utils.PropertyUtils.EXCLUSIVE_ENTEPRISE_REPOSITORY import com.facebook.react.utils.PropertyUtils.INCLUDE_JITPACK_REPOSITORY import com.facebook.react.utils.PropertyUtils.INCLUDE_JITPACK_REPOSITORY_DEFAULT import com.facebook.react.utils.PropertyUtils.INTERNAL_PUBLISHING_GROUP import com.facebook.react.utils.PropertyUtils.INTERNAL_REACT_NATIVE_MAVEN_LOCAL_REPO import com.facebook.react.utils.PropertyUtils.INTERNAL_USE_HERMES_NIGHTLY import com.facebook.react.utils.PropertyUtils.INTERNAL_VERSION_NAME +import com.facebook.react.utils.PropertyUtils.SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY import com.facebook.react.utils.PropertyUtils.SCOPED_INCLUDE_JITPACK_REPOSITORY import java.io.File import java.net.URI @@ -28,6 +30,12 @@ internal object DependencyUtils { * party libraries which are auto-linked. */ fun configureRepositories(project: Project) { + val exclusiveEnterpriseRepository = project.rootProject.exclusiveEnterpriseRepository() + if (exclusiveEnterpriseRepository != null) { + project.logger.lifecycle( + "Replacing ALL Maven Repositories with: $exclusiveEnterpriseRepository") + } + project.rootProject.allprojects { eachProject -> with(eachProject) { if (hasProperty(INTERNAL_REACT_NATIVE_MAVEN_LOCAL_REPO)) { @@ -36,6 +44,16 @@ internal object DependencyUtils { repo.content { it.excludeGroup("org.webkit") } } } + + if (exclusiveEnterpriseRepository != null) { + // We remove all previously set repositories and only configure the proxy provided by the + // user. + rootProject.repositories.clear() + mavenRepoFromUrl(exclusiveEnterpriseRepository) + // We return here as we don't want to configure other repositories as well. + return@allprojects + } + // We add the snapshot for users on nightlies. mavenRepoFromUrl("https://central.sonatype.com/repository/maven-snapshots/") { repo -> repo.content { it.excludeGroup("org.webkit") } @@ -181,4 +199,13 @@ internal object DependencyUtils { property(INCLUDE_JITPACK_REPOSITORY).toString().toBoolean() else -> INCLUDE_JITPACK_REPOSITORY_DEFAULT } + + internal fun Project.exclusiveEnterpriseRepository() = + when { + hasProperty(SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY) -> + property(SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY).toString() + hasProperty(EXCLUSIVE_ENTEPRISE_REPOSITORY) -> + property(EXCLUSIVE_ENTEPRISE_REPOSITORY).toString() + else -> null + } } diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PropertyUtils.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PropertyUtils.kt index 8a0308bd843..527f8ef5346 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PropertyUtils.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PropertyUtils.kt @@ -26,7 +26,13 @@ object PropertyUtils { const val INCLUDE_JITPACK_REPOSITORY = "includeJitpackRepository" const val SCOPED_INCLUDE_JITPACK_REPOSITORY = "react.includeJitpackRepository" - /** By default we include JitPack till React Native 0.80 where this is going to become false */ + /** + * Public property that allows to configure an enterprise repository proxy as exclusive repository + */ + const val EXCLUSIVE_ENTEPRISE_REPOSITORY = "exclusiveEnterpriseRepository" + const val SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY = "react.exclusiveEnterpriseRepository" + + /** By default we include JitPack to avoid breaking user builds */ internal const val INCLUDE_JITPACK_REPOSITORY_DEFAULT = true /** diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/DependencyUtilsTest.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/DependencyUtilsTest.kt index 16973ae47a2..cbead288fab 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/DependencyUtilsTest.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/DependencyUtilsTest.kt @@ -10,6 +10,7 @@ package com.facebook.react.utils import com.facebook.react.tests.createProject import com.facebook.react.utils.DependencyUtils.configureDependencies import com.facebook.react.utils.DependencyUtils.configureRepositories +import com.facebook.react.utils.DependencyUtils.exclusiveEnterpriseRepository import com.facebook.react.utils.DependencyUtils.getDependencySubstitutions import com.facebook.react.utils.DependencyUtils.mavenRepoFromURI import com.facebook.react.utils.DependencyUtils.mavenRepoFromUrl @@ -99,6 +100,24 @@ class DependencyUtilsTest { .isNotNull() } + @Test + fun configureRepositories_withExclusiveEnterpriseRepository_replacesAllRepositories() { + val repositoryURI = URI.create("https://maven.myfabolousorganization.it") + + val project = createProject() + project.rootProject.extensions.extraProperties.set( + "exclusiveEnterpriseRepository", repositoryURI.toString()) + + configureRepositories(project) + + assertThat(project.repositories).hasSize(1) + assertThat( + project.repositories.firstOrNull { + it is MavenArtifactRepository && it.url == repositoryURI + }) + .isNotNull() + } + @Test fun configureRepositories_withIncludeJitpackRepositoryFalse_doesNotContainJitPack() { val repositoryURI = URI.create("https://www.jitpack.io") @@ -470,7 +489,7 @@ class DependencyUtilsTest { @Test fun shouldAddJitPack_withUnscopedProperty() { val project = createProject(tempFolder.root) - project.extensions.extraProperties.set("react.includeJitpackRepository", "false") + project.extensions.extraProperties.set("includeJitpackRepository", "false") assertThat(project.shouldAddJitPack()).isFalse() } @@ -479,4 +498,28 @@ class DependencyUtilsTest { val project = createProject(tempFolder.root) assertThat(project.shouldAddJitPack()).isTrue() } + + @Test + fun exclusiveEnterpriseRepository_withScopedProperty() { + val project = createProject(tempFolder.root) + project.extensions.extraProperties.set( + "react.exclusiveEnterpriseRepository", "https://maven.myfabolousorganization.it") + assertThat(project.exclusiveEnterpriseRepository()) + .isEqualTo("https://maven.myfabolousorganization.it") + } + + @Test + fun exclusiveEnterpriseRepository_withUnscopedProperty() { + val project = createProject(tempFolder.root) + project.extensions.extraProperties.set( + "exclusiveEnterpriseRepository", "https://maven.myfabolousorganization.it") + assertThat(project.exclusiveEnterpriseRepository()) + .isEqualTo("https://maven.myfabolousorganization.it") + } + + @Test + fun exclusiveEnterpriseRepository_defaultIsTrue() { + val project = createProject(tempFolder.root) + assertThat(project.exclusiveEnterpriseRepository()).isNull() + } }