@ohos-ports/react-native-gradle-plugin 0.71.19-beta.0 → 0.86.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +11 -4
  2. package/build.gradle.kts +33 -41
  3. package/gradle/libs.versions.toml +22 -0
  4. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  5. package/gradle/wrapper/gradle-wrapper.properties +3 -1
  6. package/gradle.properties +2 -0
  7. package/gradlew +32 -18
  8. package/gradlew.bat +22 -18
  9. package/package.json +21 -11
  10. package/react-native-gradle-plugin/README.md +17 -0
  11. package/react-native-gradle-plugin/build.gradle.kts +83 -0
  12. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/ReactExtension.kt +78 -10
  13. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt +320 -0
  14. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactRootProjectPlugin.kt +86 -0
  15. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/TaskConfiguration.kt +36 -25
  16. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/internal/PrivateReactExtension.kt +11 -4
  17. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt +60 -50
  18. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTask.kt +252 -0
  19. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/GenerateCodegenArtifactsTask.kt +8 -3
  20. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTask.kt +11 -18
  21. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateEntryPointTask.kt +108 -0
  22. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GeneratePackageListTask.kt +214 -0
  23. package/{src/main/kotlin/com/facebook/react/tasks → react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal}/BuildCodegenCLITask.kt +24 -17
  24. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/CustomExecTask.kt +46 -0
  25. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/internal/PrepareBoostTask.kt +10 -4
  26. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/PrepareGflagsTask.kt +119 -0
  27. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/internal/PrepareGlogTask.kt +26 -18
  28. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/internal/PreparePrefabHeadersTask.kt +28 -12
  29. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/tasks/internal/utils/PrefabPreprocessingEntry.kt +4 -3
  30. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt +163 -0
  31. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/BackwardCompatUtils.kt +71 -0
  32. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt +299 -0
  33. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt +64 -0
  34. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt +59 -65
  35. package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/utils/PathUtils.kt +44 -30
  36. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt +119 -0
  37. package/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PropertyUtils.kt +96 -0
  38. package/settings-plugin/build.gradle.kts +73 -0
  39. package/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt +217 -0
  40. package/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsPlugin.kt +23 -0
  41. package/settings-plugin/src/test/kotlin/com/facebook/react/ReactSettingsExtensionTest.kt +475 -0
  42. package/settings.gradle.kts +10 -1
  43. package/shared/build.gradle.kts +52 -0
  44. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingAndroidProjectJson.kt +18 -0
  45. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingConfigJson.kt +14 -0
  46. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingDependenciesJson.kt +18 -0
  47. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingDependenciesPlatformAndroidJson.kt +23 -0
  48. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingDependenciesPlatformJson.kt +12 -0
  49. package/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingProjectJson.kt +10 -0
  50. package/{src → shared/src}/main/kotlin/com/facebook/react/model/ModelCodegenConfig.kt +2 -1
  51. package/{src → shared/src}/main/kotlin/com/facebook/react/model/ModelPackageJson.kt +1 -1
  52. package/shared/src/main/kotlin/com/facebook/react/utils/JsonUtils.kt +43 -0
  53. package/shared/src/main/kotlin/com/facebook/react/utils/KotlinStdlibCompatUtils.kt +32 -0
  54. package/{src → shared/src}/main/kotlin/com/facebook/react/utils/Os.kt +7 -6
  55. package/{src → shared/src}/main/kotlin/com/facebook/react/utils/TaskUtils.kt +4 -5
  56. package/shared/src/test/kotlin/com/facebook/react/utils/JsonUtilsTest.kt +382 -0
  57. package/shared/src/test/kotlin/com/facebook/react/utils/KotlinStdlibCompatUtilsTest.kt +82 -0
  58. package/shared/src/test/kotlin/com/facebook/react/utils/OsTest.kt +87 -0
  59. package/shared/src/test/kotlin/com/facebook/react/utils/TaskUtilsTest.kt +76 -0
  60. package/shared-testutil/build.gradle.kts +46 -0
  61. package/shared-testutil/src/main/kotlin/com/facebook/react/tests/OsRule.kt +52 -0
  62. package/shared-testutil/src/main/kotlin/com/facebook/react/tests/WithOs.kt +17 -0
  63. package/build/libs/react-native-gradle-plugin.jar +0 -0
  64. package/index.js +0 -73
  65. package/src/main/kotlin/com/facebook/react/ReactPlugin.kt +0 -212
  66. package/src/main/kotlin/com/facebook/react/tasks/internal/PrepareJSCTask.kt +0 -50
  67. package/src/main/kotlin/com/facebook/react/tasks/internal/PrepareLibeventTask.kt +0 -51
  68. package/src/main/kotlin/com/facebook/react/utils/AgpConfiguratorUtils.kt +0 -54
  69. package/src/main/kotlin/com/facebook/react/utils/BackwardCompatUtils.kt +0 -48
  70. package/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt +0 -109
  71. package/src/main/kotlin/com/facebook/react/utils/JsonUtils.kt +0 -21
  72. package/src/main/kotlin/com/facebook/react/utils/ProjectUtils.kt +0 -55
  73. /package/{src → react-native-gradle-plugin/src}/main/kotlin/com/facebook/react/utils/FileUtils.kt +0 -0
  74. /package/{src → shared/src}/main/kotlin/com/facebook/react/model/ModelCodegenConfigAndroid.kt +0 -0
