Skip to content

Commit

Permalink
feat: check for preliminary versions with the update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
MemencioPerez committed Nov 7, 2024
1 parent 3b3b5d7 commit a2364b9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ private static Version getLatestVersion() throws Exception {
}
}

private static boolean isUpdateAvailable(Version latestVersion) {
return Version.parse(Constants.VERSION).isEqualOrLowerThan(latestVersion);
}

public void checkForUpdates() {
try {
Version currentVersion = Version.parse(Constants.VERSION);
Version latestVersion = getLatestVersion();
if (isUpdateAvailable(latestVersion)) {
logger.info("There is an update available for UnSignedVelocity: {} (Your version: {})", latestVersion, Constants.VERSION);
if (currentVersion.isLowerThan(latestVersion)) {
logger.info(miniMessage().deserialize("<#6892bd>There is an update available for <gradient:#166D3B:#7F8C8D:#A29BFE>UnSignedVelocity</gradient><#6892bd>: " + latestVersion + " (Your version: " + currentVersion + ")"));
} else if (currentVersion.isHigherThan(latestVersion)) {
logger.info(miniMessage().deserialize("<#6892bd>You are using a development build of <gradient:#166D3B:#7F8C8D:#A29BFE>UnSignedVelocity</gradient><#6892bd>: " + currentVersion + " (Latest release: " + latestVersion + ")"));
} else {
logger.info(miniMessage().deserialize(
"<#6892bd>You are using the latest version of <gradient:#166D3B:#7F8C8D:#A29BFE>UnSignedVelocity</gradient>"));
logger.info(miniMessage().deserialize("<#6892bd>You are using the latest version of <gradient:#166D3B:#7F8C8D:#A29BFE>UnSignedVelocity</gradient>"));
}
} catch (Exception e) {
logger.error("Cannot check for updates", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package io.github._4drian3d.unsignedvelocity.updatechecker;

public record Version(int major, int minor, int patch) implements Comparable<Version> {
import java.util.Optional;

public record Version(int major, int minor, int patch, Optional<String> preRelease) implements Comparable<Version> {

public static Version parse(String version) {
String[] parts = version.split("\\.");
if (parts.length != 3) {
String[] parts = version.split("[.-]");
if (parts.length != 3 && parts.length != 4) {
throw new IllegalArgumentException("Invalid version format");
}
int major = Integer.parseInt(parts[0]);
int minor = Integer.parseInt(parts[1]);
int patch = Integer.parseInt(parts[2]);
return new Version(major, minor, patch);
Optional<String> preRelease = parts.length == 4 ? Optional.of(parts[3]) : Optional.empty();
return new Version(major, minor, patch, preRelease);
}

public boolean isHigherThan(Version version) {
return this.compareTo(version) > 0;
}

public boolean isEqualOrLowerThan(Version version) {
public boolean isLowerThan(Version version) {
return this.compareTo(version) < 0;
}

Expand All @@ -25,11 +32,50 @@ public int compareTo(Version other) {
if (this.minor != other.minor) {
return Integer.compare(this.minor, other.minor);
}
return Integer.compare(this.patch, other.patch);
if (this.patch != other.patch) {
return Integer.compare(this.patch, other.patch);
}
if (this.preRelease.isPresent() && other.preRelease.isPresent()) {
return comparePreRelease(this.preRelease.get(), other.preRelease.get());
} else if (this.preRelease.isPresent()) {
return -1;
} else if (other.preRelease.isPresent()) {
return 1;
}
return 0;
}

private int comparePreRelease(String preRelease1, String preRelease2) {
String[] parts1 = preRelease1.split("\\.");
String[] parts2 = preRelease2.split("\\.");

for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) {
int comparison = comparePreReleasePart(parts1[i], parts2[i]);
if (comparison != 0) {
return comparison;
}
}

return Integer.compare(parts1.length, parts2.length);
}

private int comparePreReleasePart(String part1, String part2) {
boolean isNumeric1 = part1.matches("\\d+");
boolean isNumeric2 = part2.matches("\\d+");

if (isNumeric1 && isNumeric2) {
return Integer.compare(Integer.parseInt(part1), Integer.parseInt(part2));
} else if (isNumeric1) {
return -1;
} else if (isNumeric2) {
return 1;
} else {
return part1.compareTo(part2);
}
}

@Override
public String toString() {
return major + "." + minor + "." + patch;
return major + "." + minor + "." + patch + preRelease.map(s -> "-" + s).orElse("");
}
}

0 comments on commit a2364b9

Please sign in to comment.