|
| 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.ModelAutolinkingDependenciesJson |
| 11 | +import com.facebook.react.utils.JsonUtils |
| 12 | +import java.io.File |
| 13 | +import org.gradle.api.DefaultTask |
| 14 | +import org.gradle.api.file.DirectoryProperty |
| 15 | +import org.gradle.api.file.RegularFileProperty |
| 16 | +import org.gradle.api.tasks.InputFile |
| 17 | +import org.gradle.api.tasks.OutputDirectory |
| 18 | +import org.gradle.api.tasks.TaskAction |
| 19 | + |
| 20 | +abstract class GeneratePackageListTask : DefaultTask() { |
| 21 | + |
| 22 | + init { |
| 23 | + group = "react" |
| 24 | + } |
| 25 | + |
| 26 | + @get:InputFile abstract val autolinkInputFile: RegularFileProperty |
| 27 | + |
| 28 | + @get:OutputDirectory abstract val generatedOutputDirectory: DirectoryProperty |
| 29 | + |
| 30 | + @TaskAction |
| 31 | + fun taskAction() { |
| 32 | + val model = JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile) |
| 33 | + |
| 34 | + val packageName = |
| 35 | + model?.project?.android?.packageName |
| 36 | + ?: error( |
| 37 | + "RNGP - Autolinking: Could not find project.android.packageName in react-native config output! Could not autolink packages without this field.") |
| 38 | + val packages = model.dependencies?.values ?: emptyList() |
| 39 | + |
| 40 | + val packageImports = composePackageImports(packageName, packages) |
| 41 | + val packageClassInstance = composePackageInstance(packageName, packages) |
| 42 | + val generatedFileContents = composeFileContent(packageImports, packageClassInstance) |
| 43 | + |
| 44 | + val outputDir = generatedOutputDirectory.get().asFile |
| 45 | + outputDir.mkdirs() |
| 46 | + File(outputDir, GENERATED_FILENAME).apply { |
| 47 | + parentFile.mkdirs() |
| 48 | + writeText(generatedFileContents) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + internal fun composePackageImports( |
| 53 | + packageName: String, |
| 54 | + packages: Collection<ModelAutolinkingDependenciesJson> |
| 55 | + ) = |
| 56 | + packages.joinToString("\n") { entry -> |
| 57 | + val packageImportPath = |
| 58 | + requireNotNull(entry.platforms?.android?.packageImportPath) { |
| 59 | + "RNGP - Autolinking: Missing `packageImportPath` in `config` for dependency ${entry.name}. This is required to generate the autolinking package list." |
| 60 | + } |
| 61 | + "// ${entry.name}\n${interpolateDynamicValues(packageImportPath, packageName)}" |
| 62 | + } |
| 63 | + |
| 64 | + internal fun composePackageInstance( |
| 65 | + packageName: String, |
| 66 | + packages: Collection<ModelAutolinkingDependenciesJson> |
| 67 | + ) = |
| 68 | + if (packages.isEmpty()) { |
| 69 | + "" |
| 70 | + } else { |
| 71 | + ",\n " + |
| 72 | + packages.joinToString(",\n ") { entry -> |
| 73 | + val packageInstance = |
| 74 | + requireNotNull(entry.platforms?.android?.packageInstance) { |
| 75 | + "RNGP - Autolinking: Missing `packageInstance` in `config` for dependency ${entry.name}. This is required to generate the autolinking package list." |
| 76 | + } |
| 77 | + interpolateDynamicValues(packageInstance, packageName) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + internal fun composeFileContent(packageImports: String, packageClassInstance: String): String = |
| 82 | + generatedFileContentsTemplate |
| 83 | + .replace("{{ packageImports }}", packageImports) |
| 84 | + .replace("{{ packageClassInstances }}", packageClassInstance) |
| 85 | + |
| 86 | + companion object { |
| 87 | + const val GENERATED_FILENAME = "com/facebook/react/PackageList2.java" |
| 88 | + |
| 89 | + /** |
| 90 | + * Before adding the package replacement mechanism, BuildConfig and R classes were imported |
| 91 | + * automatically into the scope of the file. We want to replace all non-FQDN references to those |
| 92 | + * classes with the package name of the MainApplication. |
| 93 | + * |
| 94 | + * We want to match "R" or "BuildConfig": |
| 95 | + * - new Package(R.string…), |
| 96 | + * - Module.configure(BuildConfig); |
| 97 | + * ^ hence including (BuildConfig|R) |
| 98 | + * but we don't want to match "R": |
| 99 | + * - new Package(getResources…), |
| 100 | + * - new PackageR…, |
| 101 | + * - new Royal…, |
| 102 | + * ^ hence excluding \w before and after matches |
| 103 | + * and "BuildConfig" that has FQDN reference: |
| 104 | + * - Module.configure(com.acme.BuildConfig); |
| 105 | + * ^ hence excluding . before the match. |
| 106 | + */ |
| 107 | + internal fun interpolateDynamicValues(input: String, packageName: String): String = |
| 108 | + input.replace(Regex("([^.\\w])(BuildConfig|R)(\\W)")) { match -> |
| 109 | + val (prefix, className, suffix) = match.destructured |
| 110 | + "${prefix}${packageName}.${className}${suffix}" |
| 111 | + } |
| 112 | + |
| 113 | + // language=java |
| 114 | + val generatedFileContentsTemplate = |
| 115 | + """ |
| 116 | + package com.facebook.react; |
| 117 | + |
| 118 | + import android.app.Application; |
| 119 | + import android.content.Context; |
| 120 | + import android.content.res.Resources; |
| 121 | + |
| 122 | + import com.facebook.react.ReactPackage; |
| 123 | + import com.facebook.react.shell.MainPackageConfig; |
| 124 | + import com.facebook.react.shell.MainReactPackage; |
| 125 | + import java.util.Arrays; |
| 126 | + import java.util.ArrayList; |
| 127 | + |
| 128 | + {{ packageImports }} |
| 129 | + |
| 130 | + public class PackageList2 { |
| 131 | + private Application application; |
| 132 | + private ReactNativeHost reactNativeHost; |
| 133 | + private MainPackageConfig mConfig; |
| 134 | + |
| 135 | + public PackageList(ReactNativeHost reactNativeHost) { |
| 136 | + this(reactNativeHost, null); |
| 137 | + } |
| 138 | + |
| 139 | + public PackageList(Application application) { |
| 140 | + this(application, null); |
| 141 | + } |
| 142 | + |
| 143 | + public PackageList(ReactNativeHost reactNativeHost, MainPackageConfig config) { |
| 144 | + this.reactNativeHost = reactNativeHost; |
| 145 | + mConfig = config; |
| 146 | + } |
| 147 | + |
| 148 | + public PackageList(Application application, MainPackageConfig config) { |
| 149 | + this.reactNativeHost = null; |
| 150 | + this.application = application; |
| 151 | + mConfig = config; |
| 152 | + } |
| 153 | + |
| 154 | + private ReactNativeHost getReactNativeHost() { |
| 155 | + return this.reactNativeHost; |
| 156 | + } |
| 157 | + |
| 158 | + private Resources getResources() { |
| 159 | + return this.getApplication().getResources(); |
| 160 | + } |
| 161 | + |
| 162 | + private Application getApplication() { |
| 163 | + if (this.reactNativeHost == null) return this.application; |
| 164 | + return this.reactNativeHost.getApplication(); |
| 165 | + } |
| 166 | + |
| 167 | + private Context getApplicationContext() { |
| 168 | + return this.getApplication().getApplicationContext(); |
| 169 | + } |
| 170 | + |
| 171 | + public ArrayList<ReactPackage> getPackages() { |
| 172 | + return new ArrayList<>(Arrays.<ReactPackage>asList( |
| 173 | + new MainReactPackage(mConfig){{ packageClassInstances }} |
| 174 | + )); |
| 175 | + } |
| 176 | + } |
| 177 | + """ |
| 178 | + .trimIndent() |
| 179 | + } |
| 180 | +} |
0 commit comments