Skip to content

Commit

Permalink
add spigot build tools plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ToppleTheNun committed Feb 6, 2024
1 parent 3a6fcf9 commit be6e33c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>

@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")

Check warning on line 24 in src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt

View workflow job for this annotation

GitHub Actions / qodana

Leaking 'this' in constructor

Accessing non-final property downloadUrl in constructor
downloadedJar.convention(project.layout.buildDirectory.file("spigot-build-tools/BuildTools.jar"))

Check warning on line 25 in src/main/kotlin/dev/mythicdrops/gradle/spigot/DownloadSpigotBuildToolsTask.kt

View workflow job for this annotation

GitHub Actions / qodana

Leaking 'this' in constructor

Accessing non-final property downloadedJar in constructor
}

@TaskAction
fun download() {
ant.withGroovyBuilder {
"get"("src" to downloadUrl, "dest" to downloadedJar.get().asFile.toPath())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean>

/**
* Which versions of Spigot to build and publish into [mavenLocal()].
*/
val versions: ListProperty<String>
}
Original file line number Diff line number Diff line change
@@ -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<Project> {
override fun apply(target: Project) {
target.extensions.create<MythicDropsSpigotBuildToolsExtension>("spigotBuildTools").apply {
isSkipExistingVersions.convention(true)
versions.convention(emptyList())
}

val downloadTask = target.tasks.create<DownloadSpigotBuildToolsTask>("downloadSpigotBuildTools")
target.tasks.create<RunSpigotBuildToolsTask>("runSpigotBuildTools").apply { dependsOn(downloadTask) }
}
}
Original file line number Diff line number Diff line change
@@ -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"))

Check warning on line 23 in src/main/kotlin/dev/mythicdrops/gradle/spigot/RunSpigotBuildToolsTask.kt

View workflow job for this annotation

GitHub Actions / qodana

Leaking 'this' in constructor

Accessing non-final property buildToolsJar in constructor
}

@TaskAction
fun runSpigotBuildTools() {
val buildToolsExtension = project.extensions.getByType<MythicDropsSpigotBuildToolsExtension>()
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")
}
}
}
}

0 comments on commit be6e33c

Please sign in to comment.