-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/PreReleaseType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/Version.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |