From e356a35aa7dd22ec2b1c7577d9f16938d34e0c6b Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Mon, 6 Nov 2023 13:11:35 +1000 Subject: [PATCH] Improve Gradle scripts Don't create runnable platform jars anymore, since all they really do is increase build time. Also fix texts and Gradle deprecations. Also also publish common with its shadowed configuration, not the plain java components. --- api/build.gradle.kts | 1 - .../viaversion/api/type/types/StringType.java | 4 +- build-logic/src/main/kotlin/extensions.kt | 20 ++++------ .../kotlin/via.base-conventions.gradle.kts | 4 +- .../via.platform-conventions.gradle.kts | 12 ------ build.gradle.kts | 19 +++------- bukkit-legacy/build.gradle.kts | 2 +- bukkit/build.gradle.kts | 2 +- .../bukkit/compat/ProtocolSupportCompat.java | 2 +- bungee/build.gradle.kts | 2 +- common/build.gradle.kts | 6 +++ .../common/protocol/ProtocolVersionTest.java | 7 ++-- .../viaversion/common/type/ItemTypeTest.java | 4 +- .../common/type/StringTypeTest.java | 29 +++----------- fabric/build.gradle.kts | 1 - gradle/libs.versions.toml | 2 - sponge/build.gradle.kts | 2 +- template/build.gradle.kts | 2 +- universal/build.gradle.kts | 38 ++++--------------- velocity/build.gradle.kts | 2 +- 20 files changed, 51 insertions(+), 110 deletions(-) delete mode 100644 build-logic/src/main/kotlin/via.platform-conventions.gradle.kts diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 9f3326bde..73c28fd21 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -1,7 +1,6 @@ plugins { id("net.kyori.blossom") id("org.jetbrains.gradle.plugin.idea-ext") - id("via.shadow-conventions") } sourceSets { diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java index 903512e9c..21db34c6e 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/StringType.java @@ -61,7 +61,9 @@ public class StringType extends Type { @Override public void write(ByteBuf buffer, String object) throws Exception { - Preconditions.checkArgument(object.length() <= maxLength, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length()); + if (object.length() > maxLength) { + throw new IllegalArgumentException("Cannot send string longer than Short.MAX_VALUE characters (got " + object.length() + " characters)"); + } byte[] b = object.getBytes(StandardCharsets.UTF_8); Type.VAR_INT.writePrimitive(buffer, b.length); diff --git a/build-logic/src/main/kotlin/extensions.kt b/build-logic/src/main/kotlin/extensions.kt index 398212697..e018c4abf 100644 --- a/build-logic/src/main/kotlin/extensions.kt +++ b/build-logic/src/main/kotlin/extensions.kt @@ -30,27 +30,21 @@ private fun Project.configurePublication(configurer: MavenPublication.() -> Unit } fun Project.latestCommitHash(): String { - val byteOut = ByteArrayOutputStream() - exec { - commandLine = listOf("git", "rev-parse", "--short", "HEAD") - standardOutput = byteOut - } - return byteOut.toString(Charsets.UTF_8.name()).trim() + return runGitCommand(listOf("rev-parse", "--short", "HEAD")) } fun Project.latestCommitMessage(): String { - val byteOut = ByteArrayOutputStream() - exec { - commandLine = listOf("git", "log", "-1", "--pretty=%B") - standardOutput = byteOut - } - return byteOut.toString(Charsets.UTF_8.name()).trim() + return runGitCommand(listOf("log", "-1", "--pretty=%B")) } fun Project.branchName(): String { + return runGitCommand(listOf("rev-parse", "--abbrev-ref", "HEAD")) +} + +fun Project.runGitCommand(args: List): String { val byteOut = ByteArrayOutputStream() exec { - commandLine = listOf("git", "rev-parse", "--abbrev-ref", "HEAD") + commandLine = listOf("git") + args standardOutput = byteOut } return byteOut.toString(Charsets.UTF_8.name()).trim() diff --git a/build-logic/src/main/kotlin/via.base-conventions.gradle.kts b/build-logic/src/main/kotlin/via.base-conventions.gradle.kts index cbf04f416..fe1f3d7ca 100644 --- a/build-logic/src/main/kotlin/via.base-conventions.gradle.kts +++ b/build-logic/src/main/kotlin/via.base-conventions.gradle.kts @@ -15,10 +15,12 @@ tasks { (options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet") } compileJava { - //options.release.set(8) options.encoding = Charsets.UTF_8.name() options.compilerArgs.addAll(listOf("-nowarn", "-Xlint:-unchecked", "-Xlint:-deprecation")) } + test { + useJUnitPlatform() + } } java { diff --git a/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts b/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts deleted file mode 100644 index b2d256d1e..000000000 --- a/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts +++ /dev/null @@ -1,12 +0,0 @@ -import java.util.* - -plugins { - id("via.shadow-conventions") -} - -tasks { - shadowJar { - archiveFileName.set("ViaVersion-${project.name.substringAfter("viaversion-").replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}-${project.version}.jar") - destinationDirectory.set(rootProject.layout.buildDirectory.dir("libs")) - } -} diff --git a/build.gradle.kts b/build.gradle.kts index 89678df5e..1d82fd121 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,3 @@ -import org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME - plugins { base id("via.build-logic") @@ -11,7 +9,10 @@ allprojects { description = "Allow newer clients to join older server versions." } -val platforms = setOf( +val main = setOf( + projects.viaversion, + projects.viaversionCommon, + projects.viaversionApi, projects.viaversionBukkit, projects.viaversionBungee, projects.viaversionFabric, @@ -20,23 +21,13 @@ val platforms = setOf( ).map { it.dependencyProject } val special = setOf( - projects.viaversion, - projects.viaversionApi, projects.adventure ).map { it.dependencyProject } subprojects { when (this) { - in platforms -> plugins.apply("via.platform-conventions") + in main -> plugins.apply("via.shadow-conventions") in special -> plugins.apply("via.base-conventions") else -> plugins.apply("via.standard-conventions") } - - // Note: If manually starting tests doesn't work for you in IJ, change 'Gradle -> Run Tests Using' to 'IntelliJ IDEA' - dependencies { - // The alternative to this long boi is writing "testImplementation", including the quotes - TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.netty) - TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.guava) - TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.bundles.junit) - } } diff --git a/bukkit-legacy/build.gradle.kts b/bukkit-legacy/build.gradle.kts index c69bdfe8f..40a5ffd5e 100644 --- a/bukkit-legacy/build.gradle.kts +++ b/bukkit-legacy/build.gradle.kts @@ -1,5 +1,5 @@ dependencies { - implementation(projects.viaversionCommon) + compileOnlyApi(projects.viaversionCommon) compileOnly(libs.legacyBukkit) { exclude("junit", "junit") exclude("com.google.code.gson", "gson") diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index abc00209f..b825098ac 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -1,6 +1,6 @@ dependencies { implementation(projects.viaversionBukkitLegacy) - implementation(projects.viaversionCommon) + compileOnlyApi(projects.viaversionCommon) compileOnly(libs.paper) { exclude("junit", "junit") exclude("com.google.code.gson", "gson") diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/compat/ProtocolSupportCompat.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/compat/ProtocolSupportCompat.java index 327203a3c..858234d8d 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/compat/ProtocolSupportCompat.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/compat/ProtocolSupportCompat.java @@ -72,7 +72,7 @@ public final class ProtocolSupportCompat { } catch (NoSuchMethodException ignored) { } - // Check for obfusacted b/c methods + // Check for obfuscated b/c methods try { if (clazz.getMethod("b").getReturnType() == int.class) { return HandshakeProtocolType.OBFUSCATED_B; diff --git a/bungee/build.gradle.kts b/bungee/build.gradle.kts index 24357141d..efacdc9b4 100644 --- a/bungee/build.gradle.kts +++ b/bungee/build.gradle.kts @@ -1,4 +1,4 @@ dependencies { - implementation(projects.viaversionCommon) + compileOnlyApi(projects.viaversionCommon) compileOnly(libs.bungee) } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index d3e8103db..d8411f535 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -3,6 +3,12 @@ dependencies { api(projects.viaversionApiLegacy) implementation(projects.compat.snakeyaml2Compat) implementation(projects.compat.snakeyaml1Compat) + + // Note: If manually starting tests doesn't work for you in IJ, change 'Gradle -> Run Tests Using' to 'IntelliJ IDEA' + testImplementation(rootProject.libs.netty) + testImplementation(rootProject.libs.guava) + testImplementation(rootProject.libs.snakeYaml2) + testImplementation(rootProject.libs.bundles.junit) } java { diff --git a/common/src/test/java/com/viaversion/viaversion/common/protocol/ProtocolVersionTest.java b/common/src/test/java/com/viaversion/viaversion/common/protocol/ProtocolVersionTest.java index e5e60c2e8..d88c1927d 100644 --- a/common/src/test/java/com/viaversion/viaversion/common/protocol/ProtocolVersionTest.java +++ b/common/src/test/java/com/viaversion/viaversion/common/protocol/ProtocolVersionTest.java @@ -32,9 +32,10 @@ public class ProtocolVersionTest { @Test void testVersionRange() { - Assertions.assertEquals(ProtocolVersion.v1_7_1, ProtocolVersion.getClosest("1.7")); - Assertions.assertEquals(ProtocolVersion.v1_7_1, ProtocolVersion.getClosest("1.7.0")); - Assertions.assertEquals(ProtocolVersion.v1_7_1, ProtocolVersion.getClosest("1.7.1")); + Assertions.assertEquals(ProtocolVersion.v1_20, ProtocolVersion.getClosest("1.20")); + Assertions.assertEquals(ProtocolVersion.v1_20, ProtocolVersion.getClosest("1.20.0")); + Assertions.assertEquals(ProtocolVersion.v1_20, ProtocolVersion.getClosest("1.20.1")); + Assertions.assertEquals(ProtocolVersion.v1_7_1, ProtocolVersion.getClosest("1.7.2")); Assertions.assertEquals(ProtocolVersion.v1_7_1, ProtocolVersion.getClosest("1.7.5")); } diff --git a/common/src/test/java/com/viaversion/viaversion/common/type/ItemTypeTest.java b/common/src/test/java/com/viaversion/viaversion/common/type/ItemTypeTest.java index 198b4ac1a..af91eaea9 100644 --- a/common/src/test/java/com/viaversion/viaversion/common/type/ItemTypeTest.java +++ b/common/src/test/java/com/viaversion/viaversion/common/type/ItemTypeTest.java @@ -38,7 +38,7 @@ public class ItemTypeTest { // Test item read Assertions.assertEquals( - new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null), + new DataItem(Short.MAX_VALUE, (byte) -128, (short) 257, null), Type.ITEM1_8.read(Unpooled.wrappedBuffer(new byte[]{ 127, -1, -128, @@ -83,7 +83,7 @@ public class ItemTypeTest { ByteBuf buf = Unpooled.buffer(); // Test item write - Type.ITEM1_8.write(buf, new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null)); + Type.ITEM1_8.write(buf, new DataItem(Short.MAX_VALUE, (byte) -128, (short) 257, null)); Assertions.assertArrayEquals(toBytes(buf), new byte[]{ 127, -1, -128, diff --git a/common/src/test/java/com/viaversion/viaversion/common/type/StringTypeTest.java b/common/src/test/java/com/viaversion/viaversion/common/type/StringTypeTest.java index ed15e83c8..28e3835c1 100644 --- a/common/src/test/java/com/viaversion/viaversion/common/type/StringTypeTest.java +++ b/common/src/test/java/com/viaversion/viaversion/common/type/StringTypeTest.java @@ -23,7 +23,6 @@ import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.function.Executable; public class StringTypeTest { @Test @@ -41,45 +40,29 @@ public class StringTypeTest { Type.STRING.write(buf, new String(new char[Short.MAX_VALUE])); Assertions.assertEquals(Type.STRING.read(buf), new String(new char[Short.MAX_VALUE])); - Type.STRING.write(buf, new String(new char[Short.MAX_VALUE]).replace("\0", "ç")); - Assertions.assertEquals(Type.STRING.read(buf), new String(new char[Short.MAX_VALUE]).replace("\0", "ç")); + Type.STRING.write(buf, new String(new char[Short.MAX_VALUE]).replace("\0", "a")); + Assertions.assertEquals(Type.STRING.read(buf), new String(new char[Short.MAX_VALUE]).replace("\0", "a")); Type.STRING.write(buf, new String(new char[Short.MAX_VALUE / 2]).replace("\0", "\uD83E\uDDFD")); Assertions.assertEquals(Type.STRING.read(buf), new String(new char[Short.MAX_VALUE / 2]).replace("\0", "\uD83E\uDDFD")); } @Test - public void testStringReadOverflowException() throws Exception { + public void testStringReadOverflowException() { // Read exception final ByteBuf buf = Unpooled.buffer(); Type.VAR_INT.writePrimitive(buf, (Short.MAX_VALUE + 1) * 4); for (int i = 0; i < Short.MAX_VALUE / 2 + 1; i++) { buf.writeBytes(new byte[]{0x04, (byte) 0xf0, (byte) 0x9f, (byte) 0xa7, (byte) 0xbd}); // Sponge emoji } - Assertions.assertThrows(IllegalArgumentException.class, new Executable() { - @Override - public void execute() throws Throwable { - Type.STRING.read(buf); - } - }); + Assertions.assertThrows(IllegalArgumentException.class, () -> Type.STRING.read(buf)); } @Test public void testStringWriteOverflowException() { // Write exceptions final ByteBuf buf = Unpooled.buffer(); - Assertions.assertThrows(IllegalArgumentException.class, new Executable() { - @Override - public void execute() throws Throwable { - Type.STRING.write(buf, new String(new char[Short.MAX_VALUE / 2 + 1]).replace("\0", "\uD83E\uDDFD")); - } - }); - - Assertions.assertThrows(IllegalArgumentException.class, new Executable() { - @Override - public void execute() throws Throwable { - Type.STRING.write(buf, new String(new char[Short.MAX_VALUE + 1])); - } - }); + Assertions.assertThrows(IllegalArgumentException.class, () -> Type.STRING.write(buf, new String(new char[Short.MAX_VALUE / 2 + 1]).replace("\0", "\uD83E\uDDFD"))); + Assertions.assertThrows(IllegalArgumentException.class, () -> Type.STRING.write(buf, new String(new char[Short.MAX_VALUE + 1]))); } } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 0dd230f83..7d82dc72f 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -1,3 +1,2 @@ dependencies { - implementation(projects.viaversionCommon) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e7f1c874a..9fa10bc62 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,7 +22,6 @@ paper = "1.16.5-R0.1-SNAPSHOT" legacyBukkit = "1.8.8-R0.1-SNAPSHOT" bungee = "1.19-R0.1-SNAPSHOT" sponge = "8.0.0" -legacySponge = "4.0.0" velocity = "3.1.1" @@ -52,7 +51,6 @@ paper = { group = "com.destroystokyo.paper", name = "paper-api", version.ref = " legacyBukkit = { group = "org.bukkit", name = "bukkit", version.ref = "legacyBukkit" } bungee = { group = "net.md-5", name = "bungeecord-api", version.ref = "bungee" } sponge = { group = "org.spongepowered", name = "spongeapi", version.ref = "sponge" } -legacySponge = { group = "org.spongepowered", name = "spongeapi", version.ref = "legacySponge" } velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity" } diff --git a/sponge/build.gradle.kts b/sponge/build.gradle.kts index 4846d6c7e..d79f36062 100644 --- a/sponge/build.gradle.kts +++ b/sponge/build.gradle.kts @@ -1,4 +1,4 @@ dependencies { - implementation(projects.viaversionCommon) + compileOnlyApi(projects.viaversionCommon) compileOnly(libs.sponge) } diff --git a/template/build.gradle.kts b/template/build.gradle.kts index 0dd230f83..67a762997 100644 --- a/template/build.gradle.kts +++ b/template/build.gradle.kts @@ -1,3 +1,3 @@ dependencies { - implementation(projects.viaversionCommon) + compileOnly(projects.viaversionCommon) } diff --git a/universal/build.gradle.kts b/universal/build.gradle.kts index 482b8b971..c2f683a1c 100644 --- a/universal/build.gradle.kts +++ b/universal/build.gradle.kts @@ -1,48 +1,26 @@ -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import io.papermc.hangarpublishplugin.model.Platforms plugins { - id("com.github.johnrengelman.shadow") id("io.papermc.hangar-publish-plugin") version "0.1.0" id("com.modrinth.minotaur") version "2.+" } -val platforms = setOf( - rootProject.projects.viaversionBukkit, - rootProject.projects.viaversionBungee, - rootProject.projects.viaversionFabric, - rootProject.projects.viaversionSponge, - rootProject.projects.viaversionVelocity -).map { it.dependencyProject } +dependencies { + api(projects.viaversionCommon) + api(projects.viaversionBukkit) + api(projects.viaversionBungee) + api(projects.viaversionFabric) + api(projects.viaversionSponge) + api(projects.viaversionVelocity) +} tasks { shadowJar { - archiveClassifier.set("") archiveFileName.set("ViaVersion-${project.version}.jar") destinationDirectory.set(rootProject.projectDir.resolve("build/libs")) - duplicatesStrategy = DuplicatesStrategy.EXCLUDE - platforms.forEach { platform -> - val shadowJarTask = platform.tasks.named("shadowJar").forUseAtConfigurationTime().get() - dependsOn(shadowJarTask) - dependsOn(platform.tasks.withType()) - from(zipTree(shadowJarTask.archiveFile)) - } - } - build { - dependsOn(shadowJar) - } - sourcesJar { - rootProject.subprojects.forEach { subproject -> - if (subproject == project) return@forEach - val platformSourcesJarTask = subproject.tasks.findByName("sourcesJar") as? Jar ?: return@forEach - dependsOn(platformSourcesJarTask) - from(zipTree(platformSourcesJarTask.archiveFile)) - } } } -publishShadowJar() - val branch = rootProject.branchName() val baseVersion = project.version as String val isRelease = !baseVersion.contains('-') diff --git a/velocity/build.gradle.kts b/velocity/build.gradle.kts index 5a41f16f7..b45b1e92e 100644 --- a/velocity/build.gradle.kts +++ b/velocity/build.gradle.kts @@ -1,5 +1,5 @@ dependencies { - implementation(projects.viaversionCommon) + compileOnlyApi(projects.viaversionCommon) compileOnly(libs.velocity) { // Requires Java 11 exclude("com.velocitypowered", "velocity-brigadier")