Skip to content

Feat/support rn entry point #115

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 0 additions & 3 deletions gradle-plugins/react/brownfield/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.util.Properties

plugins {
alias(libs.plugins.kotlinJvm)
`java-gradle-plugin`
Expand Down Expand Up @@ -124,4 +122,3 @@ tasks.javadoc {
source = "8"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.callstack.react.brownfield.exceptions

class NameSpaceNotFound(message: String) :
RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.callstack.react.brownfield.plugin

import com.android.build.gradle.LibraryExtension
import com.callstack.react.brownfield.exceptions.NameSpaceNotFound
import com.callstack.react.brownfield.utils.Extension
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.Directory
import org.gradle.api.tasks.Copy
import java.io.File

object RNSourceSets {
private lateinit var project: Project
Expand Down Expand Up @@ -55,6 +58,46 @@ object RNSourceSets {
}
}

private fun getLibraryNameSpace(): String {
val nameSpace = androidExtension.namespace
return nameSpace ?: throw NameSpaceNotFound("namespace must be defined in your android library build.gradle")
}

private fun patchRNEntryPoint(
task: Task,
path: String,
) {
val rnEntryPointTaskName = "generateReactNativeEntryPoint"
val rnEntryPointTask = appProject.tasks.named(rnEntryPointTaskName)

/**
* If `generateReactNativeEntryPoint` task does not exist, we early return. It means
* the consumer library is running on RN version < 0.80
*/
if (!rnEntryPointTask.isPresent) {
return
}

task.dependsOn(rnEntryPointTask)
val sourceFile = File(moduleBuildDir.toString(), "$path/com/facebook/react/ReactNativeApplicationEntryPoint.java")
task.doLast {
if (sourceFile.exists()) {
var content = sourceFile.readText()
val nameSpace = getLibraryNameSpace()

/**
* We use look-ahead regex to replace any occurrences with Build.Config referenced via the old(app) package
*
* \b[\w.]+ → matches the old package
* (?=\.BuildConfig) → only if it’s immediately followed by that suffix
*/
val regex = Regex("""\b[\w.]+(?=\.BuildConfig)""")
content = content.replace(regex, nameSpace)
sourceFile.writeText(content)
}
}
}

private fun configureTasks() {
val projectName = project.name
val appProjectName = appProject.name
Expand All @@ -64,6 +107,8 @@ object RNSourceSets {
it.dependsOn(":$appProjectName:generateAutolinkingPackageList")
it.from("$appBuildDir/$path")
it.into("$moduleBuildDir/$path")

patchRNEntryPoint(it, path)
}

androidExtension.buildTypes.forEach { buildType ->
Expand Down
Loading