diff --git a/reckon-core/src/main/java/org/ajoberstar/reckon/core/Reckoner.java b/reckon-core/src/main/java/org/ajoberstar/reckon/core/Reckoner.java index ee914b7a..1779f581 100644 --- a/reckon-core/src/main/java/org/ajoberstar/reckon/core/Reckoner.java +++ b/reckon-core/src/main/java/org/ajoberstar/reckon/core/Reckoner.java @@ -1,6 +1,7 @@ package org.ajoberstar.reckon.core; import java.util.Arrays; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; @@ -8,6 +9,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.ajoberstar.reckon.core.git.GitInventorySupplier; +import org.eclipse.jgit.lib.Repository; + public final class Reckoner { public static final String FINAL_STAGE = "final"; public static final String SNAPSHOT_STAGE = "snapshot"; @@ -66,7 +70,7 @@ private Version reckonNormal(VcsInventory inventory) { targetNormal = targetNormal.incrementNormal(scope); } - if (inventory.getClaimedVersions().contains(targetNormal)) { + if (inventory.getClaimedVersions().contains(targetNormal) && !inventory.getCurrentVersion().filter(targetNormal::equals).isPresent()) { throw new IllegalStateException("Reckoned target normal version " + targetNormal + " has already been released."); } @@ -133,6 +137,11 @@ public Builder vcs(VcsInventorySupplier inventorySupplier) { return this; } + public Builder git(Repository repo) { + this.inventorySupplier = new GitInventorySupplier(repo); + return this; + } + public Builder scopeCalc(Function> scopeCalc) { this.scopeCalc = scopeCalc; return this; @@ -160,6 +169,10 @@ public Builder stageCalc(BiFunction> sta } public Reckoner build() { + Objects.requireNonNull(inventorySupplier, "Must provide a vcs."); + Objects.requireNonNull(scopeCalc, "Must provide a scope supplier."); + Objects.requireNonNull(stages, "Must provide set of stages."); + Objects.requireNonNull(stageCalc, "Must provide a stage supplier."); return new Reckoner(inventorySupplier, scopeCalc, stageCalc, stages, defaultStage); } } diff --git a/reckon-core/src/test/groovy/org/ajoberstar/reckon/core/ReckonerTest.groovy b/reckon-core/src/test/groovy/org/ajoberstar/reckon/core/ReckonerTest.groovy index 2c572bf6..a9b220c2 100644 --- a/reckon-core/src/test/groovy/org/ajoberstar/reckon/core/ReckonerTest.groovy +++ b/reckon-core/src/test/groovy/org/ajoberstar/reckon/core/ReckonerTest.groovy @@ -178,7 +178,7 @@ class ReckonerTest extends Specification { def inventory = new VcsInventory( 'abcdef', true, - null, + Version.valueOf('0.1.0'), Version.valueOf('0.0.0'), Version.valueOf('0.0.0'), 1, diff --git a/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy index 3eb5c099..a52e49ed 100644 --- a/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy +++ b/reckon-gradle/src/compatTest/groovy/org/ajoberstar/reckon/gradle/BaseCompatTest.groovy @@ -72,7 +72,7 @@ task printVersion { when: def result = buildAndFail('printVersion') then: - result.output.contains('Must provide strategies for normal and preRelease on the reckon extension.') + result.output.contains('Must provide a scope supplier.') } def 'if reckoned version has build metadata no tag created'() { @@ -86,8 +86,8 @@ plugins { } reckon { - normal = scopeFromProp() - preRelease = stageFromProp('alpha','beta', 'final') + scopeFromProp() + stageFromProp('alpha','beta', 'final') } """ local.add(patterns: ['build.gradle']) @@ -111,8 +111,8 @@ plugins { } reckon { - normal = scopeFromProp() - preRelease = stageFromProp('alpha','beta', 'final') + scopeFromProp() + stageFromProp('alpha','beta', 'final') } """ local.add(patterns: ['build.gradle']) @@ -139,8 +139,8 @@ plugins { } reckon { - normal = scopeFromProp() - preRelease = stageFromProp('alpha','beta', 'final') + scopeFromProp() + stageFromProp('alpha', 'beta', 'final') } """ local.add(patterns: ['build.gradle']) diff --git a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java index 15ba7d80..f74c07f8 100644 --- a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java +++ b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java @@ -1,23 +1,10 @@ package org.ajoberstar.reckon.gradle; -import java.util.Arrays; import java.util.Optional; -import java.util.Set; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.stream.Collectors; import org.ajoberstar.grgit.Grgit; -import org.ajoberstar.reckon.core.NormalStrategy; -import org.ajoberstar.reckon.core.PreReleaseStrategy; import org.ajoberstar.reckon.core.Reckoner; -import org.ajoberstar.reckon.core.VcsInventory; import org.ajoberstar.reckon.core.VcsInventorySupplier; -import org.ajoberstar.reckon.core.Version; -import org.ajoberstar.reckon.core.git.GitInventorySupplier; -import org.ajoberstar.reckon.core.strategy.ScopeNormalStrategy; -import org.ajoberstar.reckon.core.strategy.SnapshotPreReleaseStrategy; -import org.ajoberstar.reckon.core.strategy.StagePreReleaseStrategy; import org.gradle.api.Project; public class ReckonExtension { @@ -27,44 +14,51 @@ public class ReckonExtension { private Project project; private Grgit grgit; - private VcsInventorySupplier vcsInventory; - private NormalStrategy normal; - private PreReleaseStrategy preRelease; + private Reckoner.Builder reckoner; public ReckonExtension(Project project) { this.project = project; + this.reckoner = Reckoner.builder(); } - public void setVcsInventory(VcsInventorySupplier vcsInventory) { - this.vcsInventory = vcsInventory; + @Deprecated + public void setVcsInventory(VcsInventorySupplier inventorySupplier) { + project.getLogger().warn("reckon.vcsInventory = is deprecated and will be removed in 1.0.0. Call reckon.git instead."); + this.reckoner.vcs(inventorySupplier); } - public void setNormal(NormalStrategy normal) { - this.normal = normal; + @Deprecated + public void setNormal(ReckonExtension ext) { + project.getLogger().warn("reckon.normal = scopeFromProp() is deprecated and will be removed in 1.0.0. Call reckon.scopeFromProp() instead."); + // no op } - public void setPreRelease(PreReleaseStrategy preRelease) { - this.preRelease = preRelease; + @Deprecated + public void setPreRelease(ReckonExtension ext) { + project.getLogger().warn("reckon.preRelease = stageFromProp() or snapshotFromProp() is deprecated and will be removed in 1.0.0. Call reckon.stageFromProp() or reckon.snapshotFromProp() instead."); + // no op } - public VcsInventorySupplier git(Grgit grgit) { + public ReckonExtension git(Grgit grgit) { this.grgit = grgit; - return new GitInventorySupplier(grgit.getRepository().getJgit().getRepository()); + this.reckoner.git(grgit.getRepository().getJgit().getRepository()); + return this; } - public NormalStrategy scopeFromProp() { - Function> supplier = inventory -> findProperty(SCOPE_PROP); - return new ScopeNormalStrategy(supplier); + public ReckonExtension scopeFromProp() { + this.reckoner.scopeCalc(inventory -> findProperty(SCOPE_PROP)); + return this; } - public PreReleaseStrategy stageFromProp(String... stages) { - Set stageSet = Arrays.stream(stages).collect(Collectors.toSet()); - BiFunction> supplier = (inventory, targetNormal) -> findProperty(STAGE_PROP); - return new StagePreReleaseStrategy(stageSet, supplier); + public ReckonExtension stageFromProp(String... stages) { + this.reckoner.stages(stages); + this.reckoner.stageCalc((inventory, targetNormal) -> findProperty(STAGE_PROP)); + return this; } - public PreReleaseStrategy snapshotFromProp() { - BiFunction> supplier = (inventory, targetNormal) -> { + public ReckonExtension snapshotFromProp() { + this.reckoner.snapshots(); + this.reckoner.stageCalc((inventory, targetNormal) -> { Optional stageProp = findProperty(STAGE_PROP); Optional snapshotProp = findProperty(SNAPSHOT_PROP) .map(Boolean::parseBoolean) @@ -75,8 +69,8 @@ public PreReleaseStrategy snapshotFromProp() { }); return stageProp.isPresent() ? stageProp : snapshotProp; - }; - return new SnapshotPreReleaseStrategy(supplier); + }); + return this; } private Optional findProperty(String name) { @@ -91,13 +85,11 @@ Grgit getGrgit() { } String reckonVersion() { - if (vcsInventory == null) { + if (grgit == null) { project.getLogger().warn("No VCS found/configured. Version will be 'unspecified'."); return "unspecified"; - } else if (normal == null || preRelease == null) { - throw new IllegalStateException("Must provide strategies for normal and preRelease on the reckon extension."); } else { - String version = Reckoner.reckon(vcsInventory, normal, preRelease); + String version = reckoner.build().reckon().toString(); project.getLogger().warn("Reckoned version: {}", version); return version; } diff --git a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonPlugin.java b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonPlugin.java index 0fce6e55..3bf6fb70 100644 --- a/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonPlugin.java +++ b/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonPlugin.java @@ -25,7 +25,7 @@ public void apply(Project project) { project.getPluginManager().withPlugin("org.ajoberstar.grgit", plugin -> { Grgit grgit = (Grgit) project.findProperty("grgit"); if (grgit != null) { - extension.setVcsInventory(extension.git(grgit)); + extension.git(grgit); } });