Added semver.project.dir processor argument. Closes #2

This commit is contained in:
Erik C. Thauvin 2019-04-19 12:04:06 -07:00
parent ed35e70af4
commit b83a17e9c8
5 changed files with 29 additions and 26 deletions

View file

@ -12,14 +12,14 @@ This processor was inspired by Cédric Beust's [version-processor](https://githu
- [Template](#template) - [Template](#template)
- [Default Template](#default-template) - [Default Template](#default-template)
- [Custom Template](#custom-template) - [Custom Template](#custom-template)
- [Elements & Properties](#elements---properties) - [Elements & Properties](#elements--properties)
- [Usage with Maven, Gradle, Kotlin and Kobalt](#usage-with-maven--gradle--kotlin-and-kobalt) - [Usage with Maven, Gradle, Kotlin and Kobalt](#usage-with-maven-gradle-kotlin-and-kobalt)
- [Maven](#maven) - [Maven](#maven)
- [Gradle](#gradle) - [Gradle](#gradle)
- [Class Generation](#class-generation) - [Class Generation](#class-generation)
- [Class & Source Generation](#class---source-generation) - [Class & Source Generation](#class-source-generation)
- [Kotlin](#kotlin) - [Kotlin](#kotlin)
- [Kotlin & Gradle](#kotlin---gradle) - [Kotlin & Gradle](#kotlin--gradle)
- [Kobalt](#kobalt) - [Kobalt](#kobalt)
- [Auto-Increment](#auto-increment) - [Auto-Increment](#auto-increment)
@ -253,7 +253,7 @@ dependencies {
kapt { kapt {
arguments { 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 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. 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.

View file

@ -62,7 +62,6 @@ dist/
ehthumbs.db ehthumbs.db
fabric.properties fabric.properties
gen/ gen/
gradle.properties
hs_err_pid* hs_err_pid*
kobaltBuild kobaltBuild
kobaltw*-test kobaltw*-test

View file

@ -22,7 +22,7 @@ dependencies {
kapt { kapt {
arguments { arguments {
arg("semver.properties", "$projectDir/version.properties") arg("semver.project.dir", projectDir)
} }
} }

View file

@ -139,9 +139,9 @@ public final class Constants {
public static final String KOTLIN_TYPE = "kt"; 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";
/** /**

View file

@ -35,6 +35,7 @@ package net.thauvin.erik.semver;
import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache; import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory; import com.github.mustachejava.MustacheFactory;
import com.github.mustachejava.MustacheNotFoundException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
@ -70,7 +71,7 @@ import java.util.Set;
* @created 2016-01-13 * @created 2016-01-13
* @since 1.0 * @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 { public class VersionProcessor extends AbstractProcessor {
private Filer filer; private Filer filer;
@ -84,23 +85,13 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); 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") @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
private VersionInfo findValues(final Version version) private VersionInfo findValues(final Version version)
throws IOException { throws IOException {
final VersionInfo versionInfo = new VersionInfo(version); final VersionInfo versionInfo = new VersionInfo(version);
if (version.properties().length() > 0) { 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()) { if (propsFile.isFile() && propsFile.canRead()) {
note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')'); note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')');
@ -147,6 +138,17 @@ public class VersionProcessor extends AbstractProcessor {
return versionInfo; 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} * {@inheritDoc}
*/ */
@ -222,9 +224,10 @@ public class VersionProcessor extends AbstractProcessor {
} else { } else {
template = version.template(); template = version.template();
} }
writeTemplate(version.type(), versionInfo, template); writeTemplate(version.type(), versionInfo, template);
} catch (IOException e) { } catch (IOException | MustacheNotFoundException e) {
error("IOException occurred while running the annotation processor: " + e.getMessage(), 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 VersionInfo versionInfo,
final String template) final String template)
throws IOException { 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 Mustache mustache = mf.compile(template);
final String templateName; final String templateName;
@ -260,7 +264,7 @@ public class VersionProcessor extends AbstractProcessor {
final String fileName = versionInfo.getClassName() + '.' + type; final String fileName = versionInfo.getClassName() + '.' + type;
if (type.equalsIgnoreCase(Constants.KOTLIN_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) { if (kaptGenDir == null) {
throw new IOException("Could not find the target directory for generated Kotlin files."); throw new IOException("Could not find the target directory for generated Kotlin files.");
} }