Skip to content

Commit

Permalink
Only soft fail on config errrors
Browse files Browse the repository at this point in the history
While we don't want to soft-fail at all in the settings plugin, we still
want to provide this as a workaround for misbehaving plugins that might
interfere with your version before it's configured.

However, as #194 made clear, we want to be more careful about which
exceptions we catch. We'd really like to catch cases of failing to build
the reckoner configuration. We do need to inspect the throwable cause
chain though given the layers Gradle adds on top.

Fixes #194
  • Loading branch information
ajoberstar committed Apr 22, 2023
1 parent d3e04b2 commit e573157
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.ajoberstar.reckon.gradle;

public class ReckonConfigurationException extends RuntimeException {
public ReckonConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ReckonExtension {
private static Logger logger = Logging.getLogger(ReckonExtension.class);

private final Reckoner.Builder reckoner;
private final Reckoner.Builder reckonerBuilder;
private final Property<GrgitService> grgitService;
private final Property<String> scope;
private final Property<String> stage;
Expand All @@ -34,7 +34,7 @@ public class ReckonExtension {

@Inject
public ReckonExtension(ObjectFactory objectFactory, ProviderFactory providerFactory) {
this.reckoner = Reckoner.builder();
this.reckonerBuilder = Reckoner.builder();
this.grgitService = objectFactory.property(GrgitService.class);
this.scope = objectFactory.property(String.class);
this.stage = objectFactory.property(String.class);
Expand All @@ -56,27 +56,27 @@ public ReckonExtension(ObjectFactory objectFactory, ProviderFactory providerFact
}

public void setDefaultInferredScope(String scope) {
this.reckoner.defaultInferredScope(Scope.from(scope));
this.reckonerBuilder.defaultInferredScope(Scope.from(scope));
}

public void setParallelBranchScope(String scope) {
this.reckoner.parallelBranchScope(Scope.from(scope));
this.reckonerBuilder.parallelBranchScope(Scope.from(scope));
}

public void stages(String... stages) {
this.reckoner.stages(stages);
this.reckonerBuilder.stages(stages);
}

public void snapshots() {
this.reckoner.snapshots();
this.reckonerBuilder.snapshots();
}

public void setScopeCalc(ScopeCalculator scopeCalc) {
this.reckoner.scopeCalc(scopeCalc);
this.reckonerBuilder.scopeCalc(scopeCalc);
}

public void setStageCalc(StageCalculator stageCalc) {
this.reckoner.stageCalc(stageCalc);
this.reckonerBuilder.stageCalc(stageCalc);
}

public ScopeCalculator calcScopeFromProp() {
Expand All @@ -97,21 +97,21 @@ public StageCalculator calcStageFromProp() {

@Deprecated
public ReckonExtension scopeFromProp() {
this.reckoner.scopeCalc(calcScopeFromProp());
this.reckonerBuilder.scopeCalc(calcScopeFromProp());
return this;
}

@Deprecated
public ReckonExtension stageFromProp(String... stages) {
this.reckoner.stages(stages);
this.reckoner.stageCalc(calcStageFromProp());
this.reckonerBuilder.stages(stages);
this.reckonerBuilder.stageCalc(calcStageFromProp());
return this;
}

@Deprecated
public ReckonExtension snapshotFromProp() {
this.reckoner.snapshots();
this.reckoner.stageCalc(calcStageFromProp());
this.reckonerBuilder.snapshots();
this.reckonerBuilder.stageCalc(calcStageFromProp());
return this;
}

Expand Down Expand Up @@ -159,13 +159,20 @@ private Version reckonVersion() {
try {
var git = grgitService.get().getGrgit();
var repo = git.getRepository().getJgit().getRepository();
reckoner.git(repo, tagParser);
reckonerBuilder.git(repo, tagParser);
} catch (Exception e) {
// no git repo found
reckoner.git(null);
reckonerBuilder.git(null);
}

var version = reckoner.build().reckon();
Reckoner reckoner;
try {
reckoner = reckonerBuilder.build();
} catch (Exception e) {
throw new ReckonConfigurationException("Failed to configure Reckon.", e);
}

var version = reckoner.reckon();
logger.warn("Reckoned version: {}", version);
return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ public String toString() {
try {
return versionProvider.get().toString();
} catch (Exception e) {
var isConfigError = false;
for (Throwable ex = e; ex != null; ex = ex.getCause()) {
if (ex instanceof ReckonConfigurationException) {
isConfigError = true;
}
}

if (!isConfigError) {
throw e;
}

if (warned.compareAndSet(false, true)) {
logger.warn("Project version evaluated before reckon was configured. Run with --info to see cause.");
}
Expand Down

0 comments on commit e573157

Please sign in to comment.