diff --git a/build.gradle b/build.gradle index d03ebee..5a4827c 100644 --- a/build.gradle +++ b/build.gradle @@ -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' @@ -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' diff --git a/src/main/java/com/widen/versioning/VersionGenerator.java b/src/main/java/com/widen/versioning/VersionGenerator.java index 7d313c1..323eb9a 100644 --- a/src/main/java/com/widen/versioning/VersionGenerator.java +++ b/src/main/java/com/widen/versioning/VersionGenerator.java @@ -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 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 generateFromString(String describe, Settings settings) { if (describe == null) { - return null; + return Optional.empty(); } String version = describe.trim().replaceFirst("-(\\d+-g.)", "+$1"); @@ -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 gitDescribe(Settings settings, File projectDir) { ArrayList args = new ArrayList<>(); args.add("git"); args.add("describe"); @@ -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); diff --git a/src/main/java/com/widen/versioning/VersioningPlugin.java b/src/main/java/com/widen/versioning/VersioningPlugin.java index 4aab246..74412d9 100644 --- a/src/main/java/com/widen/versioning/VersioningPlugin.java +++ b/src/main/java/com/widen/versioning/VersioningPlugin.java @@ -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; } } } diff --git a/src/main/resources/META-INF/gradle-plugins/com.widen.versioning.properties b/src/main/resources/META-INF/gradle-plugins/com.widen.versioning.properties deleted file mode 100644 index 59f2c69..0000000 --- a/src/main/resources/META-INF/gradle-plugins/com.widen.versioning.properties +++ /dev/null @@ -1 +0,0 @@ -implementation-class=com.widen.versioning.VersioningPlugin diff --git a/src/test/groovy/com/widen/versioning/VersionGeneratorTest.groovy b/src/test/groovy/com/widen/versioning/VersionGeneratorTest.groovy index d81adc1..a73a705 100644 --- a/src/test/groovy/com/widen/versioning/VersionGeneratorTest.groovy +++ b/src/test/groovy/com/widen/versioning/VersionGeneratorTest.groovy @@ -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