Skip to content

Commit

Permalink
Build: Add dokka generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 committed Jun 25, 2023
1 parent 26f79d6 commit 72fd001
Showing 1 changed file with 111 additions and 4 deletions.
115 changes: 111 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")!!
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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<VersioningPlugin, VersioningConfiguration> {
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()
}

0 comments on commit 72fd001

Please sign in to comment.