Skip to content

Adding Discord-only TurboModule support #34

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

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -125,7 +125,15 @@ add_library(
\${react_codegen_SRCS}
)

target_include_directories(react_codegen_${libraryName} PUBLIC . react/renderer/components/${libraryName})
# ADDED STUFF FOR DISCORD THINGS (See GenerateModuleJniH.js in the react-native repo)

if(DEFINED PREBUILT_CMAKE)
include(\${PREBUILT_CMAKE})
endif()

target_include_directories(react_codegen_RTNTurboModuleTest PUBLIC . react/renderer/components/${libraryName} \${EXTRA_INCLUDE_DIRECTORIES})

# END ADDED STUFF FOR DISCORD THINGS

target_link_libraries(
react_codegen_${libraryName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.android.build.gradle.AppExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.internal.tasks.factory.dependsOn
import com.facebook.react.model.ModelPackageJson
import com.facebook.react.tasks.BuildCodegenCLITask
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
import com.facebook.react.tasks.GenerateCodegenSchemaTask
Expand Down Expand Up @@ -103,6 +104,8 @@ class ReactPlugin : Plugin<Project> {
} else {
it.jsRootDir.set(extension.jsRootDir)
}

it.onlyIf { project.needsCodegenFromPackageJson(parsedPackageJson) }
}

// We create the task to generate Java code from schema.
Expand All @@ -118,6 +121,14 @@ class ReactPlugin : Plugin<Project> {
it.packageJsonFile.set(findPackageJsonFile(project, extension))
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
it.libraryName.set(extension.libraryName)

// We're reading the package.json at configuration time to properly feed
// the `jsRootDir` @Input property of this task. Therefore, the
// parsePackageJson should be invoked inside this lambda.
val packageJson = findPackageJsonFile(project, extension)
val parsedPackageJson = packageJson?.let { JsonUtils.fromCodegenJson(it) }

it.onlyIf { project.needsCodegenFromPackageJson(parsedPackageJson) }
}

// We add dependencies & generated sources to the project.
Expand All @@ -142,4 +153,28 @@ class ReactPlugin : Plugin<Project> {
android.sourceSets.getByName("main").java.srcDir(File(generatedSrcDir, "java"))
}
}

internal fun Project.needsCodegenFromPackageJson(model: ModelPackageJson?): Boolean {
/**
This flag allows us to codegen TurboModule bindings for only Discord modules. We need this differentiation because
React Native tooling only allows us to run TurboModule codegen if newArchEnabled=true, but the newArchEnabled flag
assumes a complete migration to the New Architecture. Third party libraries make codegen assumptions that
then break the build if we codegen, then build with newArchEnabled=false (things like codegen-ing TurboModule
classes that then share the same name as legacy module classes used when newArchEnabled=false).

The goal of this is to get us in a state where we can consume both TurboModules and legacy modules without having
to fully migrate to the new architecture.
*/
var discordApproved = true
if (project.hasProperty("onlyDiscordTurboModulesEnabled")) {
discordApproved = model?.codegenConfig?.android?.javaPackageName?.startsWith("com.discord") == true
}

// Adding a log to see what packages are getting codegen'd
val willCodegen = discordApproved && model?.codegenConfig != null
if (willCodegen) {
println("Running codegen for package ${model?.codegenConfig?.android?.javaPackageName?.toString()}")
}
return willCodegen
}
}