Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Now using semantic versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
noahhusby committed Feb 22, 2022
1 parent d1e7e40 commit 4bb5f00
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 41 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ allprojects {
apply plugin: "java"

ext {
sledgehammerVersion = "0.5." + ('git rev-list --count HEAD'.execute().text.trim())
semVersion = "0.5.0-rc.1"
fullVersion = semVersion + "+" + ('git rev-list --count HEAD'.execute().text.trim())

gsonVersion = "2.9.0"
hikariVersion = "4.0.3"
Expand All @@ -46,15 +47,15 @@ allprojects {
}

group "com.noahhusby"
version "$sledgehammerVersion"
version "$fullVersion"

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

processResources {
filter {
it.replace('${project.version}', project.sledgehammerVersion)
it.replace('${project.version}', project.fullVersion)
}
}

Expand Down Expand Up @@ -91,7 +92,7 @@ dependencies {
}

shadowJar {
archiveFileName = "Sledgehammer-${sledgehammerVersion}.jar"
archiveFileName = "Sledgehammer-${fullVersion}.jar"
dependencies {
exclude(dependency("com.google.code.gson:gson"))
exclude(dependency("org.yaml:snakeyaml"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void loadVersion(String version) {
try {
VERSION = new SledgehammerVersion(version);
} catch (VersionParseException ignored2) {
VERSION = new SledgehammerVersion(0, 0, 0, true);
VERSION = SledgehammerVersion.DEV;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import com.noahhusby.sledgehammer.common.exceptions.VersionParseException;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.Objects;

/**
Expand All @@ -35,37 +37,62 @@
public class SledgehammerVersion implements Comparable<SledgehammerVersion> {
private final int majorVersion;
private final int minorVersion;
private final int buildVersion;

private final boolean isDevBuild;

public SledgehammerVersion(int major, int minor, int build) {
this(major, minor, build, false);
}
private final int patchVersion;
private final ReleaseType type;
private final int revision;
private final int build;

public static final SledgehammerVersion DEV = new SledgehammerVersion(0, 0, 0, ReleaseType.DEV, 0, 0);

public SledgehammerVersion(@NonNull String version) throws VersionParseException {
String[] splitBuild = version.split("\\+");
if (splitBuild.length == 1) {
// There is no build present
build = -1;
} else {
try {
build = Integer.parseInt(splitBuild[1]);
} catch (NumberFormatException ignored) {
throw new VersionParseException(String.format("Invalid build number input: %s", version));
}
}

public SledgehammerVersion(String version) throws VersionParseException {
if (version == null) {
majorVersion = minorVersion = buildVersion = 0;
isDevBuild = true;
return;
String[] splitRelease = splitBuild[0].split("-");
if (splitRelease.length == 1) {
// No predefined release type or revision - assumes release
type = ReleaseType.RELEASE;
revision = 0;
} else {
String[] splitRevision = splitRelease[1].split("\\.");
type = ReleaseType.of(splitRevision[0]);
if (splitRevision.length == 2) {
// Revision is present
try {
revision = Integer.parseInt(splitRevision[1]);
} catch (NumberFormatException ignored) {
throw new VersionParseException(String.format("Invalid revision number input: %s", version));
}
} else {
revision = 0;
}
}
String[] versions = version.split("\\.");
if (versions.length < 3) {
throw new VersionParseException(String.format("Invalid version input: %s", version));

String[] splitVersion = splitRelease[0].split("\\.");
if (splitVersion.length < 3) {
throw new VersionParseException(String.format("Invalid target input: %s", version));
}
try {
majorVersion = Integer.parseInt(versions[0]);
minorVersion = Integer.parseInt(versions[1]);
buildVersion = Integer.parseInt(versions[2]);
isDevBuild = false;
majorVersion = Integer.parseInt(splitVersion[0]);
minorVersion = Integer.parseInt(splitVersion[1]);
patchVersion = Integer.parseInt(splitVersion[2]);
} catch (NumberFormatException e) {
throw new VersionParseException(String.format("Invalid version input: %s", version));
throw new VersionParseException(String.format("Invalid target input: %s", version));
}
}

@Override
public int hashCode() {
return Objects.hash(majorVersion, minorVersion, buildVersion);
return Objects.hash(majorVersion, minorVersion, patchVersion, revision, build);
}

@Override
Expand All @@ -82,14 +109,6 @@ public int compareTo(SledgehammerVersion other) {
return Integer.MAX_VALUE;
}

if (this.isDevBuild() && other.isDevBuild()) {
return 0;
} else if (this.isDevBuild) {
return Integer.MAX_VALUE;
} else if (other.isDevBuild) {
return Integer.MIN_VALUE;
}

int majorCompare = this.majorVersion - other.majorVersion;
if (majorCompare != 0) {
return majorCompare;
Expand All @@ -100,18 +119,34 @@ public int compareTo(SledgehammerVersion other) {
return minorCompare;
}

return this.buildVersion - other.buildVersion;
int patchCompare = this.patchVersion - other.patchVersion;
if (patchCompare != 0) {
return patchCompare;
}

int typeCompare = other.type.ordinal() - this.type.ordinal();
if (typeCompare != 0) {
return typeCompare;
}

int revisionCompare = this.revision - other.revision;
if (revisionCompare != 0) {
return revisionCompare;
}

return this.build - other.build;
}

@Override
public String toString() {
String version;
if (isDevBuild) {
version = "[Development Build]";
} else {
version = String.format("%d.%d.%d", majorVersion, minorVersion, buildVersion);
StringBuilder builder = new StringBuilder(String.format("%d.%d.%d", majorVersion, minorVersion, patchVersion));
if (type != ReleaseType.RELEASE) {
builder.append("-").append(type.name).append(".").append(revision);
}
if (build != -1) {
builder.append("+").append(build);
}
return version;
return builder.toString();
}

/**
Expand Down Expand Up @@ -143,4 +178,26 @@ public boolean isOlder(SledgehammerVersion other) {
public boolean isSame(SledgehammerVersion other) {
return this.compareTo(other) == 0;
}

@RequiredArgsConstructor
public enum ReleaseType {
RELEASE(""),

RELEASE_CANDIDATE("rc"),

BETA("beta"),

ALPHA("alpha"),

DEV("dev");

private final String name;

public static ReleaseType of(String name) {
return Arrays.stream(ReleaseType.values())
.filter(t -> t.name.equals(name))
.findFirst()
.orElse(DEV);
}
}
}

0 comments on commit 4bb5f00

Please sign in to comment.