@@ -12,6 +12,7 @@ import com.facebook.react.utils.detectOSAwareHermesCommand
12
12
  import com.facebook.react.utils.moveTo
13
13
  import com.facebook.react.utils.windowsAwareCommandLine
14
14
  import java.io.File
15
+ import javax.inject.Inject
15
16
  import org.gradle.api.DefaultTask
16
17
  import org.gradle.api.file.ConfigurableFileTree
17
18
  import org.gradle.api.file.DirectoryProperty
@@ -19,6 +20,7 @@ import org.gradle.api.file.RegularFileProperty
19
20
  import org.gradle.api.provider.ListProperty
20
21
  import org.gradle.api.provider.Property
21
22
  import org.gradle.api.tasks.*
23
+ import org.gradle.process.ExecOperations
22
24
 
23
25
  abstract class BundleHermesCTask : DefaultTask() {
24
26
 
@@ -26,19 +28,21 @@ abstract class BundleHermesCTask : DefaultTask() {
26
28
  group = "react"
27
29
  }
28
30
 
31
+ @get:Inject abstract val execOperations: ExecOperations
32
+
29
33
  @get:Internal abstract val root: DirectoryProperty
30
34
 
31
35
  @get:InputFiles
32
36
  val sources: ConfigurableFileTree =
33
- project.fileTree(root) {
34
- it.include("**/*.js")
35
- it.include("**/*.jsx")
36
- it.include("**/*.ts")
37
- it.include("**/*.tsx")
38
- it.exclude("**/android/**/*")
39
- it.exclude("**/ios/**/*")
40
- it.exclude("**/build/**/*")
41
- it.exclude("**/node_modules/**/*")
37
+ project.fileTree(root) { fileTree ->
38
+ fileTree.include("**/*.js")
39
+ fileTree.include("**/*.jsx")
40
+ fileTree.include("**/*.ts")
41
+ fileTree.include("**/*.tsx")
42
+ fileTree.exclude("**/android/**/*")
43
+ fileTree.exclude("**/ios/**/*")
44
+ fileTree.exclude("**/build/**/*")
45
+ fileTree.exclude("**/node_modules/**/*")
42
46
  }
43
47
 
44
48
  @get:Input abstract val nodeExecutableAndArgs: ListProperty<String>
@@ -107,7 +111,11 @@ abstract class BundleHermesCTask : DefaultTask() {
107
111
  val composeScriptFile = File(reactNativeDir, "scripts/compose-source-maps.js")
108
112
  val composeSourceMapsCommand =
109
113
  getComposeSourceMapsCommand(
110
- composeScriptFile, packagerSourceMap, compilerSourceMap, outputSourceMap)
114
+ composeScriptFile,
115
+ packagerSourceMap,
116
+ compilerSourceMap,
117
+ outputSourceMap,
118
+ )
111
119
  runCommand(composeSourceMapsCommand)
112
120
  }
113
121
  }
