Update version (checking) for semver

This commit is contained in:
dordsor21 2021-12-20 21:18:26 +00:00
parent d6e3c331d4
commit e9d97fc7b1
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
3 changed files with 53 additions and 28 deletions

View File

@ -1,5 +1,9 @@
package com.fastasyncworldedit.core; package com.fastasyncworldedit.core;
import com.fastasyncworldedit.core.util.StringMan;
import java.util.Locale;
/** /**
* An internal FAWE class not meant for public use. * An internal FAWE class not meant for public use.
**/ **/
@ -11,28 +15,34 @@ public class FaweVersion {
public final int day; public final int day;
public final int hash; public final int hash;
public final int build; public final int build;
public final int[] semver;
public final boolean snapshot;
public FaweVersion(int year, int month, int day, int hash, int build) { public FaweVersion(int year, int month, int day, int[] semver, boolean snapshot, int hash, int build) {
this.year = year; this.year = year;
this.month = month; this.month = month;
this.day = day; this.day = day;
this.hash = hash; this.hash = hash;
this.build = build; this.build = build;
this.semver = semver;
this.snapshot = snapshot;
} }
public FaweVersion(String version, String commit, String date) { public FaweVersion(String version, String commit, String date) {
String[] split = version.substring(version.indexOf('=') + 1).split("-"); String[] split = version.substring(version.indexOf('=') + 1).split("-");
int build = 0; String[] split1 = split[0].split("\\.");
try { int[] ver = new int[3];
build = Integer.parseInt(split[1]); for (int i = 0; i < 3; i++) {
} catch (NumberFormatException ignored) { ver[i] = Integer.parseInt(split1[i]);
} }
this.build = build; this.semver = ver;
this.snapshot = split.length > 1 && split[1].toLowerCase(Locale.ROOT).contains("snapshot");
this.build = version.contains("+") ? Integer.parseInt(version.substring(version.indexOf('+') + 1)) : 0;
this.hash = Integer.parseInt(commit.substring(commit.indexOf('=') + 1), 16); this.hash = Integer.parseInt(commit.substring(commit.indexOf('=') + 1), 16);
String[] split1 = date.substring(date.indexOf('=') + 1).split("\\."); String[] split2 = date.substring(date.indexOf('=') + 1).split("\\.");
this.year = Integer.parseInt(split1[0]); this.year = Integer.parseInt(split2[0]);
this.month = Integer.parseInt(split1[1]); this.month = Integer.parseInt(split2[1]);
this.day = Integer.parseInt(split1[2]); this.day = Integer.parseInt(split2[2]);
} }
public static FaweVersion tryParse(String version, String commit, String date) { public static FaweVersion tryParse(String version, String commit, String date) {
@ -40,28 +50,42 @@ public class FaweVersion {
return new FaweVersion(version, commit, date); return new FaweVersion(version, commit, date);
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
return new FaweVersion(0, 0, 0, 0, 0); return new FaweVersion(0, 0, 0, null, true, 0, 0);
} }
} }
@Override @Override
public String toString() { public String toString() {
if (hash == 0 && build == 0) { if (semver == null) {
return getSimpleVersionName() + "-NoVer-SNAPSHOT"; return "FastAsyncWorldEdit-NoVer-SNAPSHOT";
} else { } else {
return getSimpleVersionName() + "-" + build; String snapshot = this.snapshot ? "-SNAPSHOT" : "";
String build = this.build > 0 ? "+" + this.build : "";
return "FastAsyncWorldEdit-" + StringMan.join(semver, ".") + snapshot + build;
} }
} }
/** /**
* @return The qualified version name * Returns if another FaweVersion is newer than this one
*/ */
public String getSimpleVersionName() {
return "FastAsyncWorldEdit-1.17";
}
public boolean isNewer(FaweVersion other) { public boolean isNewer(FaweVersion other) {
return other.build < this.build; if (other.semver == null) {
return other.build > this.build;
}
if (this.semver == null) {
return true;
}
if (other.semver[0] != this.semver[0]) {
return other.semver[0] > this.semver[0];
} else if (other.semver[1] != this.semver[1]) {
return other.semver[1] > this.semver[1];
} else if (other.semver[2] != this.semver[2]) {
return other.semver[2] > this.semver[2];
}
if (other.snapshot == this.snapshot) {
return other.build > this.build;
}
return !other.snapshot;
} }
} }

