Added: ELVersion and ElVersionTask

Updated: GithubUpdater to make use of the new ELVersion class
This commit is contained in:
Bram 2024-07-07 15:27:45 +02:00
parent 6b642bd21f
commit 38a87a6694
No known key found for this signature in database
GPG key ID: 13E608068F40E3CC
16 changed files with 376 additions and 45 deletions

View file

@ -4,22 +4,22 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="Gradle Build Chain from Groovy -&gt; Kotlin DSL">
<change afterPath="$PROJECT_DIR$/buildSrc/build.gradle.kts" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="Added: Build Source Module">
<change afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/utils/ELVersion.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionPlugin.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionTask.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buildSrc/src/main/resources/META-INF/gradle-plugins/el-version.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/api/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code-gen/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/code-gen/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/EntityLib.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/EntityLib.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/common/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/common/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/model-engine-addon/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/model-engine-addon/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/spigot/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/standalone/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/standalone/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/velocity/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/velocity/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -145,7 +145,7 @@
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
&quot;jdk.selected.JAVA_MODULE&quot;: &quot;corretto-17&quot;,
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Development/EntityLib&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Development/EntityLib/buildSrc/src/main/resources&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
@ -165,11 +165,16 @@
}</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Development\EntityLib\buildSrc\src\main\resources" />
<recent name="C:\Development\EntityLib\buildSrc\src\main\kotlin\me\tofaa\entitylib" />
<recent name="C:\Development\EntityLib" />
<recent name="$PROJECT_DIR$/model-engine-addon" />
<recent name="D:\Github\EntityLib\api\src\main\java\me\tofaa\entitylib\extras" />
<recent name="D:\Github\EntityLib\api\src\main\java\me\tofaa\entitylib\meta" />
<recent name="D:\Github\EntityLib\test-plugin" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="C:\Development\EntityLib\buildSrc\src\main\resources\META-INF\gradle-plugins" />
<recent name="C:\Development\EntityLib\buildSrc\src\main\resources" />
<recent name="C:\Development\EntityLib\buildSrc\src\main\resources\META-INF.gradle.plugins" />
</key>
</component>
<component name="RunManager" selected="Gradle.EntityLib [runServer]">
@ -431,8 +436,21 @@
<workItem from="1720349680459" duration="2922000" />
<workItem from="1720352793422" duration="312000" />
<workItem from="1720353517992" duration="622000" />
<workItem from="1720354186649" duration="315000" />
<workItem from="1720354186649" duration="1369000" />
<workItem from="1720355632674" duration="198000" />
<workItem from="1720355846554" duration="602000" />
<workItem from="1720356495720" duration="180000" />
<workItem from="1720357993755" duration="733000" />
</task>
<task id="LOCAL-00001" summary="Added: Build Source Module">
<option name="closed" value="true" />
<created>1720354581808</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1720354581808</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -442,6 +460,10 @@
<option featureType="dependencySupport" implementationName="java:net.kyori:adventure-api" />
<option featureType="dependencySupport" implementationName="java:org.spigotmc:spigot-api" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Added: Build Source Module" />
<option name="LAST_COMMIT_MESSAGE" value="Added: Build Source Module" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>

View file