@@ -127,66 +135,67 @@ abstract class BundleHermesCTask : DefaultTask() {
127
135
  File(jsIntermediateSourceMapsDir.get().asFile, "$bundleAssetName.compiler.map")
128
136
 
129
137
  private fun runCommand(command: List<Any>) {
130
- val process = ProcessBuilder(command.map { it.toString() })
131
- .directory(root.get().asFile)
132
- .redirectErrorStream(true)
133
- .start()
134
- val exitCode = process.waitFor()
135
- if (exitCode != 0) {
136
- throw org.gradle.api.GradleException("Command failed with exit code $exitCode: $command")
138
+ execOperations.exec { exec ->
139
+ exec.workingDir(root.get().asFile)
140
+ exec.commandLine(command)
137
141
  }
138
142
  }
139
143
 
140
- internal fun getBundleCommand(bundleFile: File, sourceMapFile: File): List<Any> =
141
- windowsAwareCommandLine(
142
- buildList {
143
- val rootFile = root.get().asFile
144
- addAll(nodeExecutableAndArgs.get())
145
- add(cliFile.get().asFile.cliPath(rootFile))
146
- add(bundleCommand.get())
147
- add("--platform")
148
- add("android")
149
- add("--dev")
150
- add(devEnabled.get().toString())
151
- add("--reset-cache")
152
- add("--entry-file")
153
- add(entryFile.get().asFile.cliPath(rootFile))
154
- add("--bundle-output")
155
- add(bundleFile.cliPath(rootFile))
156
- add("--assets-dest")
157
- add(resourcesDir.get().asFile.cliPath(rootFile))
158
- add("--sourcemap-output")
159
- add(sourceMapFile.cliPath(rootFile))
160
- if (bundleConfig.isPresent) {
161
- add("--config")
162
- add(bundleConfig.get().asFile.cliPath(rootFile))
163
- }
164
- add("--minify")
165
- add(minifyEnabled.get().toString())
166
- addAll(extraPackagerArgs.get())
167
- add("--verbose")
168
- })
144
+ internal fun getBundleCommand(bundleFile: File, sourceMapFile: File): List<Any> {
145
+ val rootFile = root.get().asFile
146
+ val commandLine =
147
+ mutableListOf<String>().apply {
148
+ addAll(nodeExecutableAndArgs.get())
149
+ add(cliFile.get().asFile.cliPath(rootFile))
150
+ add(bundleCommand.get())
151
+ add("--platform")
152
+ add("android")
153
+ add("--dev")
154
+ add(devEnabled.get().toString())
155
+ add("--reset-cache")
156
+ add("--entry-file")
157
+ add(entryFile.get().asFile.cliPath(rootFile))
158
+ add("--bundle-output")
159
+ add(bundleFile.cliPath(rootFile))
160
+ add("--assets-dest")
161
+ add(resourcesDir.get().asFile.cliPath(rootFile))
162
+ add("--sourcemap-output")
163
+ add(sourceMapFile.cliPath(rootFile))
164
+ if (bundleConfig.isPresent) {
165
+ add("--config")
166
+ add(bundleConfig.get().asFile.cliPath(rootFile))
167
+ }
168
+ add("--minify")
169
+ add(minifyEnabled.get().toString())
170
+ addAll(extraPackagerArgs.get())
171
+ add("--verbose")
172
+ }
173
+ return windowsAwareCommandLine(commandLine)
174
+ }
169
175
 
170
176
  internal fun getHermescCommand(
171
177
  hermesCommand: String,
172
178
  bytecodeFile: File,
173
- bundleFile: File
179
+ bundleFile: File,
174
180
  ): List<Any> {
175
181
  val rootFile = root.get().asFile
176
182
  return windowsAwareCommandLine(
177
183
  hermesCommand,
184
+ "-w",
178
185
  "-emit-binary",
186
+ "-max-diagnostic-width=80",
179
187
  "-out",
180
188
  bytecodeFile.cliPath(rootFile),
181
189
  bundleFile.cliPath(rootFile),
182
- *hermesFlags.get().toTypedArray())
190
+ *hermesFlags.get().toTypedArray(),
191
+ )
183
192
  }
184
193
 
185
194
  internal fun getComposeSourceMapsCommand(
186
195
  composeScript: File,
187
196
  packagerSourceMap: File,
188
197
  compilerSourceMap: File,
189
- outputSourceMap: File
198
+ outputSourceMap: File,
190
199
  ): List<Any> {
191
200
  val rootFile = root.get().asFile
192
201
  return windowsAwareCommandLine(
@@ -195,6 +204,7 @@ abstract class BundleHermesCTask : DefaultTask() {
195
204
  packagerSourceMap.cliPath(rootFile),
196
205
  compilerSourceMap.cliPath(rootFile),
197
206
  "-o",
198
- outputSourceMap.cliPath(rootFile))
207
+ outputSourceMap.cliPath(rootFile),
208
+ )
199
209
  }
200
210
  }
