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 7, 2024
1 parent 3a6fcf9 commit 48c769e
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 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 Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>

@get:OutputFile
abstract val downloadedJar: RegularFileProperty

init {
description = "Download Spigot BuildTools.jar into a build directory for use."
group = "spigot"
downloadUrl.convention(

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
"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())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean>

/**
* Which version of Spigot to build and publish into [mavenLocal()].
*/
val version: Property<String>
}
Original file line number Diff line number Diff line change
@@ -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<Project> {
override fun apply(target: Project) {
val extension =
target.extensions.create<MythicDropsSpigotBuildToolsExtension>("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<DownloadSpigotBuildToolsTask>("downloadSpigotBuildTools") {
downloadedJar.set(extension.buildToolsLocation)
}
target.tasks.register<RunSpigotBuildToolsTask>("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<RemapTask>("remap") {
version.set(extension.version)
dependsOn("jar")
}
target.tasks.named<Jar>("jar") {
finalizedBy("remap")
}
}
}
Original file line number Diff line number Diff line change
@@ -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)

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

View workflow job for this annotation

GitHub Actions / qodana

Unstable API Usage

'org.gradle.api.services.ServiceReference' is marked unstable with @Incubating
abstract val syncTask: Property<SyncTaskBuildService>

@get:InputFile
abstract val buildToolsLocation: RegularFileProperty

@get:Input
abstract val includeRemapped: Property<Boolean>

@get:Input
abstract val version: Property<String>

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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<SyncTaskBuildService.Params> {
companion object {
const val NAME = "mythicDropsSyncTask"
}

interface Params : BuildServiceParameters
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=7.0.*
version=7.1.*
2 changes: 2 additions & 0 deletions versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 48c769e

Please sign in to comment.