From 51680ab0ca80240d7d900b143a759c800e709283 Mon Sep 17 00:00:00 2001 From: Artur Khusainov Date: Thu, 11 Jan 2024 01:26:44 +0300 Subject: [PATCH] Remove Spigradle (#73) --- buildSrc/build.gradle.kts | 5 ++ buildSrc/src/main/kotlin/PluginDescriptor.kt | 76 ++++++++++++++++++++ bungee/build.gradle.kts | 26 ++++--- gradle/libs.versions.toml | 4 +- spigot/build.gradle.kts | 39 +++++----- velocity/build.gradle.kts | 19 +++-- 6 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 buildSrc/src/main/kotlin/PluginDescriptor.kt diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 0828bd4..9769335 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -10,4 +10,9 @@ dependencies { // See https://github.com/gradle/gradle/issues/15383 implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) implementation(with(libs.plugins.shadow.get()) { "com.github.johnrengelman:shadow:$version" }) + with(libs.versions.jackson) { + val version = get() + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$version") + implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$version") + } } diff --git a/buildSrc/src/main/kotlin/PluginDescriptor.kt b/buildSrc/src/main/kotlin/PluginDescriptor.kt new file mode 100644 index 0000000..5277b24 --- /dev/null +++ b/buildSrc/src/main/kotlin/PluginDescriptor.kt @@ -0,0 +1,76 @@ +import com.fasterxml.jackson.core.JsonFactory +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.MapProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* +import java.io.File + +enum class PluginDescriptorFormat { + YAML, JSON +} + +abstract class PluginDescriptorTask : DefaultTask() { + + @get:Input + @get:Optional + abstract val descriptor: Property + + @get:InputFile + @get:Optional + abstract val descriptorFile: RegularFileProperty + + @get:Input + abstract val content: MapProperty + + @get:Input + @get:Optional + abstract val format: Property + + @get:Input + @get:Optional + abstract val append: Property + + init { + project.tasks.getByName("processResources") { + finalizedBy(this@PluginDescriptorTask) + } + } + + @TaskAction + fun writeFile() { + val mapper = constructMapper() + mapper.registerKotlinModule() + val file = extractDescriptorFile() + val fileContent = LinkedHashMap() + if (append.getOrElse(false)) { + fileContent.putAll(mapper.readValue>(file)) + } + fileContent.putAll(content.get()) + mapper.writeValue( + file, + fileContent, + ) + } + + private fun constructMapper(): ObjectMapper { + @Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA") + return ObjectMapper(when(format.getOrElse(PluginDescriptorFormat.YAML)) { + PluginDescriptorFormat.JSON -> JsonFactory() + PluginDescriptorFormat.YAML -> YAMLFactory() + }) + } + + private fun extractDescriptorFile(): File { + var file: File? = descriptorFile.map { it.asFile }.orNull + if (file == null) { + val sourceSets: SourceSetContainer = project.extensions.getByName("sourceSets") as SourceSetContainer + file = sourceSets.getByName("main").output.resourcesDir!!.resolve(descriptor.get()) + } + return file + } +} \ No newline at end of file diff --git a/bungee/build.gradle.kts b/bungee/build.gradle.kts index 44434e4..27c2c4b 100644 --- a/bungee/build.gradle.kts +++ b/bungee/build.gradle.kts @@ -1,20 +1,28 @@ -import kr.entree.spigradle.kotlin.bungeecord - plugins { id("java-shadow") - alias(libs.plugins.spigradle.bungee) } -bungee { - name = "MapModCompanion" - author = "turikhay" - debug { - jvmArgs = listOf("-Xmx256m", "-Dnet.md_5.bungee.console-log-level=ALL") +repositories { + maven { + name = "Sonatype" + url = uri("https://oss.sonatype.org/content/repositories/snapshots/") + } +} + +tasks { + val writeBungeeYml by creating(PluginDescriptorTask::class) { + descriptor = "bungee.yml" + content.putAll(mapOf( + "name" to "MapModCompanion", + "version" to project.version, + "author" to "turikhay", + "main" to "com.turikhay.mc.mapmodcompanion.bungee.MapModCompanion" + )) } } dependencies { implementation(project(":common")) implementation(libs.bstats.bungeecord) - compileOnly(bungeecord()) + compileOnly("net.md-5:bungeecord-api:1.19-R0.1-SNAPSHOT") } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 708e33b..3a4514b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,10 +1,10 @@ [versions] -spigradle = "2.4.3" spotbugs = "4.8.3" shadow = "8.1.1" junit = "5.10.1" bstats = "3.0.2" velocity-api = "3.1.1" +jackson = "2.16.1" [libraries] spotbugs-annotations = { module = "com.github.spotbugs:spotbugs-annotations", version.ref = "spotbugs" } @@ -17,6 +17,4 @@ velocity-api = { module = "com.velocitypowered:velocity-api", version.ref = "vel [plugins] -spigradle = { id = "kr.entree.spigradle", version.ref = "spigradle" } -spigradle-bungee = { id = "kr.entree.spigradle.bungee", version.ref = "spigradle" } shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index faebd7a..c7c30fc 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -1,35 +1,32 @@ -import kr.entree.spigradle.kotlin.spigot - plugins { id("java-shadow") - alias(libs.plugins.spigradle) } repositories { maven { url = uri("https://repo.dmulloy2.net/repository/public/") } + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots/") + } + maven { + url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") + } } val bStats = with(libs.bstats.bukkit.get()) { "$module:$versionConstraint" } -spigot { - name = "MapModCompanion" - authors = listOf("turikhay") - apiVersion = "1.13" - softDepends = listOf("ProtocolLib") - debug { - jvmArgs = listOf( - "-Xmx2048m", - "-Dlog4j.configurationFile=${projectDir}${File.separatorChar}log4j2-debug.xml", - "-Dcom.turikhay.mc.mapmodcompanion.spigot.debug=true" - ) - } - afterEvaluate { - excludeLibraries = listOf( - rootProject.allprojects.map { "${it.group}:${it.name}:${it.version}" }, - listOf(bStats) - ).flatten() +tasks { + val writePluginYml by creating(PluginDescriptorTask::class) { + descriptor = "plugin.yml" + content.putAll(mapOf( + "name" to "MapModCompanion", + "version" to project.version, + "authors" to listOf("turikhay"), + "apiVersion" to "1.13", + "softDepends" to listOf("ProtocolLib"), + "main" to "com.turikhay.mc.mapmodcompanion.spigot.MapModCompanion" + )) } } @@ -42,6 +39,6 @@ dependencies { implementation(libs.bstats.bukkit) // These dependencies are intentionally not present in libs.version.toml - compileOnly(spigot(spigot_version)) + compileOnly("org.spigotmc:spigot-api:${spigot_version}-R0.1-SNAPSHOT") compileOnly("com.comphenix.protocol:ProtocolLib:${protocolLib_version}") } diff --git a/velocity/build.gradle.kts b/velocity/build.gradle.kts index 7286c02..063fda5 100644 --- a/velocity/build.gradle.kts +++ b/velocity/build.gradle.kts @@ -1,6 +1,3 @@ -import groovy.json.JsonSlurper -import groovy.json.JsonOutput - plugins { id("java-shadow") } @@ -26,14 +23,14 @@ dependencies { } tasks { - classes { - doLast { - val pluginFile = project.layout.buildDirectory.file("classes/java/main/velocity-plugin.json").get().getAsFile() - @Suppress("UNCHECKED_CAST") - val pluginDescriptor = JsonSlurper().parse(pluginFile) as MutableMap - pluginDescriptor["version"] = version - pluginFile.writeText(JsonOutput.toJson(pluginDescriptor)) - } + val rewriteVelocityPluginJson by creating(PluginDescriptorTask::class) { + dependsOn(compileJava) + descriptorFile = project.layout.buildDirectory.file("classes/java/main/velocity-plugin.json") + format = PluginDescriptorFormat.JSON + append = true + content.putAll(mapOf( + "version" to project.version + )) } shadowJar {