Create canonical version information

This commit is contained in:
crschnick 2024-03-02 11:24:54 +00:00
parent 6df1f70a19
commit 7117b4f6f2
2 changed files with 54 additions and 0 deletions

View file

@ -38,6 +38,7 @@ public class AppProperties {
boolean debugThreads;
Path dataDir;
boolean showcase;
AppVersion canonicalVersion;
public AppProperties() {
image = ModuleHelper.isImage();
@ -63,6 +64,7 @@ public class AppProperties {
showcase = Optional.ofNullable(System.getProperty("io.xpipe.app.showcase"))
.map(Boolean::parseBoolean)
.orElse(false);
canonicalVersion = AppVersion.parse(version).orElse(null);
}
public static void logSystemProperties() {
@ -117,4 +119,8 @@ public class AppProperties {
return AppPrefs.get().developerMode().getValue();
}
public Optional<AppVersion> getCanonicalVersion() {
return Optional.ofNullable(canonicalVersion);
}
}

View file

@ -0,0 +1,48 @@
package io.xpipe.app.core;
import lombok.NonNull;
import lombok.Value;
import java.util.Optional;
@Value
public class AppVersion implements Comparable<AppVersion> {
public static Optional<AppVersion> parse(String version) {
try {
var releaseSplit = version.split("-");
var split = releaseSplit[0].split("\\.");
var major = Integer.parseInt(split[0]);
var minor = split.length > 1 ? Integer.parseInt(split[1]) : 0;
var patch = split.length > 2 ? Integer.parseInt(split[2]) : 0;
return Optional.of(new AppVersion(major, minor, patch));
} catch (Exception ex) {
// This can happen on number format exceptions
// It shouldn't happen if the version is correctly formatted though
return Optional.empty();
}
}
int major;
int minor;
int patch;
public boolean greaterThan(AppVersion other) {
return compareTo(other) > 0;
}
@Override
public int compareTo(@NonNull AppVersion o) {
var majorCompare = major - o.major;
if (majorCompare != 0) {
return majorCompare;
}
var minorCompare = minor - o.minor;
if (minorCompare != 0) {
return minorCompare;
}
return patch - o.patch;
}
}