@@ -0,0 +1,252 @@
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
+ package com.facebook.react.tasks
9
+
10
+ import com.facebook.react.model.ModelAutolinkingConfigJson
11
+ import com.facebook.react.model.ModelAutolinkingDependenciesPlatformAndroidJson
12
+ import com.facebook.react.utils.JsonUtils
13
+ import java.io.File
14
+ import org.gradle.api.DefaultTask
15
+ import org.gradle.api.file.DirectoryProperty
16
+ import org.gradle.api.file.RegularFileProperty
17
+ import org.gradle.api.tasks.InputFile
18
+ import org.gradle.api.tasks.OutputDirectory
19
+ import org.gradle.api.tasks.TaskAction
20
+
21
+ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
22
+
23
+ init {
24
+ group = "react"
25
+ }
26
+
27
+ @get:InputFile abstract val autolinkInputFile: RegularFileProperty
28
+
29
+ @get:OutputDirectory abstract val generatedOutputDirectory: DirectoryProperty
30
+
31
+ @TaskAction
32
+ fun taskAction() {
33
+ val model = JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile)
34
+
35
+ val packages = filterAndroidPackages(model)
36
+ val cmakeFileContent = generateCmakeFileContent(packages)
37
+ val cppFileContent = generateCppFileContent(packages)
38
+
39
+ val outputDir = generatedOutputDirectory.get().asFile
40
+ outputDir.mkdirs()
41
+ File(outputDir, CMAKE_FILENAME).apply { writeText(cmakeFileContent) }
42
+ File(outputDir, CPP_FILENAME).apply { writeText(cppFileContent) }
43
+ File(outputDir, H_FILENAME).apply { writeText(hTemplate) }
44
+ }
45
+
46
+ internal fun filterAndroidPackages(
47
+ model: ModelAutolinkingConfigJson?
48
+ ): List<ModelAutolinkingDependenciesPlatformAndroidJson> =
49
+ model?.dependencies?.values?.mapNotNull { it.platforms?.android } ?: emptyList()
50
+
51
+ internal fun generateCmakeFileContent(
52
+ packages: List<ModelAutolinkingDependenciesPlatformAndroidJson>
53
+ ): String {
54
+ val libraryIncludes =
55
+ packages.joinToString("\n") { dep ->
56
+ var addDirectoryString = ""
57
+ val libraryName = dep.libraryName
58
+ val cmakeListsPath = dep.cmakeListsPath
59
+ val cxxModuleCMakeListsPath = dep.cxxModuleCMakeListsPath
60
+ if (libraryName != null && cmakeListsPath != null) {
61
+ // If user provided a custom cmakeListsPath, let's honor it.
62
+ val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
63
+ addDirectoryString +=
64
+ "add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
65
+ }
66
+ if (cxxModuleCMakeListsPath != null) {
67
+ // If user provided a custom cxxModuleCMakeListsPath, let's honor it.
68
+ val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
69
+ addDirectoryString +=
70
+ "\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
71
+ }
72
+ addDirectoryString
73
+ }
74
+
75
+ val libraryModules =
76
+ packages.joinToString("\n ") { dep ->
77
+ var autolinkedLibraries = ""
78
+ if (dep.libraryName != null) {
79
+ autolinkedLibraries += "$CODEGEN_LIB_PREFIX${dep.libraryName}"
80
+ }
81
+ if (dep.cxxModuleCMakeListsModuleName != null) {
82
+ autolinkedLibraries += "\n${dep.cxxModuleCMakeListsModuleName}"
83
+ }
84
+ autolinkedLibraries
85
+ }
86
+
87
+ return CMAKE_TEMPLATE.replace("{{ libraryIncludes }}", libraryIncludes)
88
+ .replace("{{ libraryModules }}", libraryModules)
89
+ }
90
+
91
+ internal fun generateCppFileContent(
92
+ packages: List<ModelAutolinkingDependenciesPlatformAndroidJson>
93
+ ): String {
94
+ val packagesWithLibraryNames = packages.filter { android -> android.libraryName != null }
95
+
96
+ val cppIncludes =
97
+ packagesWithLibraryNames.joinToString("\n") { dep ->
98
+ var include = "#include <${dep.libraryName}.h>"
99
+ if (dep.componentDescriptors.isNotEmpty()) {
100
+ include +=
101
+ "\n#include <${COMPONENT_INCLUDE_PATH}/${dep.libraryName}/${COMPONENT_DESCRIPTOR_FILENAME}>"
102
+ }
103
+ if (dep.cxxModuleHeaderName != null) {
104
+ include += "\n#include <${dep.cxxModuleHeaderName}.h>"
105
+ }
106
+ include
107
+ }
108
+
109
+ val cppTurboModuleJavaProviders =
110
+ packagesWithLibraryNames.joinToString("\n") { dep ->
111
+ val libraryName = dep.libraryName
112
+ // language=cpp
113
+ """
114
+ auto module_$libraryName = ${libraryName}_ModuleProvider(moduleName, params);
115
+ if (module_$libraryName != nullptr) {
116
+ return module_$libraryName;
117
+ }
118
+ """
119
+ .trimIndent()
120
+ }
121
+
122
+ val cppTurboModuleCxxProviders =
123
+ packagesWithLibraryNames
124
+ .filter { it.cxxModuleHeaderName != null }
125
+ .joinToString("\n") { dep ->
126
+ val cxxModuleHeaderName = dep.cxxModuleHeaderName
127
+ // language=cpp
128
+ """
129
+ if (moduleName == $cxxModuleHeaderName::kModuleName) {
130
+ return std::make_shared<$cxxModuleHeaderName>(jsInvoker);
131
+ }
132
+ """
133
+ .trimIndent()
134
+ }
135
+
136
+ val cppComponentDescriptors =
137
+ packagesWithLibraryNames
138
+ .filter { it.componentDescriptors.isNotEmpty() }
139
+ .joinToString("\n") {
140
+ it.componentDescriptors.joinToString("\n") {
141
+ "providerRegistry->add(concreteComponentDescriptorProvider<$it>());"
142
+ }
143
+ }
144
+
145
+ return CPP_TEMPLATE.replace("{{ autolinkingCppIncludes }}", cppIncludes)
146
+ .replace("{{ autolinkingCppTurboModuleJavaProviders }}", cppTurboModuleJavaProviders)
147
+ .replace("{{ autolinkingCppTurboModuleCxxProviders }}", cppTurboModuleCxxProviders)
148
+ .replace("{{ autolinkingCppComponentDescriptors }}", cppComponentDescriptors)
149
+ }
150
+
151
+ companion object {
152
+ const val CMAKE_FILENAME = "Android-autolinking.cmake"
153
+
154
+ const val H_FILENAME = "autolinking.h"
155
+ const val CPP_FILENAME = "autolinking.cpp"
156
+
157
+ const val CODEGEN_LIB_PREFIX = "react_codegen_"
158
+
159
+ const val COMPONENT_DESCRIPTOR_FILENAME = "ComponentDescriptors.h"
160
+ const val COMPONENT_INCLUDE_PATH = "react/renderer/components"
161
+
162
+ internal fun sanitizeCmakeListsPath(cmakeListsPath: String): String =
163
+ cmakeListsPath.replace("CMakeLists.txt", "").replace(" ", "\\ ")
164
+
165
+ // language=cmake
166
+ val CMAKE_TEMPLATE =
167
+ """
168
+ # This code was generated by [React Native](https://www.npmjs.com/package/@react-native/gradle-plugin)
169
+ cmake_minimum_required(VERSION 3.13)
170
+ set(CMAKE_VERBOSE_MAKEFILE on)
171
+
172
+ # We set REACTNATIVE_MERGED_SO so libraries/apps can selectively decide to depend on either libreactnative.so
173
+ # or link against a old prefab target (this is needed for React Native 0.76 on).
174
+ set(REACTNATIVE_MERGED_SO true)
175
+
176
+ {{ libraryIncludes }}
177
+
178
+ set(AUTOLINKED_LIBRARIES
179
+ {{ libraryModules }}
180
+ )
181
+ """
182
+ .trimIndent()
183
+
184
+ // language=cpp
185
+ val CPP_TEMPLATE =
186
+ """
187
+ /**
188
+ * This code was generated by [React Native](https://www.npmjs.com/package/@react-native/gradle-plugin).
189
+ *
190
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
191
+ * once the code is regenerated.
192
+ *
193
+ */
194
+
195
+ #include "autolinking.h"
196
+ {{ autolinkingCppIncludes }}
197
+
198
+ namespace facebook {
199
+ namespace react {
200
+
201
+ std::shared_ptr<TurboModule> autolinking_ModuleProvider(const std::string moduleName, const JavaTurboModule::InitParams &params) {
202
+ {{ autolinkingCppTurboModuleJavaProviders }}
203
+ return nullptr;
204
+ }
205
+
206
+ std::shared_ptr<TurboModule> autolinking_cxxModuleProvider(const std::string moduleName, const std::shared_ptr<CallInvoker>& jsInvoker) {
207
+ {{ autolinkingCppTurboModuleCxxProviders }}
208
+ return nullptr;
209
+ }
210
+
211
+ void autolinking_registerProviders(std::shared_ptr<ComponentDescriptorProviderRegistry const> providerRegistry) {
212
+ {{ autolinkingCppComponentDescriptors }}
213
+ return;
214
+ }
215
+
216
+ } // namespace react
217
+ } // namespace facebook
218
+ """
219
+ .trimIndent()
220
+
221
+ // language=cpp
222
+ val hTemplate =
223
+ """
224
+ /**
225
+ * This code was generated by [React Native](https://www.npmjs.com/package/@react-native/gradle-plugin).
226
+ *
227
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
228
+ * once the code is regenerated.
229
+ *
230
+ */
231
+
232
+ #pragma once
233
+
234
+ #include <ReactCommon/CallInvoker.h>
235
+ #include <ReactCommon/JavaTurboModule.h>
236
+ #include <ReactCommon/TurboModule.h>
237
+ #include <jsi/jsi.h>
238
+ #include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
239
+
240
+ namespace facebook {
241
+ namespace react {
242
+
243
+ std::shared_ptr<TurboModule> autolinking_ModuleProvider(const std::string moduleName, const JavaTurboModule::InitParams &params);
244
+ std::shared_ptr<TurboModule> autolinking_cxxModuleProvider(const std::string moduleName, const std::shared_ptr<CallInvoker>& jsInvoker);
245
+ void autolinking_registerProviders(std::shared_ptr<ComponentDescriptorProviderRegistry const> providerRegistry);
246
+
247
+ } // namespace react
248
+ } // namespace facebook
249
+ """
250
+ .trimIndent()
251
+ }
252
+ }
@@ -10,6 +10,7 @@ package com.facebook.react.tasks
10
10
  import com.facebook.react.utils.JsonUtils
