From 72fd001214df0585dd00dd050aa99c16d3ca81e5 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sat, 24 Jun 2023 09:19:55 -0700 Subject: [PATCH] Build: Add dokka generation --- build.gradle.kts | 115 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cce15407..b80e802d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,22 @@ +import org.gradle.kotlin.dsl.support.unzipTo +import org.jetbrains.dokka.versioning.VersioningConfiguration +import org.jetbrains.dokka.versioning.VersioningPlugin +import java.net.HttpURLConnection +import java.net.URL +import java.io.ByteArrayOutputStream + +buildscript { + dependencies { + classpath("org.jetbrains.dokka:versioning-plugin:1.8.20") + } +} + plugins { kotlin("jvm") version "1.8.21" kotlin("plugin.serialization") version "1.8.21" id("fabric-loom") version "1.2-SNAPSHOT" id("io.github.juuxel.loom-quiltflower") version "1.8.0" + id("org.jetbrains.dokka") version "1.8.20" } version = property("mod_version")!! @@ -40,6 +54,8 @@ dependencies { include(modImplementation("gg.essential:elementa-1.18.1-fabric:587")!!) modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.1.2") + + dokkaPlugin("org.jetbrains.dokka:versioning-plugin:1.8.20") } loom { @@ -50,6 +66,13 @@ base { archivesName.set(property("archives_base_name") as String) } +java { + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + tasks { processResources { inputs.property("yarn_mappings", yarnMappings) @@ -77,9 +100,93 @@ tasks { } } -java { - withSourcesJar() +tasks.dokkaHtml { + val docVersionsDir = projectDir.resolve("build/javadocs") + val currentVersion = project.version.toString() + val currentDocsDir = docVersionsDir.resolve(currentVersion) + outputs.upToDateWhen { docVersionsDir.exists() } - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + outputDirectory.set(file(currentDocsDir)) + + pluginConfiguration { + version = currentVersion + olderVersionsDir = docVersionsDir + renderVersionsNavigationOnAllPages = true + } + + suppressInheritedMembers.set(true) + + val branch = getBranch() + dokkaSourceSets { + configureEach { + jdkVersion.set(17) + + for (pkg in setOf("engine.langs", "engine.loader", "engine.module", "utils", "listeners", "loader", "launch", "commands", "minecraft.wrappers.objects.threading")) { + perPackageOption { + matchingRegex.set("${"com.chattriggers.ctjs.$pkg".replace(".", "\\.")}(\$|\\.).*") + suppress.set(true) + } + } + + sourceLink { + localDirectory.set(file("src/main/kotlin")) + remoteUrl.set(URL("https://github.com/ChatTriggers/ctjs/blob/$branch/src/main/kotlin")) + remoteLineSuffix.set("#L") + } + } + } + + doFirst { + val archiveBase = "https://www.chattriggers.com/javadocs-archive/" + val versions = String(downloadFile(archiveBase + "versions")).lines().map(String::trim) + val tmpFile = File(temporaryDir, "oldVersionsZip.zip") + + versions.filter(String::isNotEmpty).map(String::trim).forEach { version -> + val zipBytes = downloadFile("$archiveBase$version.zip") + tmpFile.writeBytes(zipBytes) + unzipTo(docVersionsDir, tmpFile) + } + + tmpFile.delete() + } + + doLast { + // At this point we have a structure that looks something like this: + // javadocs + // \-- 2.2.0-1.8.9 + // \-- 3.0.0 + // \-- older + // + // The "older" directory contains all old versions, so we want to + // delete the top-level older versions and move everything inside the + // latest directory to the top level so the GitHub actions workflow + // doesn't need to figure out the correct version name + + docVersionsDir.listFiles()?.forEach { + if (it.name != version) + it.deleteRecursively() + } + + val latestVersionDir = docVersionsDir.listFiles()!!.single() + latestVersionDir.listFiles()!!.forEach { + it.renameTo(File(it.parentFile.parentFile, it.name)) + } + latestVersionDir.deleteRecursively() + } +} + +fun downloadFile(url: String): ByteArray { + return (URL(url).openConnection() as HttpURLConnection).apply { + requestMethod = "GET" + doOutput = true + }.inputStream.readAllBytes() +} + +fun getBranch(): String { + val stdout = ByteArrayOutputStream() + exec { + commandLine("git", "rev-parse", "HEAD") + standardOutput = stdout + } + return stdout.toString().trim() }