Skip to content

Commit 7f25183

Browse files
committed
feat: provide plugin as an embedding
1 parent 9159f84 commit 7f25183

18 files changed

Lines changed: 454 additions & 89 deletions

File tree

‎packages/gradle-plugin/build.gradle.kts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ plugins {
1212

1313
tasks.register("build") {
1414
dependsOn(
15-
":react-native-gradle-plugin-shared:build",
1615
":react-native-library-plugin:build",
1716
":react-native-gradle-plugin:build",
1817
":settings-plugin:build",
@@ -23,7 +22,6 @@ tasks.register("build") {
2322

2423
tasks.register("clean") {
2524
dependsOn(
26-
":react-native-gradle-plugin-shared:clean",
2725
":react-native-library-plugin:clean",
2826
":react-native-gradle-plugin:clean",
2927
":settings-plugin:clean",
@@ -34,7 +32,6 @@ tasks.register("clean") {
3432

3533
tasks.named("ktfmtCheck") {
3634
dependsOn(
37-
":react-native-gradle-plugin-shared:ktfmtCheck",
3835
":react-native-library-plugin:ktfmtCheck",
3936
":react-native-gradle-plugin:ktfmtCheck",
4037
":settings-plugin:ktfmtCheck",
@@ -45,7 +42,6 @@ tasks.named("ktfmtCheck") {
4542

4643
tasks.named("ktfmtFormat") {
4744
dependsOn(
48-
":react-native-gradle-plugin-shared:ktfmtFormat",
4945
":react-native-library-plugin:ktfmtFormat",
5046
":react-native-gradle-plugin:ktfmtFormat",
5147
":settings-plugin:ktfmtFormat",

‎packages/gradle-plugin/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"gradlew",
3131
"gradlew.bat",
3232
"README.md",
33-
"react-native-gradle-plugin-shared",
3433
"react-native-library-plugin",
3534
"!react-native-library-plugin/src/test",
3635
"react-native-gradle-plugin",

‎packages/gradle-plugin/react-native-gradle-plugin-shared/build.gradle.kts‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎packages/gradle-plugin/react-native-gradle-plugin/build.gradle.kts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ gradlePlugin {
3737
group = "com.facebook.react"
3838

3939
dependencies {
40-
implementation(project(":react-native-gradle-plugin-shared"))
4140
implementation(project(":shared"))
4241

4342
implementation(gradleApi())

packages/gradle-plugin/react-native-gradle-plugin-shared/src/main/kotlin/com/facebook/react/ReactCodegenConfigurator.kt renamed to packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppCodegenConfigurator.kt

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package com.facebook.react
99

1010
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
11-
import com.android.build.api.variant.LibraryAndroidComponentsExtension
1211
import com.android.build.gradle.internal.tasks.factory.dependsOn
1312
import com.facebook.react.internal.PrivateReactExtension
1413
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
@@ -23,22 +22,17 @@ import org.gradle.api.provider.Provider
2322
import org.gradle.api.tasks.TaskProvider
2423

2524
@Suppress("UnstableApiUsage")
26-
object ReactCodegenConfigurator {
25+
object ReactAppCodegenConfigurator {
2726
fun configureCodegen(
2827
project: Project,
2928
localExtension: ReactExtension,
3029
rootExtension: PrivateReactExtension,
31-
isLibrary: Boolean,
3230
needsCodegenFromPackageJson: (Project, PrivateReactExtension) -> Boolean,
3331
) {
3432
val generatedSrcDir: Provider<Directory> =
3533
project.layout.buildDirectory.dir("generated/source/codegen")
3634

37-
if (isLibrary) {
38-
localExtension.jsRootDir.convention(project.layout.projectDirectory.dir("../"))
39-
} else {
40-
localExtension.jsRootDir.convention(localExtension.root)
41-
}
35+
localExtension.jsRootDir.convention(localExtension.root)
4236

4337
val generateCodegenArtifactsTask =
4438
registerCodegenTasks(
@@ -63,33 +57,22 @@ object ReactCodegenConfigurator {
6357
task.libraryName.set(localExtension.libraryName)
6458
},
6559
onlyIf = { packageJson ->
66-
val needsCodegenFromPackageJson =
60+
val needsCodegen =
6761
needsCodegenFromPackageJson(project, rootExtension)
6862
val parsedPackageJson = packageJson?.let { JsonUtils.fromPackageJson(it) }
6963
val includesGeneratedCode =
7064
parsedPackageJson?.codegenConfig?.includesGeneratedCode ?: false
71-
(isLibrary || needsCodegenFromPackageJson) && !includesGeneratedCode
65+
needsCodegen && !includesGeneratedCode
7266
},
7367
)
7468

75-
if (isLibrary) {
76-
project.extensions.getByType(LibraryAndroidComponentsExtension::class.java).finalizeDsl { ext
77-
->
78-
ext.sourceSets
79-
.getByName("main")
80-
.java
81-
.directories
82-
.add(generatedSrcDir.get().dir("java").asFile.path)
83-
}
84-
} else {
85-
project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).finalizeDsl {
86-
ext ->
87-
ext.sourceSets
88-
.getByName("main")
89-
.java
90-
.directories
91-
.add(generatedSrcDir.get().dir("java").asFile.path)
92-
}
69+
project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).finalizeDsl { ext
70+
->
71+
ext.sourceSets
72+
.getByName("main")
73+
.java
74+
.directories
75+
.add(generatedSrcDir.get().dir("java").asFile.path)
9376
}
9477

9578
project.tasks.named("preBuild", Task::class.java).dependsOn(generateCodegenArtifactsTask)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import com.facebook.react.utils.DependencyUtils.configureRepositories
2424
import com.facebook.react.utils.DependencyUtils.readVersionAndGroupStrings
2525
import com.facebook.react.utils.JdkConfiguratorUtils.configureJavaToolChains
2626
import com.facebook.react.utils.JsonUtils
27-
import com.facebook.react.utils.KotlinPluginUtils.applyKotlinAndroidPluginIfNeeded
27+
import com.facebook.react.internal.deprecated.DeprecatedKotlinPluginUtils.applyKotlinAndroidPluginIfNeeded
28+
import com.facebook.react.internal.deprecated.DeprecatedReactLibraryConfigurator
2829
import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
2930
import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson
3031
import com.facebook.react.utils.PropertyUtils
@@ -99,11 +100,10 @@ class ReactPlugin : Plugin<Project> {
99100
}
100101
}
101102
configureAutolinking(project, extension, rootExtension)
102-
ReactCodegenConfigurator.configureCodegen(
103+
ReactAppCodegenConfigurator.configureCodegen(
103104
project = project,
104105
localExtension = extension,
105106
rootExtension = rootExtension,
106-
isLibrary = false,
107107
needsCodegenFromPackageJson = { currentProject, privateExtension ->
108108
currentProject.needsCodegenFromPackageJson(privateExtension.root)
109109
},
@@ -120,7 +120,7 @@ class ReactPlugin : Plugin<Project> {
120120
"Please use the ${ReactPluginIds.REACT_LIBRARY_PLUGIN} plugin instead."
121121
)
122122
applyKotlinAndroidPluginIfNeeded(project)
123-
ReactLibraryConfigurator.configure(project, extension, rootExtension)
123+
DeprecatedReactLibraryConfigurator.configure(project, extension, rootExtension)
124124
}
125125
}
126126

@@ -260,7 +260,7 @@ class ReactPlugin : Plugin<Project> {
260260
val generatedSrcDir = generatedPureCxxSourceDir.map { it.dir(libraryName) }
261261
val taskNameSuffix = taskNameSuffixForDependency(dependency)
262262

263-
ReactCodegenConfigurator.registerCodegenTasks(
263+
ReactAppCodegenConfigurator.registerCodegenTasks(
264264
project = project,
265265
rootExtension = rootExtension,
266266
generatedSrcDir = generatedSrcDir,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Duplicated from react-native-library-plugin for backward compat; remove when
9+
// `com.facebook.react` library support is dropped.
10+
11+
package com.facebook.react.internal.deprecated
12+
13+
import com.android.build.api.variant.AndroidComponentsExtension
14+
import org.gradle.api.Project
15+
16+
private const val AGP_BUILT_IN_KOTLIN_MAJOR = 9
17+
18+
object DeprecatedKotlinPluginUtils {
19+
fun applyKotlinAndroidPluginIfNeeded(project: Project) {
20+
if (!project.hasBuiltInKotlinSupport()) {
21+
// AGP 9 comes with built-in Kotlin support, so applying `kotlin-android` alongside it causes
22+
// both plugins to register the same `kotlin` extension.
23+
project.applyPluginIfNeeded("kotlin-android")
24+
}
25+
}
26+
27+
/**
28+
* Returns whether the project uses AGP's built-in Kotlin support. It is enabled by default on AGP
29+
* 9 and later, and can be disabled with `android.builtInKotlin=false`.
30+
*/
31+
private fun Project.hasBuiltInKotlinSupport(): Boolean {
32+
val androidComponents = extensions.findByType(AndroidComponentsExtension::class.java)
33+
?: return false
34+
if (androidComponents.pluginVersion.major < AGP_BUILT_IN_KOTLIN_MAJOR) {
35+
return false
36+
}
37+
return findProperty("android.builtInKotlin")?.toString()?.toBoolean() ?: true
38+
}
39+
40+
private fun Project.applyPluginIfNeeded(pluginId: String) {
41+
if (!pluginManager.hasPlugin(pluginId)) {
42+
pluginManager.apply(pluginId)
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Duplicated from react-native-library-plugin for backward compat; remove when
9+
// `com.facebook.react` library support is dropped.
10+
11+
package com.facebook.react.internal.deprecated
12+
13+
import com.android.build.api.variant.LibraryAndroidComponentsExtension
14+
import java.io.File
15+
import javax.xml.parsers.DocumentBuilder
16+
import javax.xml.parsers.DocumentBuilderFactory
17+
import org.gradle.api.Project
18+
import org.w3c.dom.Element
19+
20+
@Suppress("UnstableApiUsage")
21+
object DeprecatedLibraryAgpConfiguratorUtils {
22+
fun configureBuildConfigFieldsForLibraries(project: Project) {
23+
project.extensions.getByType(LibraryAndroidComponentsExtension::class.java).finalizeDsl { ext
24+
->
25+
ext.buildFeatures.buildConfig = true
26+
}
27+
}
28+
29+
fun configureNamespaceForLibraries(project: Project) {
30+
project.extensions.getByType(LibraryAndroidComponentsExtension::class.java).finalizeDsl { ext
31+
->
32+
if (ext.namespace == null) {
33+
val manifestFile =
34+
project.layout.projectDirectory.file("src/main/AndroidManifest.xml").asFile
35+
manifestFile
36+
.takeIf { it.exists() }
37+
?.let { file ->
38+
getPackageNameFromManifest(file)?.let { packageName ->
39+
ext.namespace = packageName
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
internal fun getPackageNameFromManifest(manifest: File): String? {
48+
val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
49+
val builder: DocumentBuilder = factory.newDocumentBuilder()
50+
51+
try {
52+
val xmlDocument = builder.parse(manifest)
53+
54+
val manifestElement = xmlDocument.getElementsByTagName("manifest").item(0) as? Element
55+
val packageName = manifestElement?.getAttribute("package")
56+
57+
return if (packageName.isNullOrEmpty()) null else packageName
58+
} catch (e: Exception) {
59+
return null
60+
}
61+
}

0 commit comments

Comments
 (0)