Skip to content

Commit

Permalink
Add versioning utils
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Jan 7, 2024
1 parent d4fa3c4 commit c22f817
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Hyperverse - A minecraft world management plugin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//


package se.hyperver.hyperverse.util.versioning;

import javax.annotation.Nullable;
import java.util.Locale;

public enum PreReleaseType {

UNKNOWN("unknown"),

ALPHA("alpha"),
BETA("beta"),
SNAPSHOT("snapshot"),
RELEASE_CANDIDATE("rc");

private static final PreReleaseType[] VALUES = values();

private final String asString;

PreReleaseType(String asString) {
this.asString = asString;
}

@Nullable
public static PreReleaseType parse(String release) {
if (release.isEmpty()) {
return null;
}
String sanitized = release.toLowerCase(Locale.ENGLISH);
for (PreReleaseType preReleaseType : VALUES) {
if (preReleaseType.asString.equals(sanitized)) {
return preReleaseType;
}
}
return UNKNOWN;
}

public String asString() {
return this.asString;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Hyperverse - A minecraft world management plugin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//


package se.hyperver.hyperverse.util.versioning;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public record Version(String original, VersionData versionData) {

/**
* Semver pattern, cg1 = major, cg2 = minor, cg3 = patch, cg4 = prerelease and cg5 = buildmetadata
* Taken from https://semver.org/ and https://regex101.com/r/vkijKf/1/
*/
public static final Pattern SEMVER_PATTERN = Pattern.compile(
"^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");


public static Version parse(String version) {
Matcher matcher = SEMVER_PATTERN.matcher(version);
if (!matcher.find()) {
throw new IllegalArgumentException("Invalid version: " + version);
}
String majorRaw = matcher.group(1);
String minorRaw = matcher.group(2);
String patchRaw = matcher.group(3);
String preReleaseRaw = matcher.group(4);
int major;
int minor;
try {
major = Integer.parseInt(majorRaw);
minor = Integer.parseInt(minorRaw);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Invalid version: " + version);
}
if (patchRaw == null || patchRaw.isEmpty()) {
return new Version(version, new VersionData(major, minor, 0, null));
}
int patch;
try {
patch = Integer.parseInt(patchRaw);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Invalid version: " + version);
}
if (preReleaseRaw == null || preReleaseRaw.isEmpty()) {
return new Version(version, new VersionData(major, minor, patch, null));
}
PreReleaseType releaseType = PreReleaseType.parse(preReleaseRaw);
return new Version(version, new VersionData(major, minor, patch, releaseType));
}

@Override
public String toString() {
return this.original;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Hyperverse - A minecraft world management plugin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//


package se.hyperver.hyperverse.util.versioning;

import org.jetbrains.annotations.NotNull;

public record VersionData(int major, int minor, int patch, PreReleaseType preReleaseType)
implements Comparable<VersionData> {

public VersionData(int major, int minor, int patch) {
this(major, minor, patch, null);
}

public VersionData(int major, int minor) {
this(major, minor, 0);
}

@Override
public String toString() {
return String.format("%d.%d.%d", major, minor, patch);
}

public boolean isNewerThan(VersionData other) {
return compareTo(other) > 0;
}

public boolean isOlderThan(VersionData other) {
return compareTo(other) < 0;
}


@Override
public int compareTo(@NotNull VersionData o) {
int majorComp = Integer.compare(this.major, o.major);
if (majorComp != 0) {
return majorComp;
}
int minorComp = Integer.compare(this.minor, o.minor);
if (minorComp != 0) {
return minorComp;
}
int patchComp = Integer.compare(this.patch, o.patch);
if (patchComp != 0) {
return patchComp;
}
if (this.preReleaseType == null && o.preReleaseType == null) {
return 0;
}
if (this.preReleaseType == null) {
return 1;
}
return this.preReleaseType.compareTo(o.preReleaseType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Hyperverse - A minecraft world management plugin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//


package se.hyperver.hyperverse.util.versioning;

public class VersionUtil {

public static VersionData MC_1_17_1 = new VersionData(1, 17, 1);

public static Version parseMinecraftVersion(String minecraftVersion) {
// Expecting 1.X.X-R0.1-SNAPSHOT
int stripLength = "-R0.1-SNAPSHOT".length();
int length = minecraftVersion.length();
if (length <= stripLength) {
throw new IllegalArgumentException("Invalid minecraft version: " + minecraftVersion);
}
String strippedVersion = minecraftVersion.substring(0, length - stripLength);
try {
return Version.parse(strippedVersion);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Invalid minecraft version: " + minecraftVersion, ex);
}
}

}

0 comments on commit c22f817

Please sign in to comment.