diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 7e0a3c7..7c26c8e 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index abca604..722b42e 100644 --- a/.idea/libraries/bld.xml +++ b/.idea/libraries/bld.xml @@ -2,11 +2,11 @@ - + - + diff --git a/.vscode/settings.json b/.vscode/settings.json index c83239c..b676049 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.bld/dist/bld-1.7.1.jar", + "${HOME}bld-1.7.2.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/README.md b/README.md index 5d26292..c13ec3d 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,10 @@ This processor was inspired by Cédric Beust's [version-processor](https://githu ```java import net.thauvin.erik.semver.Version; -@Version(major = 1, minor = 0, patch = 0, preRelease = "beta") +@Version(major = 2, minor = 1, patch = 1, preRelease = "beta") public class A { -// ... + // ... +} ``` * Or using a [properties](hhttps://github.com/ethauvin/semver/blob/master/examples/java/version.properties) file: @@ -50,7 +51,8 @@ import net.thauvin.erik.semver.Version; @Version(properties = "version.properties") public class A { -// ... + // ... +} ``` ```ini @@ -74,26 +76,27 @@ To specify your own template name, use: ```java @Version(template = "version.mustache") public class A { -// ... + // ... +} ``` ### Default Template The [default template](https://github.com/ethauvin/semver/blob/master/src/main/resources/semver.mustache) implements the following static variables: -Field | Description | Example -:------------------|:---------------------------------|:----------------- -`PROJECT` | The project name, if any. | `MyProject` -`BUILDDATE` | The build date. | [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) -`VERSION` | The full version string. | `1.2.3-alpha+001` -`MAJOR` | The major version. | `1` -`MINOR` | The minor version. | `2` -`PATCH` | The patch version. | `3` -`PRERELEASE` | The pre-release version, if any. | `alpha` -`PRERELASE_PREFIX` | The pre-release prefix | `-` -`BUILDMETA` | The build metadata, if any. | `001` -`BUILDMETA_PREFIX` | The metadata prefix. | `+` -`SEPARATOR` | The version separator. | `.` +| Field | Description | Example | +|:-------------------|:---------------------------------|:----------------------------------------------------------------------------------| +| `PROJECT` | The project name, if any. | `MyProject` | +| `BUILDDATE` | The build date. | [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) | +| `VERSION` | The full version string. | `1.2.3-alpha+001` | +| `MAJOR` | The major version. | `1` | +| `MINOR` | The minor version. | `2` | +| `PATCH` | The patch version. | `3` | +| `PRERELEASE` | The pre-release version, if any. | `alpha` | +| `PRERELASE_PREFIX` | The pre-release prefix | `-` | +| `BUILDMETA` | The build metadata, if any. | `001` | +| `BUILDMETA_PREFIX` | The metadata prefix. | `+` | +| `SEPARATOR` | The version separator. | `.` | ### Custom Template @@ -114,21 +117,21 @@ public final class {{className}} { The mustache variables automatically filled in by the processor are: -Variable | Description | Type -:-----------------------------|:----------------------------|:-------- -`{{packageName}}` | The package name. | `String` -`{{className}}` | The class name. | `String` -`{{project}}` | The project name. | `String` -`{{epoch}}` | The build epoch/unix time. | `long` -`{{major}}` | The major version. | `int` -`{{minor}}` | The minor version. | `int` -`{{patch}}` | The patch version. | `int` -`{{preRelease}}` | The pre-release version. | `String` -`{{preReleasePrefix}}` | The pre-release prefix. | `String` -`{{buildMeta}}` | The build metadata version. | `String` -`{{buildMetaPrefix}}` | The metadata prefix. | `String` -`{{separator}}` | The version separator. | `String` -`{{semver}}` or `{{version}}` | The full semantic version. | `String` +| Variable | Description | Type | +|:------------------------------|:----------------------------|:---------| +| `{{packageName}}` | The package name. | `String` | +| `{{className}}` | The class name. | `String` | +| `{{project}}` | The project name. | `String` | +| `{{epoch}}` | The build epoch/unix time. | `long` | +| `{{major}}` | The major version. | `int` | +| `{{minor}}` | The minor version. | `int` | +| `{{patch}}` | The patch version. | `int` | +| `{{preRelease}}` | The pre-release version. | `String` | +| `{{preReleasePrefix}}` | The pre-release prefix. | `String` | +| `{{buildMeta}}` | The build metadata version. | `String` | +| `{{buildMetaPrefix}}` | The metadata prefix. | `String` | +| `{{separator}}` | The version separator. | `String` | +| `{{semver}}` or `{{version}}` | The full semantic version. | `String` | Please also look at this [example](https://github.com/ethauvin/mobibot/blob/master/version.mustache) using [`java.time`](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) @@ -136,30 +139,30 @@ Please also look at this [example](https://github.com/ethauvin/mobibot/blob/mast The following annotation elements and properties are available: -Element | Property | Description | Default -:------------------|:----------------------------|:----------------------------------|:------------------------- -`project` | `version.project` | The project name. | -`major` | `version.major` | The major version number. | `1` -`minor` | `version.major` | The minor version number. | `0` -`patch` | `version.patch` | The patch version number. | `0` -`preRelease` | `version.prerelease` | The pre-release version. | -`preReleasePrefix` | `version.prerelease.prefix` | The pre-release prefix. | `-` -`buildMeta` | `version.buildmeta` | The build metadata version. | -`buildMetaPrefix` | `version.buildmeta.prefix` | The metadata prefix. | `+` -`separator` | `version.separator` | The version separator. | `.` -`packageName` | | The package name. | _Same as annotated class_ -`className` | | The name of the generated class. | `GeneratedVersion` -`properties` | | The properties file. | -`template` | | The template file. | `version.mustache` -`type` | | Either `java` or `kt` for Kotlin. | `java` -`keysPrefix` | | The prefix for all property keys. | `version.` +| Element | Property | Description | Default | +|:-------------------|:----------------------------|:----------------------------------|:--------------------------| +| `project` | `version.project` | The project name. | | +| `major` | `version.major` | The major version number. | `1` | +| `minor` | `version.major` | The minor version number. | `0` | +| `patch` | `version.patch` | The patch version number. | `0` | +| `preRelease` | `version.prerelease` | The pre-release version. | | +| `preReleasePrefix` | `version.prerelease.prefix` | The pre-release prefix. | `-` | +| `buildMeta` | `version.buildmeta` | The build metadata version. | | +| `buildMetaPrefix` | `version.buildmeta.prefix` | The metadata prefix. | `+` | +| `separator` | `version.separator` | The version separator. | `.` | +| `packageName` | | The package name. | _Same as annotated class_ | +| `className` | | The name of the generated class. | `GeneratedVersion` | +| `properties` | | The properties file. | | +| `template` | | The template file. | `version.mustache` | +| `type` | | Either `java` or `kt` for Kotlin. | `java` | +| `keysPrefix` | | The prefix for all property keys. | `version.` | In order to easily incorporate with existing projects, the property keys may be assigned custom values: ```java @Version( properties = "example.properties", - keysPrefix = "example." + keysPrefix = "example.", majorKey = "maj", minorKey = "min", patchKey = "build", @@ -168,7 +171,8 @@ In order to easily incorporate with existing projects, the property keys may be projectKey = "project" ) public class Example { -// ... + // ... +} ``` ```ini @@ -208,8 +212,13 @@ mvn verify To install and run from [bld](https://rife2.com/bld), just add the dependency to your build file: ```java -scope(compile) - .include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT"))); +public class ExampleBuild extends Project { + public ExampleBuild() { + // ... + scope(compile) + .include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT"))); + } +} ``` Please look at [ExamapleBuild](https://github.com/ethauvin/semver/blob/master/examples/java/bld/src/bld/java/com/example/ExampleBuild.java) in the [examples/java/bld](https://github.com/ethauvin/semver/tree/master/examples/java/bld) directory for a sample. @@ -264,7 +273,8 @@ import net.thauvin.erik.semver.Version @Version(properties = "version.properties", type="kt") open class Main { -// ... + // ... +} ``` The [Kotlin default template](https://github.com/ethauvin/semver/blob/master/src/main/resources/semver-kt.mustache) implements the same static fields and functions as the [Java template](#default-template). diff --git a/docs/README.html b/docs/README.html index 46cf22a..fef10ff 100644 --- a/docs/README.html +++ b/docs/README.html @@ -145,6 +145,7 @@ Version Plugin for Gradle.

  • Elements & Properties
  • Maven
  • +
  • bld
  • Gradle
    • Class Generation
    • @@ -164,9 +165,10 @@ Generation
      import net.thauvin.erik.semver.Version;
       
      -@Version(major = 1, minor = 0, patch = 0, preRelease = "beta")
      +@Version(major = 2, minor = 1, patch = 1, preRelease = "beta")
       public class A {
      -// ...
      + // ... +}
      • Or using a properties @@ -177,7 +179,8 @@ class="sourceCode java"> @Version(properties = "version.properties") public class A { -// ... + // ... +}
        # version.properties
         version.major=1
        @@ -200,7 +203,8 @@ processor will automatically look for it.

        @Version(template = "version.mustache")
         public class A {
        -// ...
        + // ... +}

        Default Template

        The default @@ -479,7 +483,7 @@ keys may be assigned custom values:

        @Version(
           properties = "example.properties",
        -  keysPrefix = "example."
        +  keysPrefix = "example.",
           majorKey = "maj",
           minorKey = "min",
           patchKey = "build",
        @@ -488,7 +492,8 @@ class="sourceCode java">  projectKey = "project"
         )
         public class Example {
        -// ...
        + // ... +}
        # example.properties
         example.project=Example
        @@ -514,7 +519,7 @@ follows:

        class="sourceCode xml"><dependency> <groupId>net.thauvin.erik</groupId> <artifactId>semver</artifactId> - <version>1.2.0</version> + <version>1.2.1-SNAPSHOT</version> </dependency>

        Please look at pom.xml @@ -523,24 +528,40 @@ href="https://github.com/ethauvin/semver/tree/master/examples/java">examples/jav directory for a sample:

        mvn verify
        +

        bld

        +

        To install and run from bld, just +add the dependency to your build file:

        +
        public class ExampleBuild extends Project {
        +    public ExampleBuild() {
        +        // ...
        +        scope(compile)
        +                .include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT")));
        +    }
        +}
        +

        Please look at ExamapleBuild +in the examples/java/bld +directory for a sample.

        Gradle

        Class Generation

        To install and run from Gradle, add the following to build.gradle:

        -
        repositories {
        -    mavenCentral()
        -}
        -
        -dependencies {
        -    annotationProcessor 'net.thauvin.erik:semver:1.2.0'
        -    compileOnly 'net.thauvin.erik:semver:1.2.0'
        -}
        -
        -tasks.withType(JavaCompile) {
        -    options.compilerArgs += [ "-Asemver.project.dir=$projectDir" ]
        -}
        +href="https://github.com/ethauvin/semver/blob/master/examples/java/gradle/build.gradle">build.gradle:

        +
        repositories {
        +    mavenCentral()
        +}
        +
        +dependencies {
        +    annotationProcessor 'net.thauvin.erik:semver:1.2.1-SNAPSHOT'
        +    compileOnly 'net.thauvin.erik:semver:1.2.1-SNAPSHOT'
        +}
        +
        +tasks.withType(JavaCompile) {
        +    options.compilerArgs += [ "-Asemver.project.dir=$projectDir" ]
        +}

        The directory containing the configuration files (version.properties, version.mustache) must be specified using the semver.project.dir processor @@ -550,18 +571,18 @@ href="https://github.com/ethauvin/semver/blob/master/examples/java/src/generated class will be automatically created in the build/generated directory upon compiling.

        Please look at build.gradle +href="https://github.com/ethauvin/semver/blob/master/examples/java/gradle/build.gradle">build.gradle in the examples/java +href="https://github.com/ethauvin/semver/tree/master/examples/java/gradle">examples/java/gradle directory for a sample.

        Class & Source Generation

        In order to also incorporate the generated source code into the source tree, add the following to build.gradle:

        -
        tasks.withType(JavaCompile) {
        -    options.generatedSourceOutputDirectory.set(file("${projectDir}/src/generated/java"))
        -}
        +
        tasks.withType(JavaCompile) {
        +    options.generatedSourceOutputDirectory.set(file("${projectDir}/src/generated/java"))
        +}

        The GeneratedVersion.java file will now be located in src/generated.

        @@ -570,12 +591,13 @@ file will now be located in src/generated.

        href="https://kotlinlang.org/">Kotlin.

        To generate a Kotlin version file, simply specify the type as follows:

        -
        import net.thauvin.erik.semver.Version
        -
        -@Version(properties = "version.properties", type="kt")
        -open class Main {
        -// ...
        +
        import net.thauvin.erik.semver.Version
        +
        +@Version(properties = "version.properties", type="kt")
        +open class Main {
        +    // ...
        +}

        The Kotlin default template implements the same static fields and functions as @@ -589,19 +611,19 @@ sample.

        To install and run from Gradle, add the following to build.gradle.kts:

        -
        var semverProcessor = "net.thauvin.erik:semver:1.2.0"
        -
        -dependencies {
        -    kapt(semverProcessor)
        -    compileOnly(semverProcessor)
        -}
        -
        -kapt {
        -    arguments {
        -        arg("semver.project.dir", projectDir)
        -    }
        -}
        +
        var semverProcessor = "net.thauvin.erik:semver:1.2.1-SNAPSHOT"
        +
        +dependencies {
        +    kapt(semverProcessor)
        +    compileOnly(semverProcessor)
        +}
        +
        +kapt {
        +    arguments {
        +        arg("semver.project.dir", projectDir)
        +    }
        +}

        The directory containing the configuration files (version.properties, version.mustache) must be specified using the semver.project.dir processor diff --git a/examples/java/bld/.idea/app.iml b/examples/java/bld/.idea/app.iml index 787b59b..082bb1c 100644 --- a/examples/java/bld/.idea/app.iml +++ b/examples/java/bld/.idea/app.iml @@ -9,6 +9,7 @@ + diff --git a/examples/java/bld/.idea/libraries/bld.xml b/examples/java/bld/.idea/libraries/bld.xml index abca604..722b42e 100644 --- a/examples/java/bld/.idea/libraries/bld.xml +++ b/examples/java/bld/.idea/libraries/bld.xml @@ -2,11 +2,11 @@ - + - + diff --git a/examples/java/bld/.idea/misc.xml b/examples/java/bld/.idea/misc.xml index 542659b..5238960 100644 --- a/examples/java/bld/.idea/misc.xml +++ b/examples/java/bld/.idea/misc.xml @@ -1,5 +1,7 @@ - + + + diff --git a/examples/java/bld/.vscode/settings.json b/examples/java/bld/.vscode/settings.json index c83239c..b676049 100644 --- a/examples/java/bld/.vscode/settings.json +++ b/examples/java/bld/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}/.bld/dist/bld-1.7.1.jar", + "${HOME}bld-1.7.2.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/examples/java/bld/lib/bld/bld-wrapper.jar b/examples/java/bld/lib/bld/bld-wrapper.jar index 1119a1a..605e3e4 100644 Binary files a/examples/java/bld/lib/bld/bld-wrapper.jar and b/examples/java/bld/lib/bld/bld-wrapper.jar differ diff --git a/examples/java/bld/lib/bld/bld-wrapper.properties b/examples/java/bld/lib/bld/bld-wrapper.properties index 15900ba..fc9091c 100644 --- a/examples/java/bld/lib/bld/bld-wrapper.properties +++ b/examples/java/bld/lib/bld/bld-wrapper.properties @@ -4,4 +4,4 @@ bld.extensions= bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES bld.downloadLocation= bld.sourceDirectories= -bld.version=1.7.1 +bld.version=1.7.2 diff --git a/examples/java/bld/src/bld/java/com/example/ExampleBuild.java b/examples/java/bld/src/bld/java/com/example/ExampleBuild.java index d00568c..7bf9e50 100644 --- a/examples/java/bld/src/bld/java/com/example/ExampleBuild.java +++ b/examples/java/bld/src/bld/java/com/example/ExampleBuild.java @@ -3,11 +3,20 @@ package com.example; import rife.bld.BuildCommand; import rife.bld.Project; +import java.io.File; import java.util.List; import static rife.bld.dependencies.Repository.*; import static rife.bld.dependencies.Scope.compile; +/** + * Example build. + * + *

          + *
        • ./bld run
        • + *
        • ./bld runExample
        • + *
        + */ public class ExampleBuild extends Project { public ExampleBuild() { pkg = "com.example"; @@ -20,16 +29,29 @@ public class ExampleBuild extends Project { repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL); - scope(compile) - .include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT"))); - } - - @BuildCommand(summary = "Run the example") - public void runExample() throws Exception { - runOperation().executeOnce(() -> runOperation().fromProject(this).mainClass("com.example.Example")); + scope(compile).include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT"))); } public static void main(String[] args) { new ExampleBuild().start(args); } + + /** + * Saves generated source files in the {@code build/generated} directory. + *

        + * To incorporate the generated source code into the source tree, add this directory as an additional source + * location in your IDE. + */ + @Override + public void compile() throws Exception { + var generated = new File(buildDirectory(), "generated"); + var ignore = generated.mkdir(); + this.compileOperation().compileOptions().addAll(List.of("-s", generated.getAbsolutePath())); + super.compile(); + } + + @BuildCommand(summary = "Run the example") + public void runExample() throws Exception { + runOperation().fromProject(this).mainClass("com.example.Example").execute(); + } } diff --git a/examples/java/bld/src/main/java/com/example/App.java b/examples/java/bld/src/main/java/com/example/App.java index 9875a22..91134b7 100644 --- a/examples/java/bld/src/main/java/com/example/App.java +++ b/examples/java/bld/src/main/java/com/example/App.java @@ -13,7 +13,7 @@ public final class App { * @param args The command line parameters. */ public static void main(final String... args) { - final SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); + final var sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); System.out.println("-----------------------------------------------------"); diff --git a/examples/java/bld/src/main/java/com/example/Example.java b/examples/java/bld/src/main/java/com/example/Example.java index 63a4b53..1829d52 100644 --- a/examples/java/bld/src/main/java/com/example/Example.java +++ b/examples/java/bld/src/main/java/com/example/Example.java @@ -9,7 +9,7 @@ import java.util.Locale; keysPrefix = "example.") public class Example { public static void main(final String... args) { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + final var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); System.out.println("-- From Example -------------------------------------"); diff --git a/examples/java/gradle/src/generated/java/com/example/ExampleVersion.java b/examples/java/gradle/src/generated/java/com/example/ExampleVersion.java index 1f3cf42..943ebb0 100644 --- a/examples/java/gradle/src/generated/java/com/example/ExampleVersion.java +++ b/examples/java/gradle/src/generated/java/com/example/ExampleVersion.java @@ -9,7 +9,7 @@ import java.util.Date; public final class ExampleVersion { public static final String PROJECT = "Java Example"; - public static final Date BUILDDATE = new Date(1692423608415L); + public static final Date BUILDDATE = new Date(1692564205424L); public static final String VERSION = "8.4.97-alpha+T800"; /** diff --git a/examples/java/gradle/src/generated/java/com/example/GeneratedVersion.java b/examples/java/gradle/src/generated/java/com/example/GeneratedVersion.java index 03423f9..28b778b 100644 --- a/examples/java/gradle/src/generated/java/com/example/GeneratedVersion.java +++ b/examples/java/gradle/src/generated/java/com/example/GeneratedVersion.java @@ -14,7 +14,7 @@ import java.util.Date; */ public final class GeneratedVersion { public static final String PROJECT = "Java App"; -public static final Date BUILDDATE = new Date(1692423608452L); +public static final Date BUILDDATE = new Date(1692564205452L); public static final int MAJOR = 11; public static final int MINOR = 11; public static final int PATCH = 20; diff --git a/examples/java/gradle/src/main/java/com/example/App.java b/examples/java/gradle/src/main/java/com/example/App.java index 9875a22..91134b7 100644 --- a/examples/java/gradle/src/main/java/com/example/App.java +++ b/examples/java/gradle/src/main/java/com/example/App.java @@ -13,7 +13,7 @@ public final class App { * @param args The command line parameters. */ public static void main(final String... args) { - final SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); + final var sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); System.out.println("-----------------------------------------------------"); diff --git a/examples/java/gradle/src/main/java/com/example/Example.java b/examples/java/gradle/src/main/java/com/example/Example.java index c8c8c0b..ce7b795 100644 --- a/examples/java/gradle/src/main/java/com/example/Example.java +++ b/examples/java/gradle/src/main/java/com/example/Example.java @@ -14,7 +14,7 @@ public final class Example { * @param args The command line parameters. */ public static void main(final String... args) { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + final var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); System.out.println("-- From Example -------------------------------------"); diff --git a/lib/bld/bld-wrapper.jar b/lib/bld/bld-wrapper.jar index e96bdc7..605e3e4 100644 Binary files a/lib/bld/bld-wrapper.jar and b/lib/bld/bld-wrapper.jar differ diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 5daccca3..3d087eb 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -6,4 +6,4 @@ bld.extensions-testng=com.uwyn.rife2:bld-testng:0.9.1-SNAPSHOT bld.repositories=MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.downloadLocation= bld.sourceDirectories= -bld.version=1.7.1 +bld.version=1.7.2 diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index 1c9f150..57422d2 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -34,7 +34,6 @@ 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 javax.annotation.processing.*; @@ -79,6 +78,11 @@ public class VersionProcessor extends AbstractProcessor { return template; } + private Mustache compileTemplate(final File dir, final String template) { + final var mf = new DefaultMustacheFactory(dir); + return mf.compile(template); + } + private void error(final String s) { log(Diagnostic.Kind.ERROR, s); } @@ -89,16 +93,16 @@ public class VersionProcessor extends AbstractProcessor { } private VersionInfo findValues(final Version version) throws IOException { - final VersionInfo versionInfo = new VersionInfo(version); + final var versionInfo = new VersionInfo(version); if (!version.properties().isEmpty()) { - final File propsFile = getLocalFile(version.properties()); + final var propsFile = getLocalFile(version.properties()); if (propsFile.isFile() && propsFile.canRead()) { note("Found properties: " + propsFile.getName() + " (" + propsFile.getAbsoluteFile().getParent() + ')'); - final Properties p = new Properties(); + final var p = new Properties(); - try (InputStreamReader reader = new InputStreamReader( + try (var reader = new InputStreamReader( Files.newInputStream(propsFile.toPath()), StandardCharsets.UTF_8)) { p.load(reader); @@ -139,7 +143,7 @@ public class VersionProcessor extends AbstractProcessor { private File getLocalFile(final String fileName) { if (processingEnv != null) { // null when testing. - final String prop = processingEnv.getOptions().get(Constants.SEMVER_PROJECT_DIR_ARG); + final var prop = processingEnv.getOptions().get(Constants.SEMVER_PROJECT_DIR_ARG); if (prop != null) { return new File(prop, fileName); } @@ -181,20 +185,20 @@ public class VersionProcessor extends AbstractProcessor { */ @Override public boolean process(final Set annotations, final RoundEnvironment roundEnv) { - final boolean isLocalTemplate = getLocalFile(Constants.DEFAULT_TEMPLATE_NAME).exists(); + final var isLocalTemplate = getLocalFile(Constants.DEFAULT_TEMPLATE_NAME).exists(); for (final Element element : roundEnv.getElementsAnnotatedWith(Version.class)) { - final Version version = element.getAnnotation(Version.class); + final var version = element.getAnnotation(Version.class); if (element.getKind() == ElementKind.CLASS) { - final Element enclosingElement = element.getEnclosingElement(); + final var enclosingElement = element.getEnclosingElement(); if (enclosingElement.getKind() == ElementKind.PACKAGE) { - final PackageElement packageElement = (PackageElement) enclosingElement; + final var packageElement = (PackageElement) enclosingElement; try { - final VersionInfo versionInfo = findValues(version); + final var versionInfo = findValues(version); if (Constants.EMPTY.equals(version.packageName())) { versionInfo.setPackageName(packageElement.getQualifiedName().toString()); } note("Found version: " + versionInfo.getVersion()); - final String template = getTemplate(isLocalTemplate, version); + final var template = getTemplate(isLocalTemplate, version); writeTemplate(version.type(), versionInfo, template); } catch (IOException | MustacheNotFoundException e) { @@ -232,28 +236,27 @@ public class VersionProcessor extends AbstractProcessor { private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) throws IOException { - final File dir = getLocalFile(""); - final MustacheFactory mf = new DefaultMustacheFactory(dir); - final Mustache mustache = mf.compile(template); + final var dir = getLocalFile(""); + final var mustache = compileTemplate(dir, template); - final String templateName = switch (mustache.getName()) { + final var templateName = switch (mustache.getName()) { case Constants.DEFAULT_JAVA_TEMPLATE -> "default (Java)"; case Constants.DEFAULT_KOTLIN_TEMPLATE -> "default (Kotlin)"; default -> mustache.getName() + " (" + dir.getAbsolutePath() + ')'; }; note("Loaded template: " + templateName); - final String fileName = versionInfo.getClassName() + '.' + type; + final var fileName = versionInfo.getClassName() + '.' + type; if (Constants.KOTLIN_TYPE.equalsIgnoreCase(type)) { - final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); + final var 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."); } - final File ktFile = new File(kaptGenDir, fileName); + final var ktFile = new File(kaptGenDir, fileName); if (!ktFile.getParentFile().exists() && !ktFile.getParentFile().mkdirs()) { note("Could not create target directory: " + ktFile.getParentFile().getAbsolutePath()); } - try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(ktFile.toPath()), + try (var osw = new OutputStreamWriter(Files.newOutputStream(ktFile.toPath()), StandardCharsets.UTF_8)) { mustache.execute(osw, versionInfo).flush(); } @@ -261,7 +264,7 @@ public class VersionProcessor extends AbstractProcessor { } else { final FileObject jfo = filer.createSourceFile( versionInfo.getPackageName() + '.' + versionInfo.getClassName()); - try (Writer writer = jfo.openWriter()) { + try (var writer = jfo.openWriter()) { mustache.execute(writer, versionInfo).flush(); } note("Generated source: " + fileName + " (" + new File(jfo.getName()).getAbsoluteFile().getParent() + ')'); diff --git a/src/test/java/net/thauvin/erik/semver/ConstantsTest.java b/src/test/java/net/thauvin/erik/semver/ConstantsTest.java index b6c2907..b407441 100644 --- a/src/test/java/net/thauvin/erik/semver/ConstantsTest.java +++ b/src/test/java/net/thauvin/erik/semver/ConstantsTest.java @@ -66,7 +66,7 @@ class ConstantsTest { templates.add(Constants.DEFAULT_KOTLIN_TEMPLATE); templates.add(Constants.DEFAULT_TEMPLATE_NAME); - for (final String tp : templates) { + for (final var tp : templates) { assertTrue(tp.endsWith(".mustache"), tp); } } diff --git a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java index ad3b742..5a05f69 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java @@ -32,13 +32,16 @@ package net.thauvin.erik.semver; +import com.github.mustachejava.Mustache; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Properties; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * The VersionProcessorTest class. @@ -49,29 +52,99 @@ import static org.junit.jupiter.api.Assertions.assertEquals; */ class VersionProcessorTest { private final VersionProcessor processor = new VersionProcessor(); - private final Version version = new VersionTest(); + private final VersionTest version = new VersionTest(); + + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") + @Test + void testCompileTemplate() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + final var method = processor.getClass().getDeclaredMethod("compileTemplate", File.class, String.class); + method.setAccessible(true); + + final var mustache = (Mustache) method.invoke(processor, new File("src/main/resources"), + Constants.DEFAULT_JAVA_TEMPLATE); + + assertEquals(Constants.DEFAULT_JAVA_TEMPLATE, mustache.getName(), Constants.DEFAULT_JAVA_TEMPLATE); + + try (var writer = new StringWriter()) { + mustache.execute(writer, version).flush(); + assertEquals(String.format(""" + /* + * This file is automatically generated. + * Do not modify! -- ALL CHANGES WILL BE ERASED! + */ + + package %s; + + import java.util.Date; + + /** + * Provides semantic version information. + * + * @author Semantic Version Annotation Processor + */ + public final class MyTest { + public static final String PROJECT = "%s"; + public static final Date BUILDDATE = new Date(L); + public static final int MAJOR = %d; + public static final int MINOR = %d; + public static final int PATCH = %d; + public static final String PRERELEASE = "%s"; + public static final String PRERELEASE_PREFIX = "%s"; + public static final String BUILDMETA = "%s"; + public static final String BUILDMETA_PREFIX = "%s"; + public static final String SEPARATOR = "%s"; + public static final String VERSION = ""; + + /** + * Disables the default constructor. + */ + private MyTest() { + throw new UnsupportedOperationException("Illegal constructor call."); + } + } + """, version.packageName(), version.project(), version.major(), version.minor(), version.patch(), + version.preRelease(), version.preReleasePrefix(), version.buildMeta(), + version.buildMetaPrefix(), version.separator()), + writer.toString()); + } + } @SuppressWarnings("PMD.AvoidAccessibilityAlteration") @Test void testFindValues() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - final Method method = processor.getClass().getDeclaredMethod("findValues", Version.class); + final var method = processor.getClass().getDeclaredMethod("findValues", Version.class); method.setAccessible(true); - final VersionInfo versionInfo = (VersionInfo) method.invoke(processor, version); + final var versionInfo = (VersionInfo) method.invoke(processor, version); assertEquals("0-0-7:vodka++martini", versionInfo.getVersion(), "getVersion(0-0-7:vodka++martin)"); assertEquals("James Bond", versionInfo.getProject(), "getProject(James Bond)"); } + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") + @Test + void testGetTemplate() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + final var method = processor.getClass().getDeclaredMethod("getTemplate", boolean.class, Version.class); + method.setAccessible(true); + + assertEquals(version.template(), method.invoke(processor, true, version), version.template); + version.setTemplate(Constants.DEFAULT_JAVA_TEMPLATE); + assertEquals(Constants.DEFAULT_TEMPLATE_NAME, method.invoke(processor, true, version), + "default"); + assertEquals(Constants.DEFAULT_KOTLIN_TEMPLATE, method.invoke(processor, false, version), + "kotlin"); + } + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") @Test void testParseIntProperty() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - final Properties p = new Properties(); + final var p = new Properties(); p.setProperty("1", "1"); p.setProperty("2", "2.1"); p.setProperty("3", "zero"); p.setProperty("4", " 4 "); - final Method method = processor.getClass().getDeclaredMethod("parseIntProperty", Properties.class, String.class, + final var method = processor.getClass().getDeclaredMethod("parseIntProperty", Properties.class, String.class, int.class); method.setAccessible(true); diff --git a/src/test/java/net/thauvin/erik/semver/VersionTest.java b/src/test/java/net/thauvin/erik/semver/VersionTest.java index 0153b1d..e14f359 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionTest.java @@ -43,6 +43,8 @@ import java.lang.annotation.Annotation; */ @SuppressWarnings({"ClassExplicitlyAnnotation", "SameReturnValue", "java:S2187", "PMD.TestClassWithoutTestCases"}) class VersionTest implements Version { + String template = "myversion.mustache"; + @Override public Class annotationType() { return null; @@ -160,11 +162,15 @@ class VersionTest implements Version { @Override public String template() { - return "myversion.mustache"; + return template; } @Override public String type() { return "kt"; } + + public void setTemplate(final String template) { + this.template = template; + } }