Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gradle-plugin] Read Maven group from GROUP property #37204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.facebook.react.utils.AgpConfiguratorUtils.configureDevPorts
import com.facebook.react.utils.BackwardCompatUtils.configureBackwardCompatibilityReactMap
import com.facebook.react.utils.DependencyUtils.configureDependencies
import com.facebook.react.utils.DependencyUtils.configureRepositories
import com.facebook.react.utils.DependencyUtils.readGroupString
import com.facebook.react.utils.DependencyUtils.readVersionString
import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
Expand Down Expand Up @@ -55,7 +56,8 @@ class ReactPlugin : Plugin<Project> {
val reactNativeDir = extension.reactNativeDir.get().asFile
val propertiesFile = File(reactNativeDir, "ReactAndroid/gradle.properties")
val versionString = readVersionString(propertiesFile)
configureDependencies(project, versionString)
val groupString = readGroupString(propertiesFile)
configureDependencies(project, versionString, groupString)
configureRepositories(project, reactNativeDir)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal object DependencyUtils {
* - Forcing the react-android/hermes-android version to the one specified in the package.json
* - Substituting `react-native` with `react-android` and `hermes-engine` with `hermes-android`.
*/
fun configureDependencies(project: Project, versionString: String) {
fun configureDependencies(project: Project, versionString: String, groupString: String = defaultGroupString) {
if (versionString.isBlank()) return
project.rootProject.allprojects { eachProject ->
eachProject.configurations.all { configuration ->
Expand All @@ -56,17 +56,17 @@ internal object DependencyUtils {
// implementation("com.facebook.react:react-native:+") and resolve the right dependency.
configuration.resolutionStrategy.dependencySubstitution {
it.substitute(it.module("com.facebook.react:react-native"))
.using(it.module("com.facebook.react:react-android:${versionString}"))
.using(it.module("${groupString}:react-android:${versionString}"))
.because(
"The react-native artifact was deprecated in favor of react-android due to https://github.com/facebook/react-native/issues/35210.")
it.substitute(it.module("com.facebook.react:hermes-engine"))
.using(it.module("com.facebook.react:hermes-android:${versionString}"))
.using(it.module("${groupString}:hermes-android:${versionString}"))
.because(
"The hermes-engine artifact was deprecated in favor of hermes-android due to https://github.com/facebook/react-native/issues/35210.")
}
configuration.resolutionStrategy.force(
"com.facebook.react:react-android:${versionString}",
"com.facebook.react:hermes-android:${versionString}",
"${groupString}:react-android:${versionString}",
"${groupString}:hermes-android:${versionString}",
)
}
}
Expand All @@ -84,9 +84,18 @@ internal object DependencyUtils {
}
}

// Allows react-native-tvos to get its dependencies correctly with its custom Maven group name
fun readGroupString(propertiesFile: File): String {
douglowder marked this conversation as resolved.
Show resolved Hide resolved
val reactAndroidProperties = Properties()
propertiesFile.inputStream().use { reactAndroidProperties.load(it) }
return reactAndroidProperties["GROUP"] as? String ?: defaultGroupString
}

fun Project.mavenRepoFromUrl(url: String): MavenArtifactRepository =
project.repositories.maven { it.url = URI.create(url) }

fun Project.mavenRepoFromURI(uri: URI): MavenArtifactRepository =
project.repositories.maven { it.url = uri }

val defaultGroupString = "com.facebook.react"
douglowder marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.facebook.react.utils.DependencyUtils.configureDependencies
import com.facebook.react.utils.DependencyUtils.configureRepositories
import com.facebook.react.utils.DependencyUtils.mavenRepoFromURI
import com.facebook.react.utils.DependencyUtils.mavenRepoFromUrl
import com.facebook.react.utils.DependencyUtils.readGroupString
import com.facebook.react.utils.DependencyUtils.readVersionString
import java.net.URI
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
Expand Down Expand Up @@ -226,6 +227,24 @@ class DependencyUtilsTest {
assertTrue(libForcedModules.any { it.toString() == "com.facebook.react:hermes-android:1.2.3" })
}

@Test
fun configureDependencies_withVersionStringAndGroupString_appliesOnAllProjects() {
val rootProject = ProjectBuilder.builder().build()
val appProject = ProjectBuilder.builder().withName("app").withParent(rootProject).build()
val libProject = ProjectBuilder.builder().withName("lib").withParent(rootProject).build()
appProject.plugins.apply("com.android.application")
libProject.plugins.apply("com.android.library")

configureDependencies(appProject, "1.2.3", "io.github.test")

val appForcedModules = appProject.configurations.first().resolutionStrategy.forcedModules
val libForcedModules = libProject.configurations.first().resolutionStrategy.forcedModules
assertTrue(appForcedModules.any { it.toString() == "io.github.test:react-android:1.2.3" })
assertTrue(appForcedModules.any { it.toString() == "io.github.test:hermes-android:1.2.3" })
assertTrue(libForcedModules.any { it.toString() == "io.github.test:react-android:1.2.3" })
assertTrue(libForcedModules.any { it.toString() == "io.github.test:hermes-android:1.2.3" })
}

@Test
fun readVersionString_withCorrectVersionString_returnsIt() {
val propertiesFile =
Expand Down Expand Up @@ -291,6 +310,39 @@ class DependencyUtilsTest {
assertEquals("", versionString)
}

@Test
fun readGroupString_withCorrectGroupString_returnsIt() {
val propertiesFile =
tempFolder.newFile("gradle.properties").apply {
writeText(
"""
GROUP=io.github.test
ANOTHER_PROPERTY=true
"""
.trimIndent())
}

val groupString = readGroupString(propertiesFile)

assertEquals("io.github.test", groupString)
}

@Test
fun readGroupString_withEmptyGroupString_returnsDefault() {
val propertiesFile =
tempFolder.newFile("gradle.properties").apply {
writeText(
"""
ANOTHER_PROPERTY=true
"""
.trimIndent())
}

val groupString = readGroupString(propertiesFile)

assertEquals("com.facebook.react", groupString)
}

@Test
fun mavenRepoFromUrl_worksCorrectly() {
val process = createProject()
Expand Down