11
11
  import com.facebook.react.utils.Os.cliPath
12
12
  import com.facebook.react.utils.windowsAwareCommandLine
13
+ import java.io.File
13
14
  import org.gradle.api.file.Directory
14
15
  import org.gradle.api.file.DirectoryProperty
15
16
  import org.gradle.api.file.RegularFile
@@ -31,6 +32,8 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
31
32
 
32
33
  @get:InputFile abstract val packageJsonFile: RegularFileProperty
33
34
 
35
+ @get:Input abstract val nodeWorkingDir: Property<String>
36
+
34
37
  @get:Input abstract val nodeExecutableAndArgs: ListProperty<String>
35
38
 
36
39
  @get:Input abstract val codegenJavaPackageName: Property<String>
@@ -53,7 +56,7 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
53
56
  internal fun resolveTaskParameters(): Pair<String, String> {
54
57
  val parsedPackageJson =
55
58
  if (packageJsonFile.isPresent && packageJsonFile.get().asFile.exists()) {
56
- JsonUtils.fromCodegenJson(packageJsonFile.get().asFile)
59
+ JsonUtils.fromPackageJson(packageJsonFile.get().asFile)
57
60
  } else {
58
61
  null
59
62
  }
@@ -64,7 +67,7 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
64
67
  }
