diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 7c26c8e..7e0a3c7 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -4,6 +4,5 @@ - \ No newline at end of file diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index 722b42e..abca604 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 b676049..c83239c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ ], "java.configuration.updateBuildConfiguration": "automatic", "java.project.referencedLibraries": [ - "${HOME}bld-1.7.2.jar", + "${HOME}/.bld/dist/bld-1.7.1.jar", "lib/compile/*.jar", "lib/runtime/*.jar", "lib/test/*.jar" diff --git a/README.md b/README.md index c13ec3d..5d26292 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,9 @@ This processor was inspired by Cédric Beust's [version-processor](https://githu ```java import net.thauvin.erik.semver.Version; -@Version(major = 2, minor = 1, patch = 1, preRelease = "beta") +@Version(major = 1, minor = 0, patch = 0, preRelease = "beta") public class A { - // ... -} +// ... ``` * Or using a [properties](hhttps://github.com/ethauvin/semver/blob/master/examples/java/version.properties) file: @@ -51,8 +50,7 @@ import net.thauvin.erik.semver.Version; @Version(properties = "version.properties") public class A { - // ... -} +// ... ``` ```ini @@ -76,27 +74,26 @@ 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 @@ -117,21 +114,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) @@ -139,30 +136,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", @@ -171,8 +168,7 @@ In order to easily incorporate with existing projects, the property keys may be projectKey = "project" ) public class Example { - // ... -} +// ... ``` ```ini @@ -212,13 +208,8 @@ mvn verify To install and run from [bld](https://rife2.com/bld), just add the dependency to your build file: ```java -public class ExampleBuild extends Project { - public ExampleBuild() { - // ... - scope(compile) - .include(dependency("net.thauvin.erik", "semver", version(1, 2, 1, "SNAPSHOT"))); - } -} +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. @@ -273,8 +264,7 @@ 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 fef10ff..46cf22a 100644 --- a/docs/README.html +++ b/docs/README.html @@ -145,7 +145,6 @@ Version Plugin for Gradle.

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

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

        Default Template

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

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

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

        Please look at pom.xml @@ -528,40 +523,24 @@ 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.1-SNAPSHOT'
        -    compileOnly 'net.thauvin.erik:semver:1.2.1-SNAPSHOT'
        -}
        -
        -tasks.withType(JavaCompile) {
        -    options.compilerArgs += [ "-Asemver.project.dir=$projectDir" ]
        -}
        +href="https://github.com/ethauvin/semver/blob/master/examples/java/build.gradle">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" ]
        +}

        The directory containing the configuration files (version.properties, version.mustache) must be specified using the semver.project.dir processor @@ -571,18 +550,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/build.gradle">build.gradle in the examples/java/gradle +href="https://github.com/ethauvin/semver/tree/master/examples/java">examples/java 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.

        @@ -591,13 +570,12 @@ 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 @@ -611,19 +589,19 @@ sample.

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

        -
        var semverProcessor = "net.thauvin.erik:semver:1.2.1-SNAPSHOT"
        -
        -dependencies {
        -    kapt(semverProcessor)
        -    compileOnly(semverProcessor)
        -}
        -
        -kapt {
        -    arguments {
        -        arg("semver.project.dir", projectDir)
        -    }
        -}
        +
        var semverProcessor = "net.thauvin.erik:semver:1.2.0"
        +
        +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 082bb1c..787b59b 100644 --- a/examples/java/bld/.idea/app.iml +++ b/examples/java/bld/.idea/app.iml @@ -9,7 +9,6 @@ - diff --git a/examples/java/bld/.idea/libraries/bld.xml b/examples/java/bld/.idea/libraries/bld.xml index 722b42e..abca604 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 5238960..542659b 100644 --- a/examples/java/bld/.idea/misc.xml +++ b/examples/java/bld/.idea/misc.xml @@ -1,7 +1,5 @@ + - - - diff --git a/examples/java/bld/.vscode/settings.json b/examples/java/bld/.vscode/settings.json index b676049..c83239c 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-1.7.2.jar", + "${HOME}/.bld/dist/bld-1.7.1.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 605e3e4..1119a1a 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 fc9091c..15900ba 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.2 +bld.version=1.7.1 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 7bf9e50..d00568c 100644 --- a/examples/java/bld/src/bld/java/com/example/ExampleBuild.java +++ b/examples/java/bld/src/bld/java/com/example/ExampleBuild.java @@ -3,20 +3,11 @@ 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"; @@ -29,29 +20,16 @@ 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"))); + 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")); } 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 91134b7..9875a22 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 var sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); + final SimpleDateFormat 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 1829d52..63a4b53 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 var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + final SimpleDateFormat 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 943ebb0..1f3cf42 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(1692564205424L); + public static final Date BUILDDATE = new Date(1692423608415L); 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 28b778b..03423f9 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(1692564205452L); +public static final Date BUILDDATE = new Date(1692423608452L); 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 91134b7..9875a22 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 var sdf = new SimpleDateFormat("EEE, d MMM yyyy 'at' HH:mm:ss z", Locale.US); + final SimpleDateFormat 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 ce7b795..c8c8c0b 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 var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + final SimpleDateFormat 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 605e3e4..e96bdc7 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 3d087eb..5daccca3 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.2 +bld.version=1.7.1 diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index 57422d2..1c9f150 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -34,6 +34,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 javax.annotation.processing.*; @@ -78,11 +79,6 @@ 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); } @@ -93,16 +89,16 @@ public class VersionProcessor extends AbstractProcessor { } private VersionInfo findValues(final Version version) throws IOException { - final var versionInfo = new VersionInfo(version); + final VersionInfo versionInfo = new VersionInfo(version); if (!version.properties().isEmpty()) { - final var propsFile = getLocalFile(version.properties()); + final File propsFile = getLocalFile(version.properties()); if (propsFile.isFile() && propsFile.canRead()) { note("Found properties: " + propsFile.getName() + " (" + propsFile.getAbsoluteFile().getParent() + ')'); - final var p = new Properties(); + final Properties p = new Properties(); - try (var reader = new InputStreamReader( + try (InputStreamReader reader = new InputStreamReader( Files.newInputStream(propsFile.toPath()), StandardCharsets.UTF_8)) { p.load(reader); @@ -143,7 +139,7 @@ public class VersionProcessor extends AbstractProcessor { private File getLocalFile(final String fileName) { if (processingEnv != null) { // null when testing. - final var prop = processingEnv.getOptions().get(Constants.SEMVER_PROJECT_DIR_ARG); + final String prop = processingEnv.getOptions().get(Constants.SEMVER_PROJECT_DIR_ARG); if (prop != null) { return new File(prop, fileName); } @@ -185,20 +181,20 @@ public class VersionProcessor extends AbstractProcessor { */ @Override public boolean process(final Set annotations, final RoundEnvironment roundEnv) { - final var isLocalTemplate = getLocalFile(Constants.DEFAULT_TEMPLATE_NAME).exists(); + final boolean isLocalTemplate = getLocalFile(Constants.DEFAULT_TEMPLATE_NAME).exists(); for (final Element element : roundEnv.getElementsAnnotatedWith(Version.class)) { - final var version = element.getAnnotation(Version.class); + final Version version = element.getAnnotation(Version.class); if (element.getKind() == ElementKind.CLASS) { - final var enclosingElement = element.getEnclosingElement(); + final Element enclosingElement = element.getEnclosingElement(); if (enclosingElement.getKind() == ElementKind.PACKAGE) { - final var packageElement = (PackageElement) enclosingElement; + final PackageElement packageElement = (PackageElement) enclosingElement; try { - final var versionInfo = findValues(version); + final VersionInfo versionInfo = findValues(version); if (Constants.EMPTY.equals(version.packageName())) { versionInfo.setPackageName(packageElement.getQualifiedName().toString()); } note("Found version: " + versionInfo.getVersion()); - final var template = getTemplate(isLocalTemplate, version); + final String template = getTemplate(isLocalTemplate, version); writeTemplate(version.type(), versionInfo, template); } catch (IOException | MustacheNotFoundException e) { @@ -236,27 +232,28 @@ public class VersionProcessor extends AbstractProcessor { private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) throws IOException { - final var dir = getLocalFile(""); - final var mustache = compileTemplate(dir, template); + final File dir = getLocalFile(""); + final MustacheFactory mf = new DefaultMustacheFactory(dir); + final Mustache mustache = mf.compile(template); - final var templateName = switch (mustache.getName()) { + final String 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 var fileName = versionInfo.getClassName() + '.' + type; + final String fileName = versionInfo.getClassName() + '.' + type; if (Constants.KOTLIN_TYPE.equalsIgnoreCase(type)) { - final var kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); + 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."); } - final var ktFile = new File(kaptGenDir, fileName); + final File ktFile = new File(kaptGenDir, fileName); if (!ktFile.getParentFile().exists() && !ktFile.getParentFile().mkdirs()) { note("Could not create target directory: " + ktFile.getParentFile().getAbsolutePath()); } - try (var osw = new OutputStreamWriter(Files.newOutputStream(ktFile.toPath()), + try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(ktFile.toPath()), StandardCharsets.UTF_8)) { mustache.execute(osw, versionInfo).flush(); } @@ -264,7 +261,7 @@ public class VersionProcessor extends AbstractProcessor { } else { final FileObject jfo = filer.createSourceFile( versionInfo.getPackageName() + '.' + versionInfo.getClassName()); - try (var writer = jfo.openWriter()) { + try (Writer 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 b407441..b6c2907 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 var tp : templates) { + for (final String 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 5a05f69..ad3b742 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java @@ -32,16 +32,13 @@ 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.*; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * The VersionProcessorTest class. @@ -52,99 +49,29 @@ import static org.junit.jupiter.api.Assertions.*; */ class VersionProcessorTest { private final VersionProcessor processor = new VersionProcessor(); - 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()); - } - } + private final Version version = new VersionTest(); @SuppressWarnings("PMD.AvoidAccessibilityAlteration") @Test void testFindValues() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - final var method = processor.getClass().getDeclaredMethod("findValues", Version.class); + final Method method = processor.getClass().getDeclaredMethod("findValues", Version.class); method.setAccessible(true); - final var versionInfo = (VersionInfo) method.invoke(processor, version); + final VersionInfo 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 var p = new Properties(); + final Properties p = new Properties(); p.setProperty("1", "1"); p.setProperty("2", "2.1"); p.setProperty("3", "zero"); p.setProperty("4", " 4 "); - final var method = processor.getClass().getDeclaredMethod("parseIntProperty", Properties.class, String.class, + final Method 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 e14f359..0153b1d 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionTest.java @@ -43,8 +43,6 @@ 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; @@ -162,15 +160,11 @@ class VersionTest implements Version { @Override public String template() { - return template; + return "myversion.mustache"; } @Override public String type() { return "kt"; } - - public void setTemplate(final String template) { - this.template = template; - } }