Skip to content

Commit 7d61b9d

Browse files
cortinicofacebook-github-bot
authored andcommitted
RNGP - Remove enableCodegenInApps and infer it from package.json (#35083)
Summary: Pull Request resolved: #35083 We don't need to `enableCodegenInApps` keys but we can instead rely on the existence of the `codegenConfig` key inside the `package.json` to decide if Codegen should run in App modules or not. Changelog: [Internal] [Changed] - RNGP - Remove enableCodegenInApps and infer it from package.json Reviewed By: cipolleschi Differential Revision: D40687079 fbshipit-source-id: cd4a6c67caa19c1d199ae75388a0551339f876a0
1 parent 0627cd6 commit 7d61b9d

File tree

7 files changed

+157
-12
lines changed

7 files changed

+157
-12
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,4 @@ abstract class ReactExtension @Inject constructor(project: Project) {
156156
*/
157157
val codegenJavaPackageName: Property<String> =
158158
objects.property(String::class.java).convention("com.facebook.fbreact.specs")
159-
160-
/**
161-
* Toggles the Codegen for App modules. By default we enable the codegen only for library modules.
162-
* If you want to enable codegen in your app, you will have to set this to true. Default: false
163-
*/
164-
val enableCodegenInApps: Property<Boolean> =
165-
objects.property(Boolean::class.java).convention(false)
166159
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.facebook.react.utils.DependencyUtils.configureRepositories
1919
import com.facebook.react.utils.DependencyUtils.readVersionString
2020
import com.facebook.react.utils.JsonUtils
2121
import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
22+
import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson
2223
import com.facebook.react.utils.findPackageJsonFile
2324
import java.io.File
2425
import kotlin.system.exitProcess
@@ -91,7 +92,11 @@ class ReactPlugin : Plugin<Project> {
9192
it.codegenDir.set(extension.codegenDir)
9293
val bashWindowsHome = project.findProperty("REACT_WINDOWS_BASH") as String?
9394
it.bashWindowsHome.set(bashWindowsHome)
94-
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
95+
96+
// Please note that appNeedsCodegen is triggering a read of the package.json at
97+
// configuration time as we need to feed the onlyIf condition of this task.
98+
// Therefore, the appNeedsCodegen needs to be invoked inside this lambda.
99+
it.onlyIf { isLibrary || project.needsCodegenFromPackageJson(extension) }
95100
}
96101

97102
// We create the task to produce schema from JS files.
@@ -102,10 +107,9 @@ class ReactPlugin : Plugin<Project> {
102107
it.nodeExecutableAndArgs.set(extension.nodeExecutableAndArgs)
103108
it.codegenDir.set(extension.codegenDir)
104109
it.generatedSrcDir.set(generatedSrcDir)
105-
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
106110

107111
// We're reading the package.json at configuration time to properly feed
108-
// the `jsRootDir` @Input property of this task. Therefore, the
112+
// the `jsRootDir` @Input property of this task & the onlyIf. Therefore, the
109113
// parsePackageJson should be invoked inside this lambda.
110114
val packageJson = findPackageJsonFile(project, extension)
111115
val parsedPackageJson = packageJson?.let { JsonUtils.fromCodegenJson(it) }
@@ -116,6 +120,7 @@ class ReactPlugin : Plugin<Project> {
116120
} else {
117121
it.jsRootDir.set(extension.jsRootDir)
118122
}
123+
it.onlyIf { isLibrary || project.needsCodegenFromPackageJson(parsedPackageJson) }
119124
}
120125

121126
// We create the task to generate Java code from schema.
@@ -130,7 +135,11 @@ class ReactPlugin : Plugin<Project> {
130135
it.packageJsonFile.set(findPackageJsonFile(project, extension))
131136
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
132137
it.libraryName.set(extension.libraryName)
133-
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
138+
139+
// Please note that appNeedsCodegen is triggering a read of the package.json at
140+
// configuration time as we need to feed the onlyIf condition of this task.
141+
// Therefore, the appNeedsCodegen needs to be invoked inside this lambda.
142+
it.onlyIf { isLibrary || project.needsCodegenFromPackageJson(extension) }
134143
}
135144

136145
// We update the android configuration to include the generated sources.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package com.facebook.react.utils
1111

1212
import com.facebook.react.ReactExtension
13+
import com.facebook.react.model.ModelPackageJson
1314
import java.io.File
1415
import org.gradle.api.Project
1516

@@ -206,6 +207,19 @@ internal fun findPackageJsonFile(project: Project, extension: ReactExtension): F
206207
extension.root.file("package.json").orNull?.asFile
207208
}
208209

210+
/**
211+
* Function to look for the `package.json` and parse it. It returns a [ModelPackageJson] if found or
212+
* null others.
213+
*
214+
* Please note that this function access the [ReactExtension] field properties and calls .get() on
215+
* them, so calling this during apply() of the ReactPlugin is not recommended. It should be invoked
216+
* inside lazy lambdas or at execution time.
217+
*/
218+
internal fun readPackageJsonFile(project: Project, extension: ReactExtension): ModelPackageJson? {
219+
val packageJson = findPackageJsonFile(project, extension)
220+
return packageJson?.let { JsonUtils.fromCodegenJson(it) }
221+
}
222+
209223
private const val HERMESC_IN_REACT_NATIVE_DIR = "node_modules/react-native/sdks/hermesc/%OS-BIN%/"
210224
private const val HERMESC_BUILT_FROM_SOURCE_DIR =
211225
"node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package com.facebook.react.utils
99

10+
import com.facebook.react.ReactExtension
11+
import com.facebook.react.model.ModelPackageJson
1012
import org.gradle.api.Project
1113

1214
internal object ProjectUtils {
@@ -32,4 +34,13 @@ internal object ProjectUtils {
3234
} else {
3335
HERMES_FALLBACK
3436
}
37+
38+
internal fun Project.needsCodegenFromPackageJson(extension: ReactExtension): Boolean {
39+
val parsedPackageJson = readPackageJsonFile(this, extension)
40+
return needsCodegenFromPackageJson(parsedPackageJson)
41+
}
42+
43+
internal fun Project.needsCodegenFromPackageJson(model: ModelPackageJson?): Boolean {
44+
return model?.codegenConfig != null
45+
}
3546
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.facebook.react.tests.OS
1313
import com.facebook.react.tests.OsRule
1414
import com.facebook.react.tests.WithOs
1515
import java.io.File
16+
import java.io.FileNotFoundException
1617
import org.gradle.testfixtures.ProjectBuilder
1718
import org.junit.Assert.*
1819
import org.junit.Assume.assumeTrue
@@ -299,4 +300,60 @@ class PathUtilsTest {
299300

300301
assertEquals(localFile, findPackageJsonFile(project, extension))
301302
}
303+
304+
@Test(expected = FileNotFoundException::class)
305+
fun readPackageJsonFile_withMissingFile_returnsNull() {
306+
val moduleFolder = tempFolder.newFolder("awesome-module")
307+
val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build()
308+
project.plugins.apply("com.android.library")
309+
project.plugins.apply("com.facebook.react")
310+
val extension =
311+
project.extensions.getByType(ReactExtension::class.java).apply { root.set(moduleFolder) }
312+
313+
val actual = readPackageJsonFile(project, extension)
314+
315+
assertNull(actual)
316+
}
317+
318+
@Test
319+
fun readPackageJsonFile_withFileConfiguredInExtension_andMissingCodegenConfig_returnsNullCodegenConfig() {
320+
val moduleFolder = tempFolder.newFolder("awesome-module")
321+
File(moduleFolder, "package.json").apply { writeText("{}") }
322+
val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build()
323+
project.plugins.apply("com.android.library")
324+
project.plugins.apply("com.facebook.react")
325+
val extension =
326+
project.extensions.getByType(ReactExtension::class.java).apply { root.set(moduleFolder) }
327+
328+
val actual = readPackageJsonFile(project, extension)
329+
330+
assertNotNull(actual)
331+
assertNull(actual!!.codegenConfig)
332+
}
333+
334+
@Test
335+
fun readPackageJsonFile_withFileConfiguredInExtension_andHavingCodegenConfig_returnsValidCodegenConfig() {
336+
val moduleFolder = tempFolder.newFolder("awesome-module")
337+
File(moduleFolder, "package.json").apply {
338+
writeText(
339+
// language=json
340+
"""
341+
{
342+
"name": "a-library",
343+
"codegenConfig": {}
344+
}
345+
"""
346+
.trimIndent())
347+
}
348+
val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build()
349+
project.plugins.apply("com.android.library")
350+
project.plugins.apply("com.facebook.react")
351+
val extension =
352+
project.extensions.getByType(ReactExtension::class.java).apply { root.set(moduleFolder) }
353+
354+
val actual = readPackageJsonFile(project, extension)
355+
356+
assertNotNull(actual)
357+
assertNotNull(actual!!.codegenConfig)
358+
}
302359
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,24 @@
77

88
package com.facebook.react.utils
99

10+
import com.facebook.react.TestReactExtension
11+
import com.facebook.react.model.ModelCodegenConfig
12+
import com.facebook.react.model.ModelPackageJson
1013
import com.facebook.react.tests.createProject
1114
import com.facebook.react.utils.ProjectUtils.isHermesEnabled
1215
import com.facebook.react.utils.ProjectUtils.isNewArchEnabled
16+
import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson
17+
import java.io.File
1318
import org.junit.Assert.assertFalse
1419
import org.junit.Assert.assertTrue
20+
import org.junit.Rule
1521
import org.junit.Test
22+
import org.junit.rules.TemporaryFolder
1623

1724
class ProjectUtilsTest {
1825

26+
@get:Rule val tempFolder = TemporaryFolder()
27+
1928
@Test
2029
fun isNewArchEnabled_returnsFalseByDefault() {
2130
assertFalse(createProject().isNewArchEnabled)
@@ -99,4 +108,57 @@ class ProjectUtilsTest {
99108
project.extensions.extraProperties.set("react", extMap)
100109
assertTrue(project.isHermesEnabled)
101110
}
111+
112+
@Test
113+
fun needsCodegenFromPackageJson_withCodegenConfigInPackageJson_returnsTrue() {
114+
val project = createProject()
115+
val extension = TestReactExtension(project)
116+
File(tempFolder.root, "package.json").apply {
117+
writeText(
118+
// language=json
119+
"""
120+
{
121+
"name": "a-library",
122+
"codegenConfig": {}
123+
}
124+
"""
125+
.trimIndent())
126+
}
127+
extension.root.set(tempFolder.root)
128+
assertTrue(project.needsCodegenFromPackageJson(extension))
129+
}
130+
131+
@Test
132+
fun needsCodegenFromPackageJson_withMissingCodegenConfigInPackageJson_returnsFalse() {
133+
val project = createProject()
134+
val extension = TestReactExtension(project)
135+
File(tempFolder.root, "package.json").apply {
136+
writeText(
137+
// language=json
138+
"""
139+
{
140+
"name": "a-library"
141+
}
142+
"""
143+
.trimIndent())
144+
}
145+
extension.root.set(tempFolder.root)
146+
assertFalse(project.needsCodegenFromPackageJson(extension))
147+
}
148+
149+
@Test
150+
fun needsCodegenFromPackageJson_withCodegenConfigInModel_returnsTrue() {
151+
val project = createProject()
152+
val model = ModelPackageJson(ModelCodegenConfig(null, null, null, null))
153+
154+
assertTrue(project.needsCodegenFromPackageJson(model))
155+
}
156+
157+
@Test
158+
fun needsCodegenFromPackageJson_withMissingCodegenConfigInModel_returnsFalse() {
159+
val project = createProject()
160+
val model = ModelPackageJson(null)
161+
162+
assertFalse(project.needsCodegenFromPackageJson(model))
163+
}
102164
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ react {
8888
// Codegen Configs
8989
reactNativeDir = rootDir
9090
codegenDir = new File(rootDir, "node_modules/react-native-codegen")
91-
enableCodegenInApps = true
9291
}
9392

9493
/**

0 commit comments

Comments
 (0)