65
68
 
66
69
  internal fun setupCommandLine(libraryName: String, codegenJavaPackageName: String) {
67
- val workingDir = project.projectDir
70
+ val workingDir = File(nodeWorkingDir.get())
68
71
  commandLine(
69
72
  windowsAwareCommandLine(
70
73
  *nodeExecutableAndArgs.get().toTypedArray(),
@@ -78,6 +81,8 @@ abstract class GenerateCodegenArtifactsTask : Exec() {
78
81
  "--libraryName",
79
82
  libraryName,
80
83
  "--javaPackageName",
81
- codegenJavaPackageName))
84
+ codegenJavaPackageName,
85
+ )
86
+ )
82
87
  }
83
88
  }
@@ -9,9 +9,12 @@ package com.facebook.react.tasks
9
9
 
10
10
  import com.facebook.react.utils.Os.cliPath
11
11
  import com.facebook.react.utils.windowsAwareCommandLine
12
+ import java.io.File
12
13
  import org.gradle.api.file.DirectoryProperty
14
+ import org.gradle.api.file.FileTree
13
15
  import org.gradle.api.file.RegularFile
14
16
  import org.gradle.api.provider.ListProperty
17
+ import org.gradle.api.provider.Property
15
18
  import org.gradle.api.provider.Provider
