Skip to content

Commit

Permalink
patch(plugin): Soft fail if reckon isn't configured
Browse files Browse the repository at this point in the history
Instead of throwing an exception if reckon wasn't configured when
someone called project.getVersion().toString(), return unspecified and
log that it happened. Later calls to project.getVersion().toString()
will correctly reckon the version presuming the extension is configured
at that point.

Calls to reckon.getVersion().get() will continue to throw exceptions,
because that makes use of an explicit Provider instance that users
should properly delay evaluation of until execution time.

Fixes #174
Fixes #147
  • Loading branch information
ajoberstar committed Feb 18, 2022
1 parent 4e711ad commit ad133e8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ task printVersion {
result.output.normalize().startsWith('0.1.0-alpha.0.0+')
}

def 'if no strategies specified, build fails'() {
def 'if no strategies specified, version is unspecified'() {
given:
Grgit.clone(dir: projectDir, uri: remote.repository.rootDir)

Expand All @@ -72,14 +72,43 @@ plugins {
task printVersion {
doLast {
println project.version
println version
}
}
"""
when:
def result = buildAndFail('printVersion', '--configuration-cache')
def result = build('printVersion', '-q', '--configuration-cache')
then:
result.output.contains('unspecified')
}

def 'if version evaluated before reckon configured, reckon can still be evaluated after'() {
given:
Grgit.clone(dir: projectDir, uri: remote.repository.rootDir)

buildFile << """
plugins {
id 'org.ajoberstar.reckon'
}
println version
reckon {
scopeFromProp()
stageFromProp('alpha','beta', 'final')
}
task printVersion {
doLast {
println version
}
}
"""
when:
def result = build('printVersion', '--configuration-cache')
then:
result.output.contains('Must provide a scope supplier.')
result.output.contains('unspecified')
result.output.contains('Reckoned version: 1.1.0-alpha.0')
}

def 'if reckoned version has build metadata no tag created'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import org.ajoberstar.reckon.core.VersionTagWriter;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;

import java.util.concurrent.atomic.AtomicBoolean;

public class ReckonPlugin implements Plugin<Project> {
private static Logger logger = Logging.getLogger(ReckonPlugin.class);

public static final String TAG_TASK = "reckonTagCreate";
public static final String PUSH_TASK = "reckonTagPush";

Expand Down Expand Up @@ -73,14 +79,24 @@ private TaskProvider<ReckonPushTagTask> createPushTask(Project project, ReckonEx

private static class DelayedVersion {
private final Provider<Version> versionProvider;
private final AtomicBoolean warned;

public DelayedVersion(Provider<Version> versionProvider) {
this.versionProvider = versionProvider;
this.warned = new AtomicBoolean(false);
}

@Override
public String toString() {
return versionProvider.get().toString();
try {
return versionProvider.get().toString();
} catch (Exception e) {
if (warned.compareAndSet(false, true)) {
logger.warn("Project version evaluated before reckon was configured. Run with --info to see cause.");
}
logger.info("Project version evaluated before reckon was configured.", e);
return "unspecified";
}
}
}
}

0 comments on commit ad133e8

Please sign in to comment.