View File

@ -30,8 +30,7 @@ public class UpdateNotification {
try { try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
//TODO 1.18 revisit and update to semver parsing after updating FaweVersion.java Document doc = db.parse(new URL("https://ci.athion.net/job/FastAsyncWorldEdit/api/xml/").openStream());
Document doc = db.parse(new URL("https://ci.athion.net/job/FastAsyncWorldEdit-1.17/api/xml/").openStream());
faweVersion = doc.getElementsByTagName("lastSuccessfulBuild").item(0).getFirstChild().getTextContent(); faweVersion = doc.getElementsByTagName("lastSuccessfulBuild").item(0).getFirstChild().getTextContent();
FaweVersion faweVersion = Fawe.get().getVersion(); FaweVersion faweVersion = Fawe.get().getVersion();
if (faweVersion.build == 0) { if (faweVersion.build == 0) {
@ -45,11 +44,10 @@ public class UpdateNotification {
LOGGER.warn( LOGGER.warn(
""" """
An update for FastAsyncWorldEdit is available. You are {} build(s) out of date. An update for FastAsyncWorldEdit is available. You are {} build(s) out of date.
You are running version {}, the latest version is {}-{}. You are running build {}, the latest version is build {}.
Update at https://www.spigotmc.org/resources/13932/""", Update at https://www.spigotmc.org/resources/13932/""",
versionDifference, versionDifference,
faweVersion.toString(), faweVersion.build,
faweVersion.getSimpleVersionName(),
UpdateNotification.faweVersion UpdateNotification.faweVersion
); );
} }
@ -70,8 +68,11 @@ public class UpdateNotification {
if (actor.hasPermission("fawe.admin") && UpdateNotification.hasUpdate) { if (actor.hasPermission("fawe.admin") && UpdateNotification.hasUpdate) {
FaweVersion faweVersion = Fawe.get().getVersion(); FaweVersion faweVersion = Fawe.get().getVersion();
int versionDifference = Integer.parseInt(UpdateNotification.faweVersion) - faweVersion.build; int versionDifference = Integer.parseInt(UpdateNotification.faweVersion) - faweVersion.build;
actor.print(Caption.of("fawe.info.update-available", versionDifference, faweVersion.toString(), actor.print(Caption.of(
faweVersion.getSimpleVersionName() + "-" + UpdateNotification.faweVersion, "fawe.info.update-available",
versionDifference,
faweVersion.build,
UpdateNotification.faweVersion,
TextComponent TextComponent
.of("https://www.spigotmc.org/resources/13932/") .of("https://www.spigotmc.org/resources/13932/")
.clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/13932/")) .clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/13932/"))

View File

@ -13,7 +13,7 @@
"fawe.info.worldedit.restricted": "Your FAWE edits are now restricted.", "fawe.info.worldedit.restricted": "Your FAWE edits are now restricted.",
"fawe.info.worldedit.oom.admin": "Possible options:\n - //fast\n - Do smaller edits\n - Allocate more memory\n - Disable `max-memory-percent`", "fawe.info.worldedit.oom.admin": "Possible options:\n - //fast\n - Do smaller edits\n - Allocate more memory\n - Disable `max-memory-percent`",
"fawe.info.temporarily-not-working": "Temporarily not working", "fawe.info.temporarily-not-working": "Temporarily not working",
"fawe.info.update-available": "An update for FastAsyncWorldEdit is available. You are {0} build(s) out of date.\nYou are running version {1}, the latest version is {2}.\nUpdate at {3}", "fawe.info.update-available": "An update for FastAsyncWorldEdit is available. You are {0} build(s) out of date.\nYou are running build {1}, the latest version is build {2}.\nUpdate at {3}",
"fawe.web.generating.link": "Uploading {0}, please wait...", "fawe.web.generating.link": "Uploading {0}, please wait...",
"fawe.web.generating.link.failed": "Failed to generate download link!", "fawe.web.generating.link.failed": "Failed to generate download link!",
"fawe.web.download.link": "{0}", "fawe.web.download.link": "{0}",