Skip to content

Commit 23f7c33

Browse files
committed
Upgrade to new Semver library
Handle the breaking changes and deprecations in 0.10.x of the semver library.
1 parent 8dbeb0b commit 23f7c33

File tree

8 files changed

+37
-38
lines changed

8 files changed

+37
-38
lines changed

reckon-core/src/main/java/org/ajoberstar/reckon/core/Reckoner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Version reckon() {
6464
throw new IllegalStateException("Reckoned version " + reckoned + " has already been released.");
6565
}
6666

67-
if (inventory.getClaimedVersions().contains(reckoned.getNormal()) && !inventory.getCurrentVersion().filter(reckoned.getNormal()::equals).isPresent() && reckoned.isSignificant()) {
67+
if (inventory.getClaimedVersions().contains(reckoned.getNormal()) && inventory.getCurrentVersion().filter(reckoned.getNormal()::equals).isEmpty() && reckoned.isSignificant()) {
6868
throw new IllegalStateException("Reckoned target normal version " + reckoned.getNormal() + " has already been released.");
6969
}
7070

@@ -94,7 +94,7 @@ private Version reckonNormal(VcsInventory inventory) {
9494
// if a version's already being developed on a parallel branch we'll skip it
9595
if (inventory.getParallelNormals().contains(targetNormal) && probableStage.isPresent()) {
9696
if (scope.compareTo(parallelBranchScope) < 0) {
97-
logger.debug("Skipping {} as it's being developed on a parallel branch. While {} was requested, parallel branches claim a {}, using that instead.", scope, parallelBranchScope);
97+
logger.debug("Skipping {} as it's being developed on a parallel branch. While {} was requested, parallel branches claim a {}, using that instead.", targetNormal, scope, parallelBranchScope);
9898
targetNormal = targetNormal.incrementNormal(parallelBranchScope);
9999
} else {
100100
logger.debug("Skipping {} as it's being developed on a parallel branch. Incrementing again with {}", targetNormal, scope);

reckon-core/src/main/java/org/ajoberstar/reckon/core/Scope.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public static Scope from(String value) {
4646
* @throws IllegalStateException if they have an invalid increment
4747
*/
4848
public static Optional<Scope> infer(Version before, Version after) {
49-
var major = after.getVersion().getMajorVersion() - before.getVersion().getMajorVersion();
50-
var minor = after.getVersion().getMinorVersion() - before.getVersion().getMinorVersion();
51-
var patch = after.getVersion().getPatchVersion() - before.getVersion().getPatchVersion();
52-
if (major == 1 && after.getVersion().getMinorVersion() == 0 && after.getVersion().getPatchVersion() == 0) {
49+
var major = after.getVersion().majorVersion() - before.getVersion().majorVersion();
50+
var minor = after.getVersion().minorVersion() - before.getVersion().minorVersion();
51+
var patch = after.getVersion().patchVersion() - before.getVersion().patchVersion();
52+
if (major == 1 && after.getVersion().minorVersion() == 0 && after.getVersion().patchVersion() == 0) {
5353
return Optional.of(Scope.MAJOR);
54-
} else if (major == 0 && minor == 1 && after.getVersion().getPatchVersion() == 0) {
54+
} else if (major == 0 && minor == 1 && after.getVersion().patchVersion() == 0) {
5555
return Optional.of(Scope.MINOR);
5656
} else if (major == 0 && minor == 0 && patch == 1) {
5757
return Optional.of(Scope.PATCH);

reckon-core/src/main/java/org/ajoberstar/reckon/core/VcsInventory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Optional<Version> getCurrentVersion() {
8080
}
8181

8282
/**
83-
* Number of commits between the current commmit and the base normal version tag.
83+
* Number of commits between the current commit and the base normal version tag.
8484
*/
8585
public int getCommitsSinceBase() {
8686
return commitsSinceBase;
@@ -116,7 +116,7 @@ public Set<Version> getClaimedVersions() {
116116
}
117117

118118
/**
119-
* All commit messages between the current HEAD commit and the base verison's commit.
119+
* All commit messages between the current HEAD commit and the base version's commit.
120120
*/
121121
public List<String> getCommitMessages() {
122122
return commitMessages;

reckon-core/src/main/java/org/ajoberstar/reckon/core/Version.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Objects;
44
import java.util.Optional;
5-
import java.util.regex.Matcher;
65
import java.util.regex.Pattern;
76

87
import com.github.zafarkhaja.semver.ParseException;
@@ -14,7 +13,7 @@ public final class Version implements Comparable<Version> {
1413
/**
1514
* A base version for use as a default in cases where you don't have an existing version.
1615
*/
17-
public static final Version IDENTITY = new Version(com.github.zafarkhaja.semver.Version.forIntegers(0, 0, 0));
16+
public static final Version IDENTITY = new Version(com.github.zafarkhaja.semver.Version.of(0, 0, 0));
1817

1918
private final com.github.zafarkhaja.semver.Version version;
2019
private final Version normal;
@@ -23,10 +22,10 @@ public final class Version implements Comparable<Version> {
2322
private Version(com.github.zafarkhaja.semver.Version version) {
2423
this.version = version;
2524
// need this if logic to avoid stack overflow
26-
if (version.getPreReleaseVersion().isEmpty()) {
25+
if (version.preReleaseVersion().isEmpty() && version.buildMetadata().isEmpty()) {
2726
this.normal = this;
2827
} else {
29-
this.normal = new Version(com.github.zafarkhaja.semver.Version.forIntegers(version.getMajorVersion(), version.getMinorVersion(), version.getPatchVersion()));
28+
this.normal = new Version(com.github.zafarkhaja.semver.Version.of(version.majorVersion(), version.minorVersion(), version.patchVersion()));
3029
}
3130
this.stage = Stage.valueOf(version);
3231
}
@@ -59,17 +58,17 @@ public Optional<Stage> getStage() {
5958
* {@code false} otherwise
6059
*/
6160
public boolean isFinal() {
62-
return version.getPreReleaseVersion().isEmpty();
61+
return version.preReleaseVersion().isEmpty();
6362
}
6463

6564
/**
6665
* @return {@code true} if the version is final or any other significant stage, {@code false} if
67-
* insignficant or snapshot
66+
* insignificant or snapshot
6867
*/
6968
public boolean isSignificant() {
7069
return isFinal() || getStage()
7170
.filter(stage -> !"SNAPSHOT".equals(stage.getName()))
72-
.filter(stage -> version.getBuildMetadata().isEmpty())
71+
.filter(stage -> version.buildMetadata().isEmpty())
7372
.isPresent();
7473
}
7574

@@ -82,11 +81,11 @@ public boolean isSignificant() {
8281
public Version incrementNormal(Scope scope) {
8382
switch (scope) {
8483
case MAJOR:
85-
return new Version(version.incrementMajorVersion());
84+
return new Version(version.nextMajorVersion());
8685
case MINOR:
87-
return new Version(version.incrementMinorVersion());
86+
return new Version(version.nextMinorVersion());
8887
case PATCH:
89-
return new Version(version.incrementPatchVersion());
88+
return new Version(version.nextPatchVersion());
9089
default:
9190
throw new AssertionError("Invalid scope: " + scope);
9291
}
@@ -141,14 +140,16 @@ public int getNum() {
141140
}
142141

143142
private static Stage valueOf(com.github.zafarkhaja.semver.Version version) {
144-
var matcher = STAGE_REGEX.matcher(version.getPreReleaseVersion());
145-
if (matcher.find()) {
146-
var name = matcher.group("name");
147-
int num = Optional.ofNullable(matcher.group("num")).map(Integer::parseInt).orElse(0);
148-
return new Stage(name, num);
149-
} else {
150-
return null;
151-
}
143+
var maybeMatcher = version.preReleaseVersion().map(STAGE_REGEX::matcher);
144+
return maybeMatcher.map(matcher -> {
145+
if (matcher.find()) {
146+
var name = matcher.group("name");
147+
int num = Optional.ofNullable(matcher.group("num")).map(Integer::parseInt).orElse(0);
148+
return new Stage(name, num);
149+
} else {
150+
return null;
151+
}
152+
}).orElse(null);
152153
}
153154
}
154155

@@ -161,7 +162,7 @@ private static Stage valueOf(com.github.zafarkhaja.semver.Version version) {
161162
*/
162163
public static Version valueOf(String versionString) {
163164
try {
164-
return new Version(com.github.zafarkhaja.semver.Version.valueOf(versionString));
165+
return new Version(com.github.zafarkhaja.semver.Version.parse(versionString));
165166
} catch (IllegalArgumentException | ParseException e) {
166167
var message = String.format("Invalid version \"%s\": %s", versionString, e.getMessage());
167168
throw new IllegalArgumentException(message, e);
@@ -177,7 +178,7 @@ public static Version valueOf(String versionString) {
177178
*/
178179
public static Optional<Version> parse(String versionString) {
179180
try {
180-
return Optional.of(new Version(com.github.zafarkhaja.semver.Version.valueOf(versionString)));
181+
return Optional.of(new Version(com.github.zafarkhaja.semver.Version.parse(versionString)));
181182
} catch (IllegalArgumentException | ParseException e) {
182183
return Optional.empty();
183184
}

reckon-core/src/test/java/org/ajoberstar/reckon/core/ReckonerIntegTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
public class ReckonerIntegTest {
2222
private static final Clock CLOCK = Clock.fixed(Instant.ofEpochSecond(1530724706), ZoneId.of("UTC"));
23-
private static final String TIMESTAMP = "20180704T171826Z";
2423

2524
private Path repoDir;
2625
private Git git;

reckon-core/src/test/java/org/ajoberstar/reckon/core/ReckonerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ public void doubleConflictingParallelIncrementsHigherScope() {
360360
.clock(CLOCK)
361361
.vcs(() -> inventory)
362362
.parallelBranchScope(Scope.MINOR)
363-
.scopeCalc(i -> Optional.ofNullable(Scope.PATCH))
364-
.stageCalc((i, v) -> Optional.ofNullable("final"))
363+
.scopeCalc(i -> Optional.of(Scope.PATCH))
364+
.stageCalc((i, v) -> Optional.of("final"))
365365
.defaultInferredScope(Scope.MINOR)
366366
.stages("beta", "milestone", "rc", "final")
367367
.build();

reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonCreateTagTask.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ private void tag(String userEmail, String userName, boolean allowRetry) {
7676
var errorStr = error.toString(StandardCharsets.UTF_8);
7777
if (errorStr.contains(String.format("fatal: tag '%s' already exists", getTagName().get()))) {
7878
setDidWork(false);
79-
} else if (allowRetry && errorStr.contains(String.format("Committer identity unknown"))) {
79+
} else if (allowRetry && errorStr.contains("Committer identity unknown")) {
8080
var email = getRecentUserEmail();
8181
var name = getRecentUserName();
82-
System.err.println(String.format("Tagging as recent committer %s <%s>, as this machine has no git identity set.", name, email));
82+
System.err.printf("Tagging as recent committer %s <%s>, as this machine has no git identity set.%n", name, email);
8383
tag(email, name, false);
8484
} else {
8585
System.err.println(errorStr);
@@ -90,7 +90,7 @@ private void tag(String userEmail, String userName, boolean allowRetry) {
9090

9191
private String getRecentUserEmail() {
9292
var output = new ByteArrayOutputStream();
93-
var result = getExecOperations().exec(spec -> {
93+
getExecOperations().exec(spec -> {
9494
spec.setWorkingDir(getRepoDirectory());
9595
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%ae");
9696
spec.setStandardOutput(output);
@@ -100,7 +100,7 @@ private String getRecentUserEmail() {
100100

101101
private String getRecentUserName() {
102102
var output = new ByteArrayOutputStream();
103-
var result = getExecOperations().exec(spec -> {
103+
getExecOperations().exec(spec -> {
104104
spec.setWorkingDir(getRepoDirectory());
105105
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%an");
106106
spec.setStandardOutput(output);

reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.gradle.api.provider.ProviderFactory;
1818

1919
public class ReckonExtension {
20-
private static Logger logger = Logging.getLogger(ReckonExtension.class);
20+
private static final Logger logger = Logging.getLogger(ReckonExtension.class);
2121

2222
private final DirectoryProperty repoDirectory;
2323
private final Reckoner.Builder reckonerBuilder;
@@ -184,7 +184,6 @@ private Version reckonVersion() {
184184
}
185185

186186
private Repository openRepo() {
187-
Repository repo;
188187
try {
189188
var builder = new FileRepositoryBuilder();
190189
builder.readEnvironment();

0 commit comments

Comments
 (0)