Skip to content

Commit

Permalink
Use a lazy version object
Browse files Browse the repository at this point in the history
- Change from generating a string up front to generating it upon first request using a lazy object and toString().
- Update Gradle plugin plugin versions
  • Loading branch information
sagebind committed Feb 28, 2019
1 parent af268d5 commit ca5713e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
16 changes: 12 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
id 'groovy'
id 'java'
id 'com.gradle.plugin-publish' version '0.9.10'
id 'com.widen.versioning' version '0.3.1'
id 'java-gradle-plugin'
id 'com.gradle.plugin-publish' version '0.10.1'
id 'com.widen.versioning' version '0.3.2'
}

group = 'com.widen.oss'
Expand All @@ -15,13 +15,21 @@ repositories {
}

dependencies {
compile gradleApi()
compile 'commons-io:commons-io:2.6'

testCompile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
}

gradlePlugin {
plugins {
versioningPlugin {
id = 'com.widen.versioning'
implementationClass = 'com.widen.versioning.VersioningPlugin'
}
}
}

pluginBundle {
website = 'https://github.com/Widen/gradle-versioning'
vcsUrl = 'https://github.com/Widen/gradle-versioning'
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/widen/versioning/VersionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Optional;

public class VersionGenerator {
public static String generateFromGit(Settings settings, File projectDir) {
String describe = gitDescribe(settings, projectDir);
return generateFromString(describe, settings);
public static Optional<String> generateFromGit(Settings settings, File projectDir) {
return gitDescribe(settings, projectDir)
.flatMap(describe -> generateFromString(describe, settings));
}

public static String generateFromString(String describe, Settings settings) {
public static Optional<String> generateFromString(String describe, Settings settings) {
if (describe == null) {
return null;
return Optional.empty();
}

String version = describe.trim().replaceFirst("-(\\d+-g.)", "+$1");
Expand All @@ -23,10 +24,10 @@ public static String generateFromString(String describe, Settings settings) {
version = version.substring(settings.tagPrefix.length());
}

return version;
return Optional.of(version);
}

public static String gitDescribe(Settings settings, File projectDir) {
public static Optional<String> gitDescribe(Settings settings, File projectDir) {
ArrayList<String> args = new ArrayList<>();
args.add("git");
args.add("describe");
Expand Down Expand Up @@ -63,11 +64,11 @@ public static String gitDescribe(Settings settings, File projectDir) {
String output = IOUtils.toString(process.getInputStream(), Charset.forName("UTF-8"));

process.waitFor();
if (process.exitValue() != 0 || output.isEmpty()) {
if (process.exitValue() != 0) {
throw new RuntimeException("Git returned status code " + process.exitValue() + ": " + output);
}

return output;
return Optional.of(output).filter(s -> !s.isEmpty());
}
catch (Exception e) {
throw new RuntimeException(e);
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/com/widen/versioning/VersioningPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,40 @@ public void apply(final Project project) {

Task task = project.getTasks().create("printVersion");
task.setGroup("Versioning");
task.setDescription("Prints the project\'s configured version");
task.setDescription("Prints the project's configured version");
task.doLast(t -> System.out.println(project.getVersion()));

// Defer setting the version until after the build script is evaluated.
project.afterEvaluate(p -> applyVersion(project, settings));
project.setVersion(new LazyVersion(project, settings));
}

private void applyVersion(final Project project, final Settings settings) {
String version = null;
try {
version = VersionGenerator.generateFromGit(settings, project.getRootProject().getProjectDir());
}
catch (Exception e) {
project.getLogger().warn("Error trying to determine project version", e);
private static class LazyVersion {
private final Project project;
private final Settings settings;
private String version;

private LazyVersion(Project project, Settings settings) {
this.project = project;
this.settings = settings;
}

if (version != null) {
project.setVersion(version);
private String generateVersion() {
try {
return VersionGenerator
.generateFromGit(settings, project.getRootProject().getProjectDir())
.orElse(settings.initialVersion);
}
catch (Exception e) {
project.getLogger().warn("Error trying to determine project version", e);
return settings.initialVersion;
}
}
else if ("unspecified".equals(project.getVersion())) {
project.getLogger().debug("No version specified, using initial version");
project.setVersion(settings.initialVersion);

@Override
public String toString() {
if (version == null) {
version = generateVersion();
}
return version;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class VersionGeneratorTest extends Specification {
@Unroll("git describe '#describe' should become '#version'")
def "parse git describe to version"() {
expect:
VersionGenerator.generateFromString(describe, new Settings(tagPrefix: prefix)) == version
VersionGenerator.generateFromString(describe, new Settings(tagPrefix: prefix)).orElse(null) == version

where:
prefix | describe | version
Expand Down

0 comments on commit ca5713e

Please sign in to comment.