From 48c769e38e878506be3393202962e67b03931468 Mon Sep 17 00:00:00 2001 From: Richard Harrah Date: Tue, 6 Feb 2024 16:25:35 -0500 Subject: [PATCH] add spigot build tools plugin --- .github/workflows/gradle.yml | 2 +- build.gradle.kts | 10 ++ .../spigot/DownloadSpigotBuildToolsTask.kt | 35 ++++++ .../MythicDropsSpigotBuildToolsExtension.kt | 21 ++++ ...MythicDropsSpigotBuildToolsGradlePlugin.kt | 49 ++++++++ .../gradle/spigot/RunSpigotBuildToolsTask.kt | 110 ++++++++++++++++++ .../gradle/spigot/SyncTaskBuildService.kt | 15 +++ version.properties | 2 +- versions.properties | 2 + 9 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt create mode 100644 src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsExtension.kt create mode 100644 src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsGradlePlugin.kt create mode 100644 src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt create mode 100644 src/main/kotlin/dev/mythicdrops/gradle/spigot/SyncTaskBuildService.kt diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 07169f6..7eff76d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -24,7 +24,7 @@ jobs: with: fetch-depth: '0' # https://github.com/shipkit/shipkit-auto-version#fetch-depth-on-ci - name: Gradle wrapper validation - uses: gradle/wrapper-validation-action@56b90f209b02bf6d1deae490e9ef18b21a389cd4 # v1.1.0 + uses: gradle/wrapper-validation-action@a494d935f4b56874c4a5a87d19af7afcf3a163d0 # v2.0.1 build: runs-on: ubuntu-latest diff --git a/build.gradle.kts b/build.gradle.kts index 4e7a57f..bf5f023 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -89,6 +89,13 @@ gradlePlugin { implementationClass = "dev.mythicdrops.gradle.conventions.MythicDropsMavenPublishPlugin" tags.set(listOf("kotlin", "pixeloutlaw", "convention")) } + create("mythicDropsSpigotBuildTools") { + id = "dev.mythicdrops.gradle.spigot.build" + displayName = "mythicDropsGradleSpigotBuildTools" + description = "Builds and installs versions of Spigot to Maven Local." + implementationClass = "dev.mythicdrops.gradle.spigot.MythicDropsSpigotBuildToolsGradlePlugin" + tags.set(listOf("kotlin", "pixeloutlaw", "convention", "spigot")) + } } } @@ -182,6 +189,9 @@ dependencies { // github api implementation("org.kohsuke:github-api:_") + + // spigot remapper + implementation("io.github.patrick-choe:mojang-spigot-remapper:_") } project.ext.set("gradle.publish.key", System.getenv("GRADLE_PUBLISH_KEY")) diff --git a/src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt b/src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt new file mode 100644 index 0000000..ae34726 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt @@ -0,0 +1,35 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import org.gradle.kotlin.dsl.withGroovyBuilder + +/** + * Task to download Spigot Build Tools to the build directory. + */ +abstract class DownloadSpigotBuildToolsTask : DefaultTask() { + @get:Input + abstract val downloadUrl: Property + + @get:OutputFile + abstract val downloadedJar: RegularFileProperty + + init { + description = "Download Spigot BuildTools.jar into a build directory for use." + group = "spigot" + downloadUrl.convention( + "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar", + ) + } + + @TaskAction + fun download() { + ant.withGroovyBuilder { + "get"("src" to downloadUrl.get(), "dest" to downloadedJar.get().asFile.toPath()) + } + } +} diff --git a/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsExtension.kt b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsExtension.kt new file mode 100644 index 0000000..b73d6bf --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsExtension.kt @@ -0,0 +1,21 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property + +interface MythicDropsSpigotBuildToolsExtension { + /** + * Location of the BuildTools.jar + */ + val buildToolsLocation: RegularFileProperty + + /** + * Should a version with `--remapped` also be run? + */ + val includeRemapped: Property + + /** + * Which version of Spigot to build and publish into [mavenLocal()]. + */ + val version: Property +} diff --git a/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsGradlePlugin.kt b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsGradlePlugin.kt new file mode 100644 index 0000000..9510ca4 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsGradlePlugin.kt @@ -0,0 +1,49 @@ +package dev.mythicdrops.gradle.spigot + +import io.github.patrick.gradle.remapper.RemapTask +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.jvm.tasks.Jar +import org.gradle.kotlin.dsl.create +import org.gradle.kotlin.dsl.named +import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.registerIfAbsent + +class MythicDropsSpigotBuildToolsGradlePlugin : Plugin { + override fun apply(target: Project) { + val extension = + target.extensions.create("spigotBuildTools").apply { + buildToolsLocation.convention( + target.rootProject.layout.buildDirectory.file("spigot-build-tools/BuildTools.jar"), + ) + includeRemapped.convention(true) + version.convention("") + } + + // Register sync task + target.gradle.sharedServices.registerIfAbsent(SyncTaskBuildService.NAME, SyncTaskBuildService::class) { + // throttle the usages of the build tools + maxParallelUsages.set(1) + } + + val downloadTask = + target.tasks.create("downloadSpigotBuildTools") { + downloadedJar.set(extension.buildToolsLocation) + } + target.tasks.register("runSpigotBuildTools") { + buildToolsLocation.set(extension.buildToolsLocation) + includeRemapped.set(extension.includeRemapped) + version.set(extension.version) + dependsOn(downloadTask) + } + + target.plugins.apply("io.github.patrick.remapper") + target.tasks.named("remap") { + version.set(extension.version) + dependsOn("jar") + } + target.tasks.named("jar") { + finalizedBy("remap") + } + } +} diff --git a/src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt b/src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt new file mode 100644 index 0000000..eb66a47 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt @@ -0,0 +1,110 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.DefaultTask +import org.gradle.api.file.FileSystemOperations +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.services.ServiceReference +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.TaskAction +import org.gradle.process.ExecOperations +import java.io.File +import java.nio.file.Paths +import javax.inject.Inject + +abstract class RunSpigotBuildToolsTask + @Inject + constructor( + private val execOperations: ExecOperations, + private val fileSystemOperations: FileSystemOperations, + ) : DefaultTask() { + @get:ServiceReference(SyncTaskBuildService.NAME) + abstract val syncTask: Property + + @get:InputFile + abstract val buildToolsLocation: RegularFileProperty + + @get:Input + abstract val includeRemapped: Property + + @get:Input + abstract val version: Property + + init { + description = "Runs Spigot BuildTools.jar for a specific Minecraft version" + group = "spigot" + } + + @TaskAction + fun runSpigotBuildTools() { + val version = version.getOrElse("") + if (version.isBlank()) { + logger.lifecycle("Not running Spigot build tools as the version is blank") + return + } + + val mavenLocalDirectory = Paths.get(project.repositories.mavenLocal().url).toFile() + if (!mavenLocalDirectory.exists()) { + logger.lifecycle("Creating Maven Local repository at ${mavenLocalDirectory.absolutePath}") + mavenLocalDirectory.mkdirs() + } + + normalVersion(mavenLocalDirectory, version) + if (includeRemapped.getOrElse(false)) { + remappedVersion(mavenLocalDirectory, version) + } + } + + private fun normalVersion( + mavenLocalDirectory: File, + version: String, + ) { + val versionJar = + mavenLocalDirectory.resolve( + "org/spigotmc/spigot/$version-R0.1-SNAPSHOT/spigot-$version-R0.1-SNAPSHOT.jar", + ) + if (versionJar.exists()) { + logger.lifecycle("Skipping $version as Spigot JAR is found at ${versionJar.absolutePath}") + return + } + val jar = buildToolsLocation.get().asFile + val versionDir = jar.parentFile.resolve(version) + fileSystemOperations.copy { + from(jar) + into(versionDir) + } + execOperations.javaexec { + args(listOf("--rev", version)) + workingDir = versionDir.absoluteFile + jvmArgs = listOf("-Xmx1024M") + classpath(buildToolsLocation) + } + } + + private fun remappedVersion( + mavenLocalDirectory: File, + version: String, + ) { + val versionJar = + mavenLocalDirectory.resolve( + "org/spigotmc/spigot/$version-R0.1-SNAPSHOT/spigot-$version-R0.1-SNAPSHOT-remapped-mojang.jar", + ) + if (versionJar.exists()) { + logger.lifecycle("Skipping $version as Spigot remapped JAR is found at ${versionJar.absolutePath}") + return + } + val jar = buildToolsLocation.get().asFile + val versionDir = jar.parentFile.resolve(version) + fileSystemOperations.copy { + from(jar) + into(versionDir) + } + execOperations.javaexec { + args(listOf("--rev", version, "--remapped")) + workingDir = versionDir.absoluteFile + jvmArgs = listOf("-Xmx1024M") + classpath(buildToolsLocation) + } + } + } diff --git a/src/main/kotlin/dev/mythicdrops/gradle/spigot/SyncTaskBuildService.kt b/src/main/kotlin/dev/mythicdrops/gradle/spigot/SyncTaskBuildService.kt new file mode 100644 index 0000000..b11d8c1 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/SyncTaskBuildService.kt @@ -0,0 +1,15 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.services.BuildService +import org.gradle.api.services.BuildServiceParameters + +/** + * Used for preventing multiple expensive tasks from being used simultaneously. + */ +abstract class SyncTaskBuildService : BuildService { + companion object { + const val NAME = "mythicDropsSyncTask" + } + + interface Params : BuildServiceParameters +} diff --git a/version.properties b/version.properties index 6510cb4..c7be73f 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version=7.0.* +version=7.1.* diff --git a/versions.properties b/versions.properties index 3bc1c7b..d7244fb 100644 --- a/versions.properties +++ b/versions.properties @@ -43,3 +43,5 @@ version.io.github.gradle-nexus..publish-plugin=1.3.0 ## # available=2.0.0-rc-1 version.org.kohsuke..github-api=1.318 + +version.io.github.patrick-choe..mojang-spigot-remapper=1.4.0