|
7 | 7 |
|
8 | 8 | package com.facebook.react |
9 | 9 |
|
| 10 | +import com.facebook.react.utils.JsonUtils |
10 | 11 | import com.facebook.react.utils.projectPathToLibraryName |
| 12 | +import java.io.File |
11 | 13 | import javax.inject.Inject |
12 | 14 | import org.gradle.api.Project |
13 | 15 | import org.gradle.api.file.DirectoryProperty |
14 | | -import org.gradle.api.file.FileCollection |
15 | 16 | import org.gradle.api.file.RegularFileProperty |
16 | 17 | import org.gradle.api.provider.ListProperty |
17 | 18 | import org.gradle.api.provider.Property |
18 | 19 |
|
19 | | -abstract class ReactExtension @Inject constructor(project: Project) { |
| 20 | +abstract class ReactExtension @Inject constructor(val project: Project) { |
20 | 21 |
|
21 | 22 | private val objects = project.objects |
22 | 23 |
|
@@ -149,34 +150,54 @@ abstract class ReactExtension @Inject constructor(project: Project) { |
149 | 150 | val codegenJavaPackageName: Property<String> = |
150 | 151 | objects.property(String::class.java).convention("com.facebook.fbreact.specs") |
151 | 152 |
|
152 | | - /** Auto-linking Config */ |
| 153 | + /** Auto-linking Utils */ |
153 | 154 |
|
154 | 155 | /** |
155 | | - * Location of the JSON file used to configure autolinking. This file is the output of the |
156 | | - * `@react-native-community/cli` config command. |
| 156 | + * Utility function to autolink libraries to the app. |
157 | 157 | * |
158 | | - * If not specified, RNGP will just invoke whatever you pass as [autolinkConfigCommand]. |
159 | | - */ |
160 | | - val autolinkConfigFile: RegularFileProperty = objects.fileProperty() |
161 | | - |
162 | | - /** |
163 | | - * The command to invoke as source of truth for the autolinking configuration. Default is `["npx", |
164 | | - * "@react-native-community/cli", "config"]`. |
165 | | - */ |
166 | | - val autolinkConfigCommand: ListProperty<String> = |
167 | | - objects |
168 | | - .listProperty(String::class.java) |
169 | | - .convention(listOf("npx", "@react-native-community/cli", "config")) |
170 | | - |
171 | | - /** |
172 | | - * Location of the lock files used to consider whether autolinking [autolinkConfigCommand] should |
173 | | - * re-execute or not. If file collection is unchanged, the autolinking command will not be |
174 | | - * re-executed. |
175 | | - * |
176 | | - * If not specified, RNGP will just look for both yarn.lock and package.lock in the [root] folder. |
177 | | - */ |
178 | | - val autolinkLockFiles: Property<FileCollection> = |
179 | | - objects |
180 | | - .property(FileCollection::class.java) |
181 | | - .convention(root.files("../yarn.lock", "../package-lock.json")) |
| 158 | + * This function will read the autolinking configuration file and add Gradle dependencies to the |
| 159 | + * app. This function should be invoked inside the react {} block in the app's build.gradle and is |
| 160 | + * necessary for libraries to be linked correctly. |
| 161 | + */ |
| 162 | + fun autolinkLibrariesWithApp() { |
| 163 | + val inputFile = |
| 164 | + project.rootProject.layout.buildDirectory |
| 165 | + .file("generated/autolinking/autolinking.json") |
| 166 | + .get() |
| 167 | + .asFile |
| 168 | + val dependenciesToApply = getGradleDependenciesToApply(inputFile) |
| 169 | + dependenciesToApply.forEach { (configuration, path) -> |
| 170 | + project.dependencies.add(configuration, project.dependencies.project(mapOf("path" to path))) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + companion object { |
| 175 | + /** |
| 176 | + * Util function to construct a list of Gradle Configuration <-> Project name pairs for |
| 177 | + * autolinking. Pairs looks like: "implementation" -> ":react-native_oss-library-example" |
| 178 | + * |
| 179 | + * They will be applied to the Gradle project for linking the libraries. |
| 180 | + * |
| 181 | + * @param inputFile The file to read the autolinking configuration from. |
| 182 | + * @return A list of Gradle Configuration <-> Project name pairs. |
| 183 | + */ |
| 184 | + internal fun getGradleDependenciesToApply(inputFile: File): MutableList<Pair<String, String>> { |
| 185 | + val model = JsonUtils.fromAutolinkingConfigJson(inputFile) |
| 186 | + val result = mutableListOf<Pair<String, String>>() |
| 187 | + model?.dependencies?.values?.forEach { deps -> |
| 188 | + val nameCleansed = deps.nameCleansed |
| 189 | + val dependencyConfiguration = deps.platforms?.android?.dependencyConfiguration |
| 190 | + val buildTypes = deps.platforms?.android?.buildTypes ?: emptyList() |
| 191 | + if (buildTypes.isEmpty()) { |
| 192 | + result.add((dependencyConfiguration ?: "implementation") to ":$nameCleansed") |
| 193 | + } else { |
| 194 | + buildTypes.forEach { buildType -> |
| 195 | + result.add( |
| 196 | + (dependencyConfiguration ?: "${buildType}Implementation") to ":$nameCleansed") |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + return result |
| 201 | + } |
| 202 | + } |
182 | 203 | } |
0 commit comments