Skip to content

Commit

Permalink
Refactor plugin to use new reckoner
Browse files Browse the repository at this point in the history
This includes a partial fix for #78.
  • Loading branch information
ajoberstar committed May 29, 2018
1 parent 312589c commit 1f04fe4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;
import java.util.function.Function;
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";
Expand Down Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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<VcsInventory, Optional<String>> scopeCalc) {
this.scopeCalc = scopeCalc;
return this;
Expand Down Expand Up @@ -160,6 +169,10 @@ public Builder stageCalc(BiFunction<VcsInventory, Version, Optional<String>> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'() {
Expand All @@ -86,8 +86,8 @@ plugins {
}
reckon {
normal = scopeFromProp()
preRelease = stageFromProp('alpha','beta', 'final')
scopeFromProp()
stageFromProp('alpha','beta', 'final')
}
"""
local.add(patterns: ['build.gradle'])
Expand All @@ -111,8 +111,8 @@ plugins {
}
reckon {
normal = scopeFromProp()
preRelease = stageFromProp('alpha','beta', 'final')
scopeFromProp()
stageFromProp('alpha','beta', 'final')
}
"""
local.add(patterns: ['build.gradle'])
Expand All @@ -139,8 +139,8 @@ plugins {
}
reckon {
normal = scopeFromProp()
preRelease = stageFromProp('alpha','beta', 'final')
scopeFromProp()
stageFromProp('alpha', 'beta', 'final')
}
"""
local.add(patterns: ['build.gradle'])
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 = <vcs> 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<VcsInventory, Optional<String>> 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<String> stageSet = Arrays.stream(stages).collect(Collectors.toSet());
BiFunction<VcsInventory, Version, Optional<String>> 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<VcsInventory, Version, Optional<String>> supplier = (inventory, targetNormal) -> {
public ReckonExtension snapshotFromProp() {
this.reckoner.snapshots();
this.reckoner.stageCalc((inventory, targetNormal) -> {
Optional<String> stageProp = findProperty(STAGE_PROP);
Optional<String> snapshotProp = findProperty(SNAPSHOT_PROP)
.map(Boolean::parseBoolean)
Expand All @@ -75,8 +69,8 @@ public PreReleaseStrategy snapshotFromProp() {
});

return stageProp.isPresent() ? stageProp : snapshotProp;
};
return new SnapshotPreReleaseStrategy(supplier);
});
return this;
}

private Optional<String> findProperty(String name) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down

0 comments on commit 1f04fe4

Please sign in to comment.