|
| 1 | +package buildsrc.conventions |
| 2 | + |
| 3 | +import org.gradle.api.tasks.PathSensitivity.NONE |
| 4 | +import org.gradle.kotlin.dsl.* |
| 5 | + |
| 6 | +plugins { |
| 7 | + id("dev.adamko.dokkatoo-html") |
| 8 | + id("buildsrc.conventions.dokka-source-downloader") |
| 9 | +} |
| 10 | + |
| 11 | +val prepareDokkaTemplates by tasks.registering { |
| 12 | + description = "Prepare Dokka FTL templates" |
| 13 | + // This task is too complicated, but Dokka doesn't provide a way of injecting a single script. |
| 14 | + // I don't want to copy-paste the current Dokka templates into this project, because if Dokka |
| 15 | + // updates the templates then I won't realise they've changed. |
| 16 | + |
| 17 | + val outputDir = layout.buildDirectory.dir("dokkaConfig/templates") |
| 18 | + outputs.dir(outputDir).withPropertyName("outputDir") |
| 19 | + |
| 20 | + outputs.cacheIf("always cache, to avoid downloading Dokka source") { true } |
| 21 | + |
| 22 | + val dokkaSourceDir = tasks.named<Sync>("prepareDokkaSource").map { it.destinationDir } |
| 23 | + |
| 24 | + inputs.dir(dokkaSourceDir).withPropertyName("dokkaSourceDir") |
| 25 | + .withPathSensitivity(NONE) |
| 26 | + |
| 27 | + val cloudflareScript = """ |
| 28 | + <script src="https://static.cloudflareinsights.com/beacon.min.js" data-cf-beacon='{"token": "60b3e3c8134343cc8cb3b0fa0bae9a28"}'></script> |
| 29 | + """.trimIndent() |
| 30 | + inputs.property("cloudflareScript", cloudflareScript) |
| 31 | + |
| 32 | + doLast { |
| 33 | + val pageMetadataFtl = outputDir.get().asFile.resolve("includes/page_metadata.ftl").apply { |
| 34 | + parentFile.mkdirs() |
| 35 | + } |
| 36 | + |
| 37 | + val sourcePageMetadataFtl = dokkaSourceDir.get() |
| 38 | + .resolve("dokka-subprojects/plugin-base/src/main/resources/dokka/templates/includes/page_metadata.ftl") |
| 39 | + |
| 40 | + pageMetadataFtl.writeText( |
| 41 | + "<#-- DO NOT EDIT generated by $path -->\n" + |
| 42 | + sourcePageMetadataFtl.readText() |
| 43 | + .replace( |
| 44 | + """</title>""", |
| 45 | + """</title>${"\n"} $cloudflareScript""", |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + require(cloudflareScript in pageMetadataFtl.readText()) { |
| 50 | + "Cloudflare script missing from $pageMetadataFtl" |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +dokkatoo { |
| 56 | + |
| 57 | + dokkatooSourceSets.configureEach { |
| 58 | + externalDocumentationLinks.register("gradle") { |
| 59 | + // https://docs.gradle.org/current/javadoc/index.html |
| 60 | + url("https://docs.gradle.org/${gradle.gradleVersion}/javadoc/") |
| 61 | + } |
| 62 | + |
| 63 | + sourceLink { |
| 64 | + localDirectory.set(file("src/main/kotlin")) |
| 65 | + val relativeProjectPath = projectDir.relativeToOrNull(rootDir)?.invariantSeparatorsPath ?: "" |
| 66 | + remoteUrl("https://github.com/adamko-dev/dokkatoo/tree/main/$relativeProjectPath/src/main/kotlin") |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pluginsConfiguration { |
| 71 | + html { |
| 72 | + templatesDir.fileProvider(prepareDokkaTemplates.map { it.outputs.files.singleFile }) |
| 73 | + homepageLink = "https://github.com/adamko-dev/dokkatoo/" |
| 74 | + footerMessage = "Copyright © 2023" |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments