@@ -11,6 +11,7 @@ import com.android.build.gradle.AppExtension
11
11
import com.android.build.gradle.BaseExtension
12
12
import com.android.build.gradle.LibraryExtension
13
13
import com.android.build.gradle.internal.tasks.factory.dependsOn
14
+ import com.facebook.react.model.ModelPackageJson
14
15
import com.facebook.react.tasks.BuildCodegenCLITask
15
16
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
16
17
import com.facebook.react.tasks.GenerateCodegenSchemaTask
@@ -103,6 +104,8 @@ class ReactPlugin : Plugin<Project> {
103
104
} else {
104
105
it.jsRootDir.set(extension.jsRootDir)
105
106
}
107
+
108
+ it.onlyIf { project.needsCodegenFromPackageJson(parsedPackageJson) }
106
109
}
107
110
108
111
// We create the task to generate Java code from schema.
@@ -118,6 +121,14 @@ class ReactPlugin : Plugin<Project> {
118
121
it.packageJsonFile.set(findPackageJsonFile(project, extension))
119
122
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
120
123
it.libraryName.set(extension.libraryName)
124
+
125
+ // We're reading the package.json at configuration time to properly feed
126
+ // the `jsRootDir` @Input property of this task. Therefore, the
127
+ // parsePackageJson should be invoked inside this lambda.
128
+ val packageJson = findPackageJsonFile(project, extension)
129
+ val parsedPackageJson = packageJson?.let { JsonUtils .fromCodegenJson(it) }
130
+
131
+ it.onlyIf { project.needsCodegenFromPackageJson(parsedPackageJson) }
121
132
}
122
133
123
134
// We add dependencies & generated sources to the project.
@@ -142,4 +153,28 @@ class ReactPlugin : Plugin<Project> {
142
153
android.sourceSets.getByName(" main" ).java.srcDir(File (generatedSrcDir, " java" ))
143
154
}
144
155
}
156
+
157
+ internal fun Project.needsCodegenFromPackageJson (model : ModelPackageJson ? ): Boolean {
158
+ /* *
159
+ This flag allows us to codegen TurboModule bindings for only Discord modules. We need this differentiation because
160
+ React Native tooling only allows us to run TurboModule codegen if newArchEnabled=true, but the newArchEnabled flag
161
+ assumes a complete migration to the New Architecture. Third party libraries make codegen assumptions that
162
+ then break the build if we codegen, then build with newArchEnabled=false (things like codegen-ing TurboModule
163
+ classes that then share the same name as legacy module classes used when newArchEnabled=false).
164
+
165
+ The goal of this is to get us in a state where we can consume both TurboModules and legacy modules without having
166
+ to fully migrate to the new architecture.
167
+ */
168
+ var discordApproved = true
169
+ if (project.hasProperty(" onlyDiscordTurboModulesEnabled" )) {
170
+ discordApproved = model?.codegenConfig?.android?.javaPackageName?.startsWith(" com.discord" ) == true
171
+ }
172
+
173
+ // Adding a log to see what packages are getting codegen'd
174
+ val willCodegen = discordApproved && model?.codegenConfig != null
175
+ if (willCodegen) {
176
+ println (" Running codegen for package ${model?.codegenConfig?.android?.javaPackageName?.toString()} " )
177
+ }
178
+ return willCodegen
179
+ }
145
180
}
0 commit comments