|
| 1 | +/* |
| 2 | + * Copyright (C) 2021-2024 Rick Busarow |
| 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 | + |
| 16 | +package modulecheck.builds |
| 17 | + |
| 18 | +import com.rickbusarow.kgx.dependsOn |
| 19 | +import com.rickbusarow.kgx.isRootProject |
| 20 | +import com.rickbusarow.kgx.projectDependency |
| 21 | +import com.rickbusarow.ktlint.KtLintTask |
| 22 | +import com.vanniktech.maven.publish.tasks.JavadocJar |
| 23 | +import dev.adamko.dokkatoo.DokkatooExtension |
| 24 | +import dev.adamko.dokkatoo.dokka.plugins.DokkaVersioningPluginParameters |
| 25 | +import dev.adamko.dokkatoo.tasks.DokkatooGenerateTask |
| 26 | +import org.gradle.api.GradleException |
| 27 | +import org.gradle.api.Plugin |
| 28 | +import org.gradle.api.Project |
| 29 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin |
| 30 | +import org.gradle.language.base.plugins.LifecycleBasePlugin |
| 31 | +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 32 | +import java.net.URI |
| 33 | + |
| 34 | +abstract class DokkatooConventionPlugin : Plugin<Project> { |
| 35 | + override fun apply(target: Project) { |
| 36 | + |
| 37 | + target.pluginManager.apply(dev.adamko.dokkatoo.DokkatooPlugin::class.java) |
| 38 | + |
| 39 | + target.extensions.configure(DokkatooExtension::class.java) { dokkatoo -> |
| 40 | + |
| 41 | + dokkatoo.versions.jetbrainsDokka.set(target.libs.versions.dokka) |
| 42 | + |
| 43 | + dokkatoo.moduleVersion.set(target.VERSION_NAME_STABLE) |
| 44 | + val fullModuleName = target.path.removePrefix(":") |
| 45 | + dokkatoo.moduleName.set(fullModuleName) |
| 46 | + |
| 47 | + dokkatoo.dokkatooSourceSets.configureEach { sourceSet -> |
| 48 | + sourceSet.documentedVisibilities( |
| 49 | + dev.adamko.dokkatoo.dokka.parameters.VisibilityModifier.PRIVATE, |
| 50 | + dev.adamko.dokkatoo.dokka.parameters.VisibilityModifier.INTERNAL, |
| 51 | + dev.adamko.dokkatoo.dokka.parameters.VisibilityModifier.PROTECTED, |
| 52 | + dev.adamko.dokkatoo.dokka.parameters.VisibilityModifier.PACKAGE, |
| 53 | + dev.adamko.dokkatoo.dokka.parameters.VisibilityModifier.PUBLIC |
| 54 | + ) |
| 55 | + |
| 56 | + sourceSet.languageVersion.set(target.KOTLIN_API) |
| 57 | + sourceSet.jdkVersion.set(target.JVM_TARGET_INT) |
| 58 | + |
| 59 | + // include all project sources when resolving kdoc samples |
| 60 | + sourceSet.samples.setFrom(target.fileTree(target.file("src"))) |
| 61 | + |
| 62 | + if (!target.isRootProject()) { |
| 63 | + val readmeFile = target.projectDir.resolve("README.md") |
| 64 | + if (readmeFile.exists()) { |
| 65 | + sourceSet.includes.from(readmeFile) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + sourceSet.sourceLink { sourceLinkBuilder -> |
| 70 | + sourceLinkBuilder.localDirectory.set(target.file("src/main")) |
| 71 | + |
| 72 | + val modulePath = target.path.replace(":", "/") |
| 73 | + .replaceFirst("/", "") |
| 74 | + |
| 75 | + // URL showing where the source code can be accessed through the web browser |
| 76 | + sourceLinkBuilder.remoteUrl.set( |
| 77 | + URI("${SOURCE_WEBSITE}/blob/main/$modulePath/src/main") |
| 78 | + ) |
| 79 | + // Suffix which is used to append the line number to the URL. Use #L for GitHub |
| 80 | + sourceLinkBuilder.remoteLineSuffix.set("#L") |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + target.tasks.withType(DokkatooGenerateTask::class.java).configureEach { task -> |
| 85 | + |
| 86 | + task.workerMinHeapSize.set("512m") |
| 87 | + task.workerMaxHeapSize.set("1g") |
| 88 | + |
| 89 | + if (target.isRootProject()) { |
| 90 | + task.dependsOn("unzipDokkaArchives") |
| 91 | + } |
| 92 | + |
| 93 | + // Dokka uses their outputs but doesn't explicitly depend upon them. |
| 94 | + task.mustRunAfter(target.tasks.withType(KotlinCompile::class.java)) |
| 95 | + task.mustRunAfter(target.tasks.withType(KtLintTask::class.java)) |
| 96 | + } |
| 97 | + |
| 98 | + if (target.isRootProject()) { |
| 99 | + |
| 100 | + val config = target.configurations.getByName("dokkatoo") |
| 101 | + |
| 102 | + config.dependencies.addAllLater( |
| 103 | + target.provider { |
| 104 | + target.subprojects |
| 105 | + .filter { sub -> sub.subprojects.isEmpty() } |
| 106 | + .map { sub -> target.projectDependency(sub.path) } |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + val pluginConfig = "dokkatooPluginHtml" |
| 111 | + |
| 112 | + target.dependencies.add(pluginConfig, target.libs.dokka.all.modules) |
| 113 | + target.dependencies.add(pluginConfig, target.libs.dokka.versioning) |
| 114 | + |
| 115 | + val dokkaArchiveBuildDir = target.rootProject.layout |
| 116 | + .buildDirectory |
| 117 | + .dir("tmp/dokka-archive") |
| 118 | + |
| 119 | + dokkatoo.pluginsConfiguration |
| 120 | + .withType(DokkaVersioningPluginParameters::class.java).configureEach { versioning -> |
| 121 | + versioning.version.set(target.VERSION_NAME) |
| 122 | + versioning.olderVersionsDir.set(dokkaArchiveBuildDir) |
| 123 | + versioning.renderVersionsNavigationOnAllPages.set(true) |
| 124 | + } |
| 125 | + |
| 126 | + dokkatoo.dokkatooPublications.configureEach { |
| 127 | + it.suppressObviousFunctions.set(true) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Make dummy tasks with the original Dokka plugin names, then delegate them to the Dokkatoo tasks |
| 133 | + target.tasks.register("dokkaHtml").dependsOn(DOKKATOO_HTML_TASK_NAME) |
| 134 | + target.tasks.register("dokkaHtmlMultiModule") |
| 135 | + .dependsOn(target.rootProject.tasks.named(DOKKATOO_HTML_TASK_NAME)) |
| 136 | + |
| 137 | + target.plugins.withType(MavenPublishPlugin::class.java).configureEach { |
| 138 | + |
| 139 | + val checkJavadocJarIsNotVersioned = target.tasks |
| 140 | + .register("checkJavadocJarIsNotVersioned") { task -> |
| 141 | + |
| 142 | + task.description = |
| 143 | + "Ensures that generated javadoc.jar artifacts don't include old Dokka versions" |
| 144 | + task.group = "dokka versioning" |
| 145 | + |
| 146 | + val javadocTasks = target.tasks.withType(JavadocJar::class.java) |
| 147 | + task.dependsOn(javadocTasks) |
| 148 | + |
| 149 | + task.inputs.files(javadocTasks.map { it.outputs }) |
| 150 | + |
| 151 | + val zipTrees = javadocTasks.map { target.zipTree(it.archiveFile) } |
| 152 | + |
| 153 | + task.doLast { |
| 154 | + |
| 155 | + val jsonReg = """older/($SEMVER_REGEX)/version\.json""".toRegex() |
| 156 | + |
| 157 | + val versions = zipTrees.flatMap { tree -> |
| 158 | + tree |
| 159 | + .filter { it.path.startsWith("older/") } |
| 160 | + .filter { it.isFile } |
| 161 | + .mapNotNull { jsonReg.find(it.path)?.groupValues?.get(1) } |
| 162 | + } |
| 163 | + |
| 164 | + if (versions.isNotEmpty()) { |
| 165 | + throw GradleException("Found old Dokka versions in javadoc.jar: $versions") |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + target.tasks.named(LifecycleBasePlugin.CHECK_TASK_NAME).dependsOn( |
| 171 | + checkJavadocJarIsNotVersioned |
| 172 | + ) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + companion object { |
| 177 | + internal const val DOKKATOO_HTML_TASK_NAME = "dokkatooGeneratePublicationHtml" |
| 178 | + } |
| 179 | +} |
0 commit comments