Skip to content

Commit 8bc324f

Browse files
cortinicofacebook-github-bot
authored andcommitted
Gradle: Deprecate reactRoot in favor of root and reactNativeDir (#33142)
Summary: Pull Request resolved: #33142 The `reactRoot` property was confusing as we were using it for both the root of the project and the react-native NPM Package root. I'm deprecating it and splitting it in two. I've added several warning in the codebase to tell the people how to migrate away from it. Moreover this is specifying default values that are more user-friendly. Users won't have to configure anything unless they are in a monorepo. Changelog: [Android] [Changed] - Gradle: Deprecate `reactRoot` in favor of `root` and `reactNativeDir` Reviewed By: ShikaSD Differential Revision: D34277050 fbshipit-source-id: fc7f45017452b086726516a9586cacd9a661c287
1 parent 16feabf commit 8bc324f

File tree

9 files changed

+104
-46
lines changed

9 files changed

+104
-46
lines changed

ReactAndroid/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ react {
407407
// This should be changed to a more generic name, e.g. `ReactCoreSpec`.
408408
libraryName = "rncore"
409409
jsRootDir = file("../Libraries")
410-
reactRoot = file("$projectDir/..")
410+
reactNativeDir = file("$projectDir/..")
411411
useJavaGenerator = System.getenv("USE_CODEGEN_JAVAPOET")?.toBoolean() ?: false
412412
// We search for the codegen in either one of the `node_modules` folder or in the
413413
// root packages folder (that's for when we build from source without calling `yarn install`).

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt

+45-14
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ abstract class ReactExtension @Inject constructor(project: Project) {
2929
val applyAppPlugin: Property<Boolean> = objects.property(Boolean::class.java).convention(false)
3030

3131
/**
32-
* The path to the react root folder. This is the path to the root folder where the `node_modules`
33-
* folder is present. All the CLI commands will be invoked from this folder as working directory.
32+
* The path to the root of your project. This is the path to where the `package.json` lives. All
33+
* the CLI commands will be invoked from this folder as working directory.
3434
*
35-
* Default: $projectDir/../../
35+
* Default: ${rootProject.dir}/../
3636
*/
37-
val reactRoot: DirectoryProperty =
38-
objects.directoryProperty().convention(project.layout.projectDirectory.dir("../../"))
37+
val root: DirectoryProperty =
38+
objects.directoryProperty().convention(project.rootProject.layout.projectDirectory.dir("../"))
3939

4040
/**
4141
* The path to the JS entry file. If not specified, the plugin will try to resolve it using a list
@@ -45,7 +45,7 @@ abstract class ReactExtension @Inject constructor(project: Project) {
4545

4646
/**
4747
* The path to the React Native CLI. If not specified, the plugin will try to resolve it looking
48-
* for `react-native` CLI inside `node_modules` in [reactRoot].
48+
* for `react-native` CLI inside `node_modules` in [root].
4949
*/
5050
val cliPath: Property<String> = objects.property(String::class.java)
5151

@@ -56,9 +56,7 @@ abstract class ReactExtension @Inject constructor(project: Project) {
5656
val nodeExecutableAndArgs: ListProperty<String> =
5757
objects.listProperty(String::class.java).convention(listOf("node"))
5858

59-
/**
60-
* The command to use to invoke bundle. Default is `bundle` and will be invoked on [reactRoot].
61-
*/
59+
/** The command to use to invoke bundle. Default is `bundle` and will be invoked on [root]. */
6260
val bundleCommand: Property<String> = objects.property(String::class.java).convention("bundle")
6361

6462
/**
@@ -190,19 +188,27 @@ abstract class ReactExtension @Inject constructor(project: Project) {
190188
/** Codegen Config */
191189

192190
/**
193-
* The path to the react-native-codegen folder.
191+
* The path to the react-native-codegen NPM package folder.
194192
*
195-
* Default: $projectDir/../../node_modules/react-native-codegen
193+
* Default: ${rootProject.dir}/../node_modules/react-native-codegen
196194
*/
197195
val codegenDir: DirectoryProperty =
198-
objects.directoryProperty().convention(reactRoot.dir("node_modules/react-native-codegen"))
196+
objects.directoryProperty().convention(root.dir("node_modules/react-native-codegen"))
197+
198+
/**
199+
* The path to the react-native NPM package folder.
200+
*
201+
* Default: ${rootProject.dir}/../node_modules/react-native-codegen
202+
*/
203+
val reactNativeDir: DirectoryProperty =
204+
objects.directoryProperty().convention(root.dir("node_modules/react-native"))
199205

200206
/**
201207
* The root directory for all JS files for the app.
202208
*
203-
* Default: $projectDir/../../
209+
* Default: [root] (i.e. ${rootProject.dir}/../)
204210
*/
205-
val jsRootDir: DirectoryProperty = objects.directoryProperty().convention(reactRoot.get())
211+
val jsRootDir: DirectoryProperty = objects.directoryProperty().convention(root.get())
206212

207213
/**
208214
* The library name that will be used for the codegen artifacts.
@@ -222,4 +228,29 @@ abstract class ReactExtension @Inject constructor(project: Project) {
222228

223229
/** Whether the Java Generator (based on Javapoet) should be used or not. Default: false */
224230
val useJavaGenerator: Property<Boolean> = objects.property(Boolean::class.java).convention(false)
231+
232+
/**
233+
* The `reactRoot` property was confusing and should not be used.
234+
*
235+
* You should instead use either:
236+
* - [root] to point to your root project (where the package.json lives)
237+
* - [reactNativeDir] to point to the NPM package of react native.
238+
*
239+
* A valid configuration would look like:
240+
*
241+
* ```
242+
* react {
243+
* root = rootProject.file("..")
244+
* reactNativeDir = rootProject.file("../node_modules/react-native")
245+
* }
246+
* ```
247+
*
248+
* Please also note that those are the default value and you most likely don't need those at all.
249+
*/
250+
@Deprecated(
251+
"reactRoot was confusing and has been replace with root" +
252+
"to point to your root project and reactNativeDir to point to " +
253+
"the folder of the react-native NPM package",
254+
replaceWith = ReplaceWith("reactNativeRoot"))
255+
val reactRoot: DirectoryProperty = objects.directoryProperty()
225256
}

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt

+15-13
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,26 @@ import org.gradle.internal.jvm.Jvm
2323

2424
class ReactPlugin : Plugin<Project> {
2525
override fun apply(project: Project) {
26-
checkJvmVersion()
26+
checkJvmVersion(project)
2727
val extension = project.extensions.create("react", ReactExtension::class.java, project)
2828
applyAppPlugin(project, extension)
2929
applyCodegenPlugin(project, extension)
3030
}
3131

32-
private fun checkJvmVersion() {
32+
private fun checkJvmVersion(project: Project) {
3333
val jvmVersion = Jvm.current()?.javaVersion?.majorVersion
3434
if ((jvmVersion?.toIntOrNull() ?: 0) <= 8) {
35-
println("\n\n\n")
36-
println(
37-
"**************************************************************************************************************")
38-
println("\n\n")
39-
println("ERROR: requires JDK11 or higher.")
40-
println("Incompatible major version detected: '" + jvmVersion + "'")
41-
println("\n\n")
42-
println(
43-
"**************************************************************************************************************")
44-
println("\n\n\n")
35+
project.logger.error(
36+
"""
37+
38+
********************************************************************************
39+
40+
ERROR: requires JDK11 or higher.
41+
Incompatible major version detected: '$jvmVersion'
42+
43+
********************************************************************************
44+
45+
""".trimIndent())
4546
exitProcess(1)
4647
}
4748
}
@@ -95,7 +96,8 @@ class ReactPlugin : Plugin<Project> {
9596
project.tasks.register(
9697
"generateCodegenArtifactsFromSchema", GenerateCodegenArtifactsTask::class.java) {
9798
it.dependsOn(generateCodegenSchemaTask)
98-
it.reactRoot.set(extension.reactRoot)
99+
it.reactNativeDir.set(extension.reactNativeDir)
100+
it.deprecatedReactRoot.set(extension.reactRoot)
99101
it.nodeExecutableAndArgs.set(extension.nodeExecutableAndArgs)
100102
it.codegenDir.set(extension.codegenDir)
101103
it.useJavaGenerator.set(extension.useJavaGenerator)

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import com.facebook.react.utils.detectedCliPath
1717
import com.facebook.react.utils.detectedEntryFile
1818
import com.facebook.react.utils.detectedHermesCommand
1919
import java.io.File
20-
import java.util.*
2120
import org.gradle.api.Project
2221
import org.gradle.api.Task
2322
import org.gradle.api.tasks.Copy
@@ -56,11 +55,9 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
5655
it.group = REACT_GROUP
5756
it.description = "create JS bundle and assets for $targetName."
5857

59-
it.reactRoot = config.reactRoot.get().asFile
58+
it.reactRoot = config.root.get().asFile
6059
it.sources =
61-
fileTree(config.reactRoot) { fileTree ->
62-
fileTree.setExcludes(config.inputExcludes.get())
63-
}
60+
fileTree(config.root) { fileTree -> fileTree.setExcludes(config.inputExcludes.get()) }
6461
it.execCommand = execCommand
6562
it.bundleCommand = config.bundleCommand.get()
6663
it.devEnabled = !config.disableDevForVariant(variant)
@@ -98,7 +95,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte
9895
it.group = REACT_GROUP
9996
it.description = "bundle hermes resources for $targetName"
10097

101-
it.reactRoot = config.reactRoot.get().asFile
98+
it.reactRoot = config.root.get().asFile
10299
it.hermesCommand = detectedHermesCommand(config)
103100
it.hermesFlags = config.hermesFlagsForVariant(variant)
104101
it.jsBundleFile = jsBundleFile

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateCodegenArtifactsTask.kt

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.gradle.api.tasks.*
2020

2121
abstract class GenerateCodegenArtifactsTask : Exec() {
2222

23-
@get:Internal abstract val reactRoot: DirectoryProperty
23+
@get:Internal abstract val reactNativeDir: DirectoryProperty
2424

2525
@get:Internal abstract val codegenDir: DirectoryProperty
2626

@@ -34,6 +34,9 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
3434

3535
@get:Input abstract val libraryName: Property<String>
3636

37+
// We're keeping this just to fire a warning at the user should they use the `reactRoot` property.
38+
@get:Internal abstract val deprecatedReactRoot: DirectoryProperty
39+
3740
@get:InputFile
3841
val combineJsToSchemaCli: Provider<RegularFile> =
3942
codegenDir.file("lib/cli/combine/combine-js-to-schema-cli.js")
@@ -46,6 +49,7 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
4649
@get:OutputDirectory val generatedJniFiles: Provider<Directory> = generatedSrcDir.dir("jni")
4750

4851
override fun exec() {
52+
checkForDeprecatedProperty()
4953
setupCommandLine()
5054
super.exec()
5155
if (useJavaGenerator.getOrElse(false)) {
@@ -63,11 +67,35 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
6367
}
6468
}
6569

70+
private fun checkForDeprecatedProperty() {
71+
if (deprecatedReactRoot.isPresent) {
72+
project.logger.error(
73+
"""
74+
********************************************************************************
75+
The `reactRoot` property is deprecated and will be removed in
76+
future versions of React Native. The property is currently ignored.
77+
78+
You should instead use either:
79+
- [root] to point to your root project (where the package.json lives)
80+
- [reactNativeDir] to point to the NPM package of react native.
81+
82+
You should be fine by just removing the `reactRoot` line entirely from
83+
your build.gradle file. Otherwise a valid configuration would look like:
84+
85+
react {
86+
root = rootProject.file('..')
87+
reactNativeDir = rootProject.file('../node_modules/react-native')
88+
}
89+
********************************************************************************
90+
""".trimIndent())
91+
}
92+
}
93+
6694
internal fun setupCommandLine() {
6795
commandLine(
6896
windowsAwareYarn(
6997
*nodeExecutableAndArgs.get().toTypedArray(),
70-
reactRoot.file("scripts/generate-specs-cli.js").get().asFile.absolutePath,
98+
reactNativeDir.file("scripts/generate-specs-cli.js").get().asFile.absolutePath,
7199
"--platform",
72100
"android",
73101
"--schemaPath",

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package com.facebook.react.utils
1111

1212
import com.facebook.react.ReactExtension
1313
import java.io.File
14-
import java.util.*
1514
import org.apache.tools.ant.taskdefs.condition.Os
1615

1716
/**
@@ -25,7 +24,7 @@ import org.apache.tools.ant.taskdefs.condition.Os
2524
*/
2625
internal fun detectedEntryFile(config: ReactExtension): File =
2726
detectEntryFile(
28-
entryFile = config.entryFile.orNull?.asFile, reactRoot = config.reactRoot.get().asFile)
27+
entryFile = config.entryFile.orNull?.asFile, reactRoot = config.root.get().asFile)
2928

3029
/**
3130
* Computes the CLI location for React Native. The Algo follows this order:
@@ -40,7 +39,7 @@ internal fun detectedCliPath(
4039
): String =
4140
detectCliPath(
4241
projectDir = projectDir,
43-
reactRoot = config.reactRoot.get().asFile,
42+
reactRoot = config.root.get().asFile,
4443
preconfiguredCliPath = config.cliPath.orNull)
4544

4645
/**

packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateCodegenArtifactsTaskTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ class GenerateCodegenArtifactsTaskTest {
7979
@Test
8080
@WithOs(OS.UNIX)
8181
fun setupCommandLine_withoutJavaGenerator_willSetupCorrectly() {
82-
val reactRoot = tempFolder.newFolder("node_modules/react-native/")
82+
val reactNativeDir = tempFolder.newFolder("node_modules/react-native/")
8383
val codegenDir = tempFolder.newFolder("codegen")
8484
val outputDir = tempFolder.newFolder("output")
8585

8686
val task =
8787
createTestTask<GenerateCodegenArtifactsTask> {
88-
it.reactRoot.set(reactRoot)
88+
it.reactNativeDir.set(reactNativeDir)
8989
it.codegenDir.set(codegenDir)
9090
it.generatedSrcDir.set(outputDir)
9191
it.nodeExecutableAndArgs.set(listOf("--verbose"))
@@ -99,7 +99,7 @@ class GenerateCodegenArtifactsTaskTest {
9999
listOf(
100100
"yarn",
101101
"--verbose",
102-
File(reactRoot, "scripts/generate-specs-cli.js").toString(),
102+
File(reactNativeDir, "scripts/generate-specs-cli.js").toString(),
103103
"--platform",
104104
"android",
105105
"--schemaPath",

packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PathUtilsTest {
3333
@Test
3434
fun detectedEntryFile_withAndroidEntryPoint() {
3535
val extension = TestReactExtension(ProjectBuilder.builder().build())
36-
extension.reactRoot.set(tempFolder.root)
36+
extension.root.set(tempFolder.root)
3737
tempFolder.newFile("index.android.js")
3838

3939
val actual = detectedEntryFile(extension)
@@ -44,7 +44,7 @@ class PathUtilsTest {
4444
@Test
4545
fun detectedEntryFile_withDefaultEntryPoint() {
4646
val extension = TestReactExtension(ProjectBuilder.builder().build())
47-
extension.reactRoot.set(tempFolder.root)
47+
extension.root.set(tempFolder.root)
4848

4949
val actual = detectedEntryFile(extension)
5050

@@ -80,7 +80,7 @@ class PathUtilsTest {
8080
fun detectedCliPath_withCliFromNodeModules() {
8181
val project = ProjectBuilder.builder().build()
8282
val extension = TestReactExtension(project)
83-
extension.reactRoot.set(tempFolder.root)
83+
extension.root.set(tempFolder.root)
8484
val expected =
8585
File(tempFolder.root, "node_modules/react-native/cli.js").apply {
8686
parentFile.mkdirs()

packages/rn-tester/android/app/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ react {
8080
cliPath = "../../../../cli.js"
8181
bundleAssetName = "RNTesterApp.android.bundle"
8282
entryFile = file("../../js/RNTesterApp.android.js")
83-
reactRoot = rootDir
83+
root = rootDir
8484
inputExcludes = ["android/**", "./**", ".gradle/**"]
8585
composeSourceMapsPath = "$rootDir/scripts/compose-source-maps.js"
8686
hermesCommand = "$rootDir/node_modules/hermes-engine/%OS-BIN%/hermesc"
8787
enableHermesForVariant { def v -> v.name.contains("hermes") }
8888

8989
// Codegen Configs
9090
jsRootDir = file("$rootDir/packages/rn-tester")
91+
reactNativeDir = rootDir
9192
libraryName = "rntester"
9293
useJavaGenerator = System.getenv("USE_CODEGEN_JAVAPOET")?.toBoolean() ?: false
9394
}

0 commit comments

Comments
 (0)