|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.gradle.plugins; |
| 16 | + |
| 17 | +import com.android.build.gradle.LibraryExtension; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import java.io.File; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URL; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Optional; |
| 24 | +import org.gradle.api.GradleException; |
| 25 | +import org.gradle.api.Project; |
| 26 | +import org.gradle.api.attributes.Attribute; |
| 27 | +import org.gradle.api.file.FileCollection; |
| 28 | +import org.gradle.api.file.RelativePath; |
| 29 | +import org.gradle.api.tasks.Copy; |
| 30 | +import org.jetbrains.dokka.DokkaConfiguration; |
| 31 | +import org.jetbrains.dokka.gradle.DokkaAndroidTask; |
| 32 | + |
| 33 | +final class Dokka { |
| 34 | + /** |
| 35 | + * Configures the dokka task for a 'firebase-library'. |
| 36 | + * |
| 37 | + * <p>Configuration includes: |
| 38 | + * |
| 39 | + * <ol> |
| 40 | + * <li>Configure Metalava task for Java(non-Kotlin) libraries |
| 41 | + * <li>Configure Dokka with the DAC format and full classpath for symbol resolution |
| 42 | + * <li>Postprocessing of the produced Kotlindoc |
| 43 | + * <ul> |
| 44 | + * <li>Copy the _toc.yaml file to "client/{libName}/_toc.yaml" |
| 45 | + * <li>Filter out unneeded files |
| 46 | + * <li>Copy docs to the buildDir of the root project |
| 47 | + * <li>Remove the "https://firebase.google.com" prefix from all urls |
| 48 | + */ |
| 49 | + static void configure( |
| 50 | + Project project, LibraryExtension android, FirebaseLibraryExtension firebaseLibrary) { |
| 51 | + project.apply(ImmutableMap.of("plugin", "org.jetbrains.dokka-android")); |
| 52 | + |
| 53 | + if (!firebaseLibrary.publishJavadoc) { |
| 54 | + project.getTasks().register("kotlindoc"); |
| 55 | + return; |
| 56 | + } |
| 57 | + DokkaAndroidTask dokkaAndroidTask = |
| 58 | + project |
| 59 | + .getTasks() |
| 60 | + .create( |
| 61 | + "kotlindocDokka", |
| 62 | + DokkaAndroidTask.class, |
| 63 | + dokka -> { |
| 64 | + dokka.setOutputDirectory(project.getBuildDir() + "/dokka/firebase"); |
| 65 | + dokka.setOutputFormat("dac"); |
| 66 | + |
| 67 | + dokka.setGenerateClassIndexPage(false); |
| 68 | + dokka.setGeneratePackageIndexPage(false); |
| 69 | + if (!project.getPluginManager().hasPlugin("kotlin-android")) { |
| 70 | + dokka.dependsOn("docStubs"); |
| 71 | + dokka.setSourceDirs( |
| 72 | + Collections.singletonList( |
| 73 | + project.file(project.getBuildDir() + "/doc-stubs"))); |
| 74 | + } |
| 75 | + |
| 76 | + dokka.setNoAndroidSdkLink(true); |
| 77 | + |
| 78 | + createLink( |
| 79 | + project, |
| 80 | + "https://developers.android.com/reference/kotlin/", |
| 81 | + "kotlindoc/package-lists/android/package-list") |
| 82 | + .map(dokka.getExternalDocumentationLinks()::add); |
| 83 | + createLink( |
| 84 | + project, |
| 85 | + "https://developers.google.com/android/reference/", |
| 86 | + "kotlindoc/package-lists/google/package-list") |
| 87 | + .map(dokka.getExternalDocumentationLinks()::add); |
| 88 | + createLink( |
| 89 | + project, |
| 90 | + "https://firebase.google.com/docs/reference/kotlin/", |
| 91 | + "kotlindoc/package-lists/firebase/package-list") |
| 92 | + .map(dokka.getExternalDocumentationLinks()::add); |
| 93 | + createLink( |
| 94 | + project, |
| 95 | + "https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/", |
| 96 | + "kotlindoc/package-lists/coroutines/package-list") |
| 97 | + .map(dokka.getExternalDocumentationLinks()::add); |
| 98 | + |
| 99 | + android |
| 100 | + .getLibraryVariants() |
| 101 | + .all( |
| 102 | + v -> { |
| 103 | + if (v.getName().equals("release")) { |
| 104 | + project.afterEvaluate( |
| 105 | + p -> { |
| 106 | + FileCollection artifactFiles = |
| 107 | + v.getRuntimeConfiguration() |
| 108 | + .getIncoming() |
| 109 | + .artifactView( |
| 110 | + view -> { |
| 111 | + view.attributes( |
| 112 | + attrs -> |
| 113 | + attrs.attribute( |
| 114 | + Attribute.of( |
| 115 | + "artifactType", String.class), |
| 116 | + "jar")); |
| 117 | + view.componentFilter( |
| 118 | + c -> |
| 119 | + !c.getDisplayName() |
| 120 | + .startsWith( |
| 121 | + "androidx.annotation:annotation:")); |
| 122 | + }) |
| 123 | + .getArtifacts() |
| 124 | + .getArtifactFiles() |
| 125 | + .plus(project.files(android.getBootClasspath())); |
| 126 | + dokka.setClasspath(artifactFiles); |
| 127 | + }); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + project |
| 132 | + .getTasks() |
| 133 | + .create( |
| 134 | + "kotlindoc", |
| 135 | + Copy.class, |
| 136 | + copy -> { |
| 137 | + copy.dependsOn(dokkaAndroidTask); |
| 138 | + copy.setDestinationDir( |
| 139 | + project.file(project.getRootProject().getBuildDir() + "/firebase-kotlindoc")); |
| 140 | + copy.from( |
| 141 | + project.getBuildDir() + "/dokka/firebase", |
| 142 | + cfg -> { |
| 143 | + cfg.exclude("package-list"); |
| 144 | + cfg.filesMatching( |
| 145 | + "_toc.yaml", |
| 146 | + fileCopy -> |
| 147 | + fileCopy.setRelativePath( |
| 148 | + new RelativePath( |
| 149 | + true, |
| 150 | + "client", |
| 151 | + firebaseLibrary.artifactId.get(), |
| 152 | + "_toc.yaml"))); |
| 153 | + cfg.filesMatching( |
| 154 | + "**/*.html", |
| 155 | + fileCopy -> |
| 156 | + fileCopy.filter( |
| 157 | + line -> line.replaceAll("https://firebase.google.com", ""))); |
| 158 | + }); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + private static Optional<DokkaConfiguration.ExternalDocumentationLink> createLink( |
| 163 | + Project project, String url, String packageListPath) { |
| 164 | + |
| 165 | + File packageListFile = project.getRootProject().file(packageListPath); |
| 166 | + if (!packageListFile.exists()) { |
| 167 | + return Optional.empty(); |
| 168 | + } |
| 169 | + try { |
| 170 | + DokkaConfiguration.ExternalDocumentationLink.Builder builder = |
| 171 | + new DokkaConfiguration.ExternalDocumentationLink.Builder(); |
| 172 | + builder.setUrl(new URL(url)); |
| 173 | + builder.setPackageListUrl(packageListFile.toURI().toURL()); |
| 174 | + return Optional.of(builder.build()); |
| 175 | + } catch (MalformedURLException e) { |
| 176 | + throw new GradleException("Could not parse url", e); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments