diff --git a/README.md b/README.md index c0ac051..2804d53 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,14 @@ This processor was inspired by Cédric Beust's [version-processor](https://githu - [Template](#template) - [Default Template](#default-template) - [Custom Template](#custom-template) -- [Elements & Properties](#elements---properties) -- [Usage with Maven, Gradle, Kotlin and Kobalt](#usage-with-maven--gradle--kotlin-and-kobalt) +- [Elements & Properties](#elements--properties) +- [Usage with Maven, Gradle, Kotlin and Kobalt](#usage-with-maven-gradle-kotlin-and-kobalt) - [Maven](#maven) - [Gradle](#gradle) - [Class Generation](#class-generation) - - [Class & Source Generation](#class---source-generation) + - [Class & Source Generation](#class-source-generation) - [Kotlin](#kotlin) - - [Kotlin & Gradle](#kotlin---gradle) + - [Kotlin & Gradle](#kotlin--gradle) - [Kobalt](#kobalt) - [Auto-Increment](#auto-increment) @@ -253,7 +253,7 @@ dependencies { kapt { arguments { - arg("semver.properties", "$projectDir/version.properties") + arg("semver.project.dir", projectDir) } } ``` @@ -264,7 +264,7 @@ The arguments block is not required if `kapt` is configured to use the Gradle Wo kapt.use.worker.api=true ``` -This option will likely be enabled by default in the future, but is currently not working under Java 10/11 see [KT-26203](https://youtrack.jetbrains.net/issue/KT-26203). +This option will likely be enabled by default in the future, but is currently not working under Java 10+ see [KT-26203](https://youtrack.jetbrains.net/issue/KT-26203). Please look at the [Kotlin example](https://github.com/ethauvin/semver/tree/master/examples/kotlin) project for a [build.gradle.kts](https://github.com/ethauvin/semver/blob/master/examples/kotlin/build.gradle.kts) sample. diff --git a/examples/kotlin/.gitignore b/examples/kotlin/.gitignore index a78c5c2..638960b 100644 --- a/examples/kotlin/.gitignore +++ b/examples/kotlin/.gitignore @@ -62,7 +62,6 @@ dist/ ehthumbs.db fabric.properties gen/ -gradle.properties hs_err_pid* kobaltBuild kobaltw*-test diff --git a/examples/kotlin/build.gradle.kts b/examples/kotlin/build.gradle.kts index 03418b4..5a82d39 100644 --- a/examples/kotlin/build.gradle.kts +++ b/examples/kotlin/build.gradle.kts @@ -22,7 +22,7 @@ dependencies { kapt { arguments { - arg("semver.properties", "$projectDir/version.properties") + arg("semver.project.dir", projectDir) } } diff --git a/src/main/java/net/thauvin/erik/semver/Constants.java b/src/main/java/net/thauvin/erik/semver/Constants.java index 5571ff7..be57649 100644 --- a/src/main/java/net/thauvin/erik/semver/Constants.java +++ b/src/main/java/net/thauvin/erik/semver/Constants.java @@ -139,9 +139,9 @@ public final class Constants { public static final String KOTLIN_TYPE = "kt"; /** - * The argument used to specify the properties file location. + * The argument used to specify the project location. */ - public static final String SEMVER_PROPERTIES_ARG = "semver.properties"; + public static final String SEMVER_PROJECT_DIR_ARG = "semver.project.dir"; /** diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index 4a4a0a7..3816b04 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -35,6 +35,7 @@ package net.thauvin.erik.semver; import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; +import com.github.mustachejava.MustacheNotFoundException; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import javax.annotation.processing.AbstractProcessor; @@ -70,7 +71,7 @@ import java.util.Set; * @created 2016-01-13 * @since 1.0 */ -@SupportedOptions({Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, Constants.SEMVER_PROPERTIES_ARG}) +@SupportedOptions({Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, Constants.SEMVER_PROJECT_DIR_ARG}) public class VersionProcessor extends AbstractProcessor { private Filer filer; @@ -84,23 +85,13 @@ public class VersionProcessor extends AbstractProcessor { log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); } - private String getEnv(String envOption, String defaultValue) { - if (processingEnv != null) { // null when testing. - final String prop = processingEnv.getOptions().get(envOption); - if (prop != null) { - return prop; - } - } - return defaultValue; - } - @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") private VersionInfo findValues(final Version version) throws IOException { final VersionInfo versionInfo = new VersionInfo(version); if (version.properties().length() > 0) { - final File propsFile = new File(getEnv(Constants.SEMVER_PROPERTIES_ARG, version.properties())); + final File propsFile = new File(getEnv(Constants.SEMVER_PROJECT_DIR_ARG, version.properties())); if (propsFile.isFile() && propsFile.canRead()) { note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')'); @@ -147,6 +138,17 @@ public class VersionProcessor extends AbstractProcessor { return versionInfo; } + private String getEnv(String envOption, String fileName) { + if (processingEnv != null) { // null when testing. + final String prop = processingEnv.getOptions().get(envOption); + if (prop != null) { + return prop + File.separator + fileName; + } + } + return fileName; + } + + /** * {@inheritDoc} */ @@ -222,9 +224,10 @@ public class VersionProcessor extends AbstractProcessor { } else { template = version.template(); } + writeTemplate(version.type(), versionInfo, template); - } catch (IOException e) { - error("IOException occurred while running the annotation processor: " + e.getMessage(), e); + } catch (IOException | MustacheNotFoundException e) { + error("An error occurred while running the annotation processor: " + e.getMessage(), e); } } } @@ -241,7 +244,8 @@ public class VersionProcessor extends AbstractProcessor { final VersionInfo versionInfo, final String template) throws IOException { - final MustacheFactory mf = new DefaultMustacheFactory(); + final MustacheFactory mf = new DefaultMustacheFactory( + new File(getEnv(Constants.SEMVER_PROJECT_DIR_ARG, "."))); final Mustache mustache = mf.compile(template); final String templateName; @@ -260,7 +264,7 @@ public class VersionProcessor extends AbstractProcessor { final String fileName = versionInfo.getClassName() + '.' + type; if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { - final String kaptGenDir = getEnv(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, null); + final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); if (kaptGenDir == null) { throw new IOException("Could not find the target directory for generated Kotlin files."); }