@ -1,6 +1,7 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
`el-version`
}
dependencies {
@ -9,3 +10,9 @@ dependencies {
compileOnlyApi(libs.bundles.adventure)
compileOnlyApi(libs.packetevents.api)
}
tasks {
generateVersionsFile {
packageName = "me.tofaa.entitylib.utils"
}
}

View file

@ -30,7 +30,7 @@ public final class APIConfig {
@Blocking
public boolean requiresUpdate() throws IOException {
if (!checkForUpdates) return false;
GithubUpdater updater = new GithubUpdater("Tofaa2", "EntityLib", EntityLib.getVersion());
GithubUpdater updater = new GithubUpdater("Tofaa2", "EntityLib");
return !updater.isLatestVersion();
}

View file

@ -1,5 +1,9 @@
package me.tofaa.entitylib;
import com.github.retrooper.packetevents.util.PEVersion;
import me.tofaa.entitylib.utils.ELVersion;
import me.tofaa.entitylib.utils.ELVersions;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
@ -15,6 +19,7 @@ public final class EntityLib {
EntityLib.platform = platform;
platform.setupApi(settings);
api = platform.getAPI();
if (api.getSettings().shouldCheckForUpdate()) {
try {
if (api.getSettings().isDebugMode()) {
@ -45,7 +50,7 @@ public final class EntityLib {
return platform;
}
public static String getVersion() {
return "2.3.1-SNAPSHOT";
public static ELVersion getVersion() {
return ELVersions.CURRENT;
}
}

View file

@ -0,0 +1,187 @@
package me.tofaa.entitylib.utils;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class ELVersion implements Comparable<ELVersion> {
private final int major;
private final int minor;
private final int patch;
private final boolean snapshot;
/**
* Constructs a {@link ELVersion} instance.
*
* @param major the major version number.
* @param minor the minor version number.
* @param patch the patch version number.
* @param snapshot whether the version is a snapshot.
*/
public ELVersion(final int major, final int minor, final int patch, final boolean snapshot) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.snapshot = snapshot;
}
/**
* Constructs a {@link ELVersion} instance with snapshot defaulted to false.
*
* @param major the major version number.
* @param minor the minor version number.
* @param patch the patch version number.
*/
public ELVersion(final int major, final int minor, final int patch) {
this(major, minor, patch, false);
}
/**
* Constructs a {@link ELVersion} instance from a version string.
*
* @param version the version string (e.g., "1.8.9-SNAPSHOT").
* @throws IllegalArgumentException if the version string format is incorrect.
*/
public static ELVersion fromString(@NotNull final String version) {
String versionWithoutSnapshot = version.replace("-SNAPSHOT", "");
String[] parts = versionWithoutSnapshot.split("\\.");
if (parts.length < 2 || parts.length > 3) {
throw new IllegalArgumentException("Version string must be in the format 'major.minor[.patch][-SNAPSHOT]' found '" + version + "' instead.");
}
int major = Integer.parseInt(parts[0]);
int minor = Integer.parseInt(parts[1]);
int patch = parts.length > 2 ? Integer.parseInt(parts[2]) : 0;
boolean snapshot = version.contains("-SNAPSHOT");
return new ELVersion(major, minor, patch, snapshot);
}
/**
* Gets the major version number.
*
* @return the major version number.
*/
public int major() {
return major;
}
/**
* Gets the minor version number.
*
* @return the minor version number.
*/
public int minor() {
return minor;
}
/**
* Gets the patch version number.
*
* @return the patch version number.
*/
public int patch() {
return patch;
}
/**
* Checks if the version is a snapshot.
*
* @return true if snapshot, false otherwise.
*/
public boolean snapshot() {
return snapshot;
}
/**
* Compares this {@link ELVersion} with another {@link ELVersion}.
*
* @param other the other {@link ELVersion}.
* @return a negative integer, zero, or a positive integer as this version is less than,
* equal to, or greater than the specified version.
*/
@Override
public int compareTo(@NotNull final ELVersion other) {
int majorCompare = Integer.compare(this.major, other.major);
if (majorCompare != 0) return majorCompare;
int minorCompare = Integer.compare(this.minor, other.minor);
if (minorCompare != 0) return minorCompare;
int patchCompare = Integer.compare(this.patch, other.patch);
if (patchCompare != 0) return patchCompare;
return Boolean.compare(other.snapshot, this.snapshot);
}
/**
* Checks if the provided object is equal to this {@link ELVersion}.
*
* @param obj the object to compare.
* @return true if the provided object is equal to this {@link ELVersion}, false otherwise.
*/
@Override
public boolean equals(@NotNull final Object obj) {
if (this == obj) return true;
if (!(obj instanceof ELVersion)) return false;
ELVersion other = (ELVersion) obj;
return this.major == other.major &&
this.minor == other.minor &&
this.patch == other.patch &&
this.snapshot == other.snapshot;
}
/**
* Checks if this version is newer than the provided version.
*
* @param otherVersion the other {@link ELVersion}.
* @return true if this version is newer, false otherwise.
*/
public boolean isNewerThan(@NotNull final ELVersion otherVersion) {
return this.compareTo(otherVersion) > 0;
}
/**
* Checks if this version is older than the provided version.
*
* @param otherVersion the other {@link ELVersion}.
* @return true if this version is older, false otherwise.
*/
public boolean isOlderThan(@NotNull final ELVersion otherVersion) {
return this.compareTo(otherVersion) < 0;
}
/**
* Returns a hash code value for this {@link ELVersion}.
*
* @return a hash code value.
*/
@Override
public int hashCode() {
return Objects.hash(major, minor, patch, snapshot);
}
/**
* Creates and returns a copy of this {@link ELVersion}.
*
* @return a clone of this instance.
*/
@Override
public ELVersion clone() {
return new ELVersion(major, minor, patch, snapshot);
}
/**
* Converts the {@link ELVersion} to a string representation.
*
* @return a string representation of the version.
*/
@Override
public String toString() {
return major + "." + minor + "." + patch + (snapshot ? "-SNAPSHOT" : "");
}
}

View file

@ -14,23 +14,22 @@ public final class GithubUpdater {
private final String org;
private final String repo;
private final String currentVersion;
private final ELVersion currentVersion;
public GithubUpdater(String org, String repo, String currentVersion) {
public GithubUpdater(String org, String repo) {
this.org = org;
this.repo = repo;
this.currentVersion = currentVersion;
this.currentVersion = ELVersions.CURRENT;
}
@Blocking
public boolean isLatestVersion() throws IOException {
String latest = getLatestVersion();
return latest != null && latest.equals(currentVersion);
ELVersion latest = getLatestVersion();
return currentVersion.isNewerThan(latest);
}
@Blocking
public String getLatestVersion() throws IOException {
public ELVersion getLatestVersion() throws IOException {
URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
URLConnection connection = url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
@ -38,16 +37,16 @@ public final class GithubUpdater {
BufferedReader reader = new BufferedReader(isr);
String response = reader.readLine();
JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
reader.close();
isr.close();
if (json.has("name")) {
return json.get("name").getAsString();
return ELVersion.fromString(json.get("name").getAsString().replaceFirst("^[vV]", ""));
}
throw new IOException("Could not find name attribute in github api fetch");
}
@Deprecated
public String getCurrentVersion() {
return currentVersion;
return currentVersion.toString();
}
public String getOrg() {

View file

@ -1,6 +1,6 @@
plugins {
java
id("maven-publish")
`maven-publish`
}
group = rootProject.group

View file

@ -0,0 +1,31 @@
package me.tofaa.entitylib.version
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.register
class ELVersionPlugin : Plugin<Project> {
override fun apply(target: Project) {
val task = target.tasks.register<ELVersionTask>(ELVersionTask.TASK_NAME) {
group = target.rootProject.name.toString()
version = target.version.toString()
outputDir = target.layout.buildDirectory.dir("generated/sources/src/java/main")
}
target.afterEvaluate {
val sourceSets = target.extensions.getByName<SourceSetContainer>("sourceSets")
sequenceOf(SourceSet.MAIN_SOURCE_SET_NAME, SourceSet.TEST_SOURCE_SET_NAME).forEach {
sourceSets.getByName(it).java.srcDir(task.flatMap { it.outputDir })
}
task.get().generate()
}
}
}

View file

@ -0,0 +1,78 @@
package me.tofaa.entitylib.version
import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
abstract class ELVersionTask : DefaultTask() {
companion object {
const val TASK_NAME = "generateVersionsFile"
}
@get:Input
abstract var packageName: String
@get:Input
abstract var version: String
@get:OutputDirectory
abstract var outputDir: Provider<Directory>
@TaskAction
fun generate() {
val dir = outputDir.get().dir(packageName.replace('.', '/'))
dir.asFile.mkdirs()
val file = dir.file("ELVersions.java").asFile
if (!file.exists()) {
file.createNewFile()
}
val ver = Version.fromString(version)
logger.info("Generating ELVersions.java with version $ver")
file.writeText("""
/**
* This file is generated by the auto-version task. Modifying it will have no effect.
*/
package $packageName;
public final class ELVersions {
public static final String RAW = "$version";
public static final ELVersion CURRENT = new ELVersion(${ver.major}, ${ver.minor}, ${ver.patch}, ${ver.snapShot});
public static final ELVersion UNKNOWN = new ELVersion(0, 0, 0);
private ELVersions() {
throw new IllegalStateException();
}
}
""".trimIndent())
}
private data class Version(
val major: Int,
val minor: Int,
val patch: Int,
val snapShot: Boolean
) {
companion object {
private val REGEX = Regex("""(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?""")
fun fromString(version: String): Version {
val match = REGEX.matchEntire(version) ?: throw IllegalArgumentException("Invalid version: $version")
return Version(
match.groupValues[1].toInt(),
match.groupValues[2].toInt(),
match.groupValues[3].toInt(),
match.groupValues[4].isNotEmpty()
)
}
}
}
}

View file

@ -0,0 +1 @@
implementation-class=me.tofaa.entitylib.version.ELVersionPlugin

View file

@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
}
dependencies {

View file

@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
}
repositories {

View file

@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
}
repositories {

View file

@ -45,8 +45,9 @@ public class SpigotEntityLibPlatform extends AbstractPlatform<JavaPlugin> {
if (settings.shouldUseBstats()) {
PacketEventsAPI<Plugin> pe = (PacketEventsAPI<Plugin>)api.getPacketEvents();
Metrics metrics = new Metrics((JavaPlugin) pe.getPlugin(), 21916);
metrics.addCustomChart(new Metrics.SimplePie("entitylib-version", EntityLib::getVersion));
metrics.addCustomChart(new Metrics.SimplePie("entitylib-version", () -> EntityLib.getVersion().toString()));
}
}

View file

@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
}
dependencies {

View file

@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
id("java-library")
`java-library`
}
repositories {