From 89eff977c77e2e6e4af22c6fac1538586a97e51c 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 | 6 +++ .../react/utils/DependencyUtilsTest.kt | 42 +++++++++++++++++++ 3 files changed, 75 insertions(+) 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 393863b3211..7cc58e62496 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,10 +8,12 @@ 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.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 java.io.File import java.net.URI import java.util.* @@ -25,6 +27,12 @@ internal object DependencyUtils { * party libraries which are auto-linked. */ fun configureRepositories(project: Project, reactNativeDir: File) { + 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)) { @@ -33,6 +41,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") } @@ -169,4 +187,13 @@ internal object DependencyUtils { it.url = uri action(it) } + + 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 61cd0f5107d..d6171ede744 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 @@ -22,6 +22,12 @@ object PropertyUtils { const val REACT_NATIVE_ARCHITECTURES = "reactNativeArchitectures" const val SCOPED_REACT_NATIVE_ARCHITECTURES = "react.nativeArchitectures" + /** + * 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" + /** * Internal Property that acts as a killswitch to configure the JDK version and align it for app * and all the libraries. 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 f1947bc4d4f..006d33a667e 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 @@ -107,7 +108,24 @@ class DependencyUtilsTest { val project = createProject() configureRepositories(project, tempFolder.root) + assertThat( + project.repositories.firstOrNull { + it is MavenArtifactRepository && it.url == repositoryURI + }) + .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, tempFolder.root) + + assertThat(project.repositories).hasSize(1) assertThat( project.repositories.firstOrNull { it is MavenArtifactRepository && it.url == repositoryURI @@ -421,4 +439,28 @@ class DependencyUtilsTest { assertThat(mavenRepo.url).isEqualTo(repoFolder.toURI()) } + + @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() + } }