|
| 1 | +package com.google.firebase.gradle.plugins |
| 2 | + |
| 3 | +import com.android.build.api.attributes.BuildTypeAttr |
| 4 | +import com.android.build.gradle.LibraryExtension |
| 5 | +import java.io.File |
| 6 | +import org.gradle.api.Plugin |
| 7 | +import org.gradle.api.Project |
| 8 | +import org.gradle.api.provider.Provider |
| 9 | +import org.gradle.api.tasks.Copy |
| 10 | +import org.gradle.api.tasks.Delete |
| 11 | +import org.gradle.kotlin.dsl.getByType |
| 12 | +import org.gradle.kotlin.dsl.register |
| 13 | + |
| 14 | +/** |
| 15 | + * Facilitates the creation of Firesite compliant reference documentation. |
| 16 | + * |
| 17 | + * This plugin handles a procedure of processes, all registered under the "kotlindoc" task. |
| 18 | + * |
| 19 | + * Those tasks are: |
| 20 | + * - Collect the arguments needed to run Dackka |
| 21 | + * - Run Dackka with [GenerateDocumentationTask] to create the initial reference docs |
| 22 | + * - Clean up the output with [FiresiteTransformTask] to fix minor inconsistencies |
| 23 | + * - Remove the java references generated from the task (we do not currently support them) |
| 24 | + * - Copies the output files to a common directory under the root project's build directory |
| 25 | + * |
| 26 | + * @see GenerateDocumentationTask |
| 27 | + * @see FiresiteTransformTask |
| 28 | + * @see JavadocPlugin |
| 29 | + */ |
| 30 | +abstract class DackkaPlugin : Plugin<Project> { |
| 31 | + override fun apply(project: Project) { |
| 32 | + prepareJavadocConfiguration(project) |
| 33 | + registerCleanDackkaDocumentation(project) |
| 34 | + project.afterEvaluate { |
| 35 | + if (shouldWePublish(project)) { |
| 36 | + val generateDocumentation = registerGenerateDackkaDocumentationTask(project) |
| 37 | + val outputDirectory = generateDocumentation.flatMap { it.outputDirectory } |
| 38 | + val firesiteTransform = registerFiresiteTransformTask(project, outputDirectory) |
| 39 | + val deleteJavaReferences = registerDeleteDackkaGeneratedJavaReferencesTask(project, outputDirectory) |
| 40 | + val copyOutputToCommonDirectory = |
| 41 | + registerCopyDackkaOutputToCommonDirectoryTask(project, outputDirectory) |
| 42 | + |
| 43 | + project.tasks.register("kotlindoc") { |
| 44 | + group = "documentation" |
| 45 | + dependsOn( |
| 46 | + generateDocumentation, |
| 47 | + firesiteTransform, |
| 48 | + deleteJavaReferences, |
| 49 | + copyOutputToCommonDirectory |
| 50 | + ) |
| 51 | + } |
| 52 | + } else { |
| 53 | + project.tasks.register("kotlindoc") |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private fun shouldWePublish(project: Project) = |
| 59 | + project.extensions.getByType<FirebaseLibraryExtension>().publishJavadoc |
| 60 | + |
| 61 | + private fun prepareJavadocConfiguration(project: Project) { |
| 62 | + val javadocConfig = project.javadocConfig |
| 63 | + javadocConfig.dependencies += project.dependencies.create("com.google.code.findbugs:jsr305:3.0.2") |
| 64 | + javadocConfig.dependencies += project.dependencies.create("com.google.errorprone:error_prone_annotations:2.15.0") |
| 65 | + javadocConfig.attributes.attribute( |
| 66 | + BuildTypeAttr.ATTRIBUTE, |
| 67 | + project.objects.named(BuildTypeAttr::class.java, "release") |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + private fun registerGenerateDackkaDocumentationTask(project: Project) = |
| 72 | + project.tasks.register<GenerateDocumentationTask>("generateDackkaDocumentation") { |
| 73 | + with(project.extensions.getByType<LibraryExtension>()) { |
| 74 | + libraryVariants.all { |
| 75 | + if (name == "release") { |
| 76 | + mustRunAfter("createFullJarRelease") |
| 77 | + dependsOn("createFullJarRelease") |
| 78 | + |
| 79 | + val classpath = project.provider { |
| 80 | + runtimeConfiguration.getJars() + project.javadocConfig.getJars() + bootClasspath |
| 81 | + } |
| 82 | + |
| 83 | + val sourcesForJava = sourceSets.flatMap { |
| 84 | + it.javaDirectories.map { it.absoluteFile } + projectSpecificSources(project) |
| 85 | + } |
| 86 | + |
| 87 | + // this will become useful with the agp upgrade, as they're separate in 7.x+ |
| 88 | + val sourcesForKotlin = emptyList<File>() |
| 89 | + val excludedFiles = emptyList<File>() + projectSpecificSuppressedFiles(project) |
| 90 | + |
| 91 | + dependencies.set(classpath) |
| 92 | + javaSources.set(sourcesForJava) |
| 93 | + kotlinSources.set(sourcesForKotlin) |
| 94 | + suppressedFiles.set(excludedFiles) |
| 95 | + |
| 96 | + applyCommonConfigurations() |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // TODO(b/243534168): Remove when fixed |
| 103 | + private fun projectSpecificSources(project: Project) = |
| 104 | + when (project.name) { |
| 105 | + "firebase-common" -> { |
| 106 | + project.project(":firebase-firestore").files("src/main/java/com/google/firebase").toList() |
| 107 | + } |
| 108 | + else -> emptyList() |
| 109 | + } |
| 110 | + |
| 111 | + // TODO(b/243534168): Remove when fixed |
| 112 | + private fun projectSpecificSuppressedFiles(project: Project): List<File> = |
| 113 | + when (project.name) { |
| 114 | + "firebase-common" -> { |
| 115 | + val firestoreProject = project.project(":firebase-firestore") |
| 116 | + firestoreProject.files("src/main/java/com/google/firebase/firestore").toList() |
| 117 | + } |
| 118 | + "firebase-firestore" -> { |
| 119 | + project.files("src/main/java/com/google/firebase/Timestamp.java").toList() |
| 120 | + } |
| 121 | + else -> emptyList() |
| 122 | + } |
| 123 | + |
| 124 | + private fun GenerateDocumentationTask.applyCommonConfigurations() { |
| 125 | + val dackkaFile = project.provider { project.dackkaConfig.singleFile } |
| 126 | + val dackkaOutputDirectory = File(project.buildDir, "dackkaDocumentation") |
| 127 | + |
| 128 | + dackkaJarFile.set(dackkaFile) |
| 129 | + outputDirectory.set(dackkaOutputDirectory) |
| 130 | + } |
| 131 | + |
| 132 | + private fun registerFiresiteTransformTask(project: Project, outputDirectory: Provider<File>) = |
| 133 | + project.tasks.register<FiresiteTransformTask>("firesiteTransform") { |
| 134 | + dackkaFiles.set(outputDirectory) |
| 135 | + } |
| 136 | + |
| 137 | + // If we decide to publish java variants, we'll need to address the generated format as well |
| 138 | + private fun registerDeleteDackkaGeneratedJavaReferencesTask(project: Project, outputDirectory: Provider<File>) = |
| 139 | + project.tasks.register<Delete>("deleteDackkaGeneratedJavaReferences") { |
| 140 | + mustRunAfter("generateDackkaDocumentation") |
| 141 | + |
| 142 | + val filesWeDoNotNeed = listOf( |
| 143 | + "reference/client", |
| 144 | + "reference/com" |
| 145 | + ) |
| 146 | + val filesToDelete = outputDirectory.map { dir -> |
| 147 | + filesWeDoNotNeed.map { |
| 148 | + project.files("${dir.path}/$it") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + delete(filesToDelete) |
| 153 | + } |
| 154 | + |
| 155 | + private fun registerCopyDackkaOutputToCommonDirectoryTask(project: Project, outputDirectory: Provider<File>) = |
| 156 | + project.tasks.register<Copy>("copyDackkaOutputToCommonDirectory") { |
| 157 | + mustRunAfter("deleteDackkaGeneratedJavaReferences") |
| 158 | + mustRunAfter("firesiteTransform") |
| 159 | + |
| 160 | + val referenceFolder = outputDirectory.map { project.file("${it.path}/reference") } |
| 161 | + val outputFolder = project.file("${project.rootProject.buildDir}/firebase-kotlindoc") |
| 162 | + |
| 163 | + from(referenceFolder) |
| 164 | + destinationDir = outputFolder |
| 165 | + } |
| 166 | + |
| 167 | + // Useful for local testing, but may not be desired for standard use (that's why it's not depended on) |
| 168 | + private fun registerCleanDackkaDocumentation(project: Project) = |
| 169 | + project.tasks.register<Delete>("cleanDackkaDocumentation") { |
| 170 | + group = "cleanup" |
| 171 | + |
| 172 | + delete("${project.buildDir}/dackkaDocumentation") |
| 173 | + } |
| 174 | +} |
0 commit comments