16
19
  import org.gradle.api.tasks.*
17
20
 
@@ -28,24 +31,11 @@ abstract class GenerateCodegenSchemaTask : Exec() {
28
31
 
29
32
  @get:Internal abstract val generatedSrcDir: DirectoryProperty
30
33
 
34
+ @get:Input abstract val nodeWorkingDir: Property<String>
35
+
31
36
  @get:Input abstract val nodeExecutableAndArgs: ListProperty<String>
32
37
 
33
- @get:InputFiles
34
- val jsInputFiles =
35
- project.fileTree(jsRootDir) {
36
- it.include("**/*.js")
37
- it.include("**/*.ts")
38
- // Those are known build paths where the source map or other
39
- // .js files could be stored/generated. We want to make sure we don't pick them up
40
- // for execution avoidance.
41
- it.exclude("**/generated/source/codegen/**/*")
42
- it.exclude("**/build/ASSETS/**/*")
43
- it.exclude("**/build/RES/**/*")
44
- it.exclude("**/build/generated/assets/react/**/*")
45
- it.exclude("**/build/generated/res/react/**/*")
46
- it.exclude("**/build/generated/sourcemaps/react/**/*")
47
- it.exclude("**/build/intermediates/sourcemaps/react/**/*")
48
- }
38
+ @get:InputFiles abstract val jsInputFiles: Property<FileTree>
49
39
 
50
40
  @get:OutputFile
51
41
  val generatedSchemaFile: Provider<RegularFile> = generatedSrcDir.file("schema.json")
@@ -64,7 +54,7 @@ abstract class GenerateCodegenSchemaTask : Exec() {
64
54
  }
65
55
 
66
56
  internal fun setupCommandLine() {
67
- val workingDir = project.projectDir
57
+ val workingDir = File(nodeWorkingDir.get())
68
58
  commandLine(
69
59
  windowsAwareCommandLine(
70
60
  *nodeExecutableAndArgs.get().toTypedArray(),
@@ -75,8 +65,11 @@ abstract class GenerateCodegenSchemaTask : Exec() {
75
65
  .cliPath(workingDir),
76
66
  "--platform",
77
67
  "android",
68
+ "--exclude",
69
+ "NativeSampleTurboModule",
78
70
  generatedSchemaFile.get().asFile.cliPath(workingDir),
79
71
  jsRootDir.asFile.get().cliPath(workingDir),
80
- ))
72
+ )
73
+ )
81
74
  }
