From be6e33c7b8037ed950d1fb31f0e68671f55d74e6 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 --- build.gradle.kts | 7 +++ .../spigot/DownloadSpigotBuildToolsTask.kt | 34 ++++++++++++ .../MythicDropsSpigotBuildToolsExtension.kt | 16 ++++++ ...MythicDropsSpigotBuildToolsGradlePlugin.kt | 17 ++++++ .../gradle/spigot/RunSpigotBuildToolsTask.kt | 54 +++++++++++++++++++ 5 files changed, 128 insertions(+) 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 diff --git a/build.gradle.kts b/build.gradle.kts index 4e7a57f..b8bd74d 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")) + } } } 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..bab6f70 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt @@ -0,0 +1,34 @@ +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") + downloadedJar.convention(project.layout.buildDirectory.file("spigot-build-tools/BuildTools.jar")) + } + + @TaskAction + fun download() { + ant.withGroovyBuilder { + "get"("src" to downloadUrl, "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..edcc6e6 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsExtension.kt @@ -0,0 +1,16 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.Property + +interface MythicDropsSpigotBuildToolsExtension { + /** + * Should existing versions not be built? + */ + val isSkipExistingVersions: Property + + /** + * Which versions of Spigot to build and publish into [mavenLocal()]. + */ + val versions: ListProperty +} 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..0c78e8e --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/MythicDropsSpigotBuildToolsGradlePlugin.kt @@ -0,0 +1,17 @@ +package dev.mythicdrops.gradle.spigot + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.create + +class MythicDropsSpigotBuildToolsGradlePlugin : Plugin { + override fun apply(target: Project) { + target.extensions.create("spigotBuildTools").apply { + isSkipExistingVersions.convention(true) + versions.convention(emptyList()) + } + + val downloadTask = target.tasks.create("downloadSpigotBuildTools") + target.tasks.create("runSpigotBuildTools").apply { dependsOn(downloadTask) } + } +} 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..3155340 --- /dev/null +++ b/src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt @@ -0,0 +1,54 @@ +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.tasks.InputFile +import org.gradle.api.tasks.TaskAction +import org.gradle.kotlin.dsl.getByType +import org.gradle.process.ExecOperations +import java.nio.file.Paths +import javax.inject.Inject + +abstract class RunSpigotBuildToolsTask + @Inject + constructor( + private val execOperations: ExecOperations, + private val fileSystemOperations: FileSystemOperations, + ) : DefaultTask() { + @get:InputFile + abstract val buildToolsJar: RegularFileProperty + + init { + buildToolsJar.convention(project.layout.buildDirectory.file("spigot-build-tools/BuildTools.jar")) + } + + @TaskAction + fun runSpigotBuildTools() { + val buildToolsExtension = project.extensions.getByType() + buildToolsExtension.versions.getOrElse(emptyList()).forEach { + val mavenLocalDirectory = Paths.get(project.repositories.mavenLocal().url).toFile() + if (!mavenLocalDirectory.exists()) { + logger.lifecycle("Creating Maven Local repository at ${mavenLocalDirectory.absolutePath}") + mavenLocalDirectory.mkdirs() + } + val versionJar = + mavenLocalDirectory.resolve("org/spigotmc/spigot/$it-R0.1-SNAPSHOT/spigot-$it-R0.1-SNAPSHOT.jar") + if (versionJar.exists()) { + logger.lifecycle("Skipping $it as Spigot JAR is found at ${versionJar.absolutePath}") + return@forEach + } + val jar = buildToolsJar.get().asFile + val versionDir = jar.parentFile.resolve(it) + fileSystemOperations.copy { + from(jar) + into(versionDir) + } + execOperations.javaexec { + args(listOf("-jar", jar, "--rev", it)) + workingDir = versionDir.absoluteFile + jvmArgs = listOf("-Xmx1024M") + } + } + } + }