Skip to content

Commit

Permalink
build: semver
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jan 21, 2025
1 parent b2c499f commit 8c29645
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scripts
44 changes: 44 additions & 0 deletions src/main/java/org/auioc/mcmod/arnicalib/base/util/SemVer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2025 AUIOC.ORG
*
* This file is part of ArnicaLib, a mod made for Minecraft.
*
* ArnicaLib 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 <https://www.gnu.org/licenses/>.
*/

package org.auioc.mcmod.arnicalib.base.util;

import javax.annotation.Nullable;

public record SemVer(int major, int minor, int patch, @Nullable String prerelease, @Nullable String build) {

@Override
public String toString() {
return String.format(
"%d.%d.%d%s%s",
major, minor, patch,
(isPrerelease()) ? "-" + prerelease : "",
(build != null && !build.isEmpty()) ? "+" + build : ""
);
}

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

public boolean isPrerelease() {
return prerelease != null && !prerelease.isEmpty();
}

}
20 changes: 13 additions & 7 deletions src/main/java/org/auioc/mcmod/arnicalib/game/util/BuildInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
package org.auioc.mcmod.arnicalib.game.util;

import org.auioc.mcmod.arnicalib.ArnicaLib;
import org.auioc.mcmod.arnicalib.base.util.SemVer;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public record BuildInfo(
SemVer semver,
String version,
String reversion,
int buildNumber,
Expand All @@ -40,16 +42,13 @@ public String shortReversion() {

@Override
public String toString() {
return String.format(
"%s-%s-rev.%s-build.%d%s%s",
minecraftVersion, version, shortReversion(), buildNumber,
isRelease ? "" : "-dev", isDirty ? "-dirty" : ""
);
return semver.toString();
}

public void log(Logger logger, Marker marker) {
logger.info(marker, "Version: " + this.version + " (" + this + ")");
if (!this.isRelease) { logger.warn(marker, "Mod is a development version"); }
logger.info(marker, "Version: " + semver.core() + " (" + this + ")");
if (!isRelease) { logger.warn(marker, "Mod is a development version"); }
if (semver.isPrerelease()) { logger.warn(marker, "Mod is a pre-release version"); }
if (this.isDirty) { logger.warn(marker, "Mod is a dirty build"); }
}

Expand All @@ -61,6 +60,13 @@ public void log(Logger logger, Marker marker) {
public static BuildInfo fromClass(Class<?> clazz) {
try {
var b = new BuildInfo(
new SemVer(
clazz.getField("SEMVER_MAJOR").getInt(null),
clazz.getField("SEMVER_MINOR").getInt(null),
clazz.getField("SEMVER_PATCH").getInt(null),
(String) clazz.getField("SEMVER_PRERELEASE").get(null),
(String) clazz.getField("SEMVER_BUILD").get(null)
),
clazz.getField("VERSION").get(null).toString(),
clazz.getField("REVERSION").get(null).toString(),
clazz.getField("BUILD_NUMBER").getInt(null),
Expand Down

0 comments on commit 8c29645

Please sign in to comment.