82
75
  }
@@ -0,0 +1,108 @@
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
+ package com.facebook.react.tasks
9
+
10
+ import com.facebook.react.utils.JsonUtils
11
+ import java.io.File
12
+ import org.gradle.api.DefaultTask
13
+ import org.gradle.api.file.DirectoryProperty
14
+ import org.gradle.api.file.RegularFileProperty
15
+ import org.gradle.api.tasks.InputFile
16
+ import org.gradle.api.tasks.OutputDirectory
17
+ import org.gradle.api.tasks.TaskAction
18
+
19
+ abstract class GenerateEntryPointTask : DefaultTask() {
20
+
21
+ init {
22
+ group = "react"
23
+ }
24
+
25
+ @get:InputFile abstract val autolinkInputFile: RegularFileProperty
26
+
27
+ @get:OutputDirectory abstract val generatedOutputDirectory: DirectoryProperty
28
+
29
+ @TaskAction
30
+ fun taskAction() {
31
+ val model =
32
+ JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile)
33
+ ?: error(
34
+ """
35
+ RNGP - Autolinking: Could not parse autolinking config file:
36
+ ${autolinkInputFile.get().asFile.absolutePath}
37
+
38
+ The file is either missing or not containing valid JSON so the build won't succeed.
39
+ """
40
+ .trimIndent()
41
+ )
42
+
43
+ val packageName =
44
+ model.project?.android?.packageName
45
+ ?: error(
46
+ "RNGP - Autolinking: Could not find project.android.packageName in react-native config output! Could not autolink packages without this field."
47
+ )
48
+ val generatedFileContents = composeFileContent(packageName)
49
+
50
+ val outputDir = generatedOutputDirectory.get().asFile
51
+ outputDir.mkdirs()
52
+ File(outputDir, GENERATED_FILENAME).apply {
53
+ parentFile.mkdirs()
54
+ writeText(generatedFileContents)
55
+ }
56
+ }
57
+
58
+ internal fun composeFileContent(packageName: String): String =
59
+ generatedFileContentsTemplate.replace("{{packageName}}", packageName)
60
+
61
+ companion object {
62
+ const val GENERATED_FILENAME = "com/facebook/react/ReactNativeApplicationEntryPoint.java"
63
+
64
+ // language=java
65
+ val generatedFileContentsTemplate =
66
+ """
67
+ package com.facebook.react;
68
+
69
+ import android.app.Application;
70
+ import android.content.Context;
71
+ import android.content.res.Resources;
72
+
73
+ import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
74
+ import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger;
75
+ import com.facebook.react.views.view.WindowUtilKt;
76
+ import com.facebook.react.soloader.OpenSourceMergedSoMapping;
77
+ import com.facebook.soloader.SoLoader;
78
+
79
+ import java.io.IOException;
80
+
81
+ /**
82
+ * This class is the entry point for loading React Native using the configuration
83
+ * that the users specifies in their .gradle files.
84
+ *
85
+ * The `loadReactNative(this)` method invocation should be called inside the
86
+ * application onCreate otherwise the app won't load correctly.
87
+ */
88
+ public class ReactNativeApplicationEntryPoint {
89
+ public static void loadReactNative(Context context) {
90
+ try {
91
+ SoLoader.init(context, OpenSourceMergedSoMapping.INSTANCE);
92
+ } catch (IOException error) {
93
+ throw new RuntimeException(error);
94
+ }
95
+
96
+ if ({{packageName}}.BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
97
+ DefaultNewArchitectureEntryPoint.load();
98
+ }
99
+
100
+ if ({{packageName}}.BuildConfig.IS_EDGE_TO_EDGE_ENABLED) {
101
+ WindowUtilKt.setEdgeToEdgeFeatureFlagOn();
102
+ }
103
+ }
104
+ }
105
+ """
106
+ .trimIndent()
107
+ }
108
+ }