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)
- [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.

View file

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

View file

@ -22,7 +22,7 @@ dependencies {
kapt {
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";
/**
* 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.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.");
}