From 7bc3f0ec3f176bdc18a55597ee9dd904abf0c5fd Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 09:59:20 +0200 Subject: [PATCH 1/6] generate a version class --- build.gradle | 12 +++++ buildSrc/src/main/groovy/VersionPlugin.groovy | 44 +++++++++++++++ buildSrc/src/main/groovy/VersionTask.groovy | 54 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 buildSrc/src/main/groovy/VersionPlugin.groovy create mode 100644 buildSrc/src/main/groovy/VersionTask.groovy diff --git a/build.gradle b/build.gradle index 0ff3a259..47326e98 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,17 @@ repositories { } } +sourceSets { + main { + java { + srcDirs += "${buildDir}/version/java/main" + } + } +} + +compileJava.dependsOn "generateVersion" + + test { useJUnitPlatform() } @@ -156,4 +167,5 @@ dokka { outputDirectory = "$buildDir/docs/kotlin" } +apply plugin: VersionPlugin apply from: "${rootProject.rootDir}/gradle/publishing.gradle" diff --git a/buildSrc/src/main/groovy/VersionPlugin.groovy b/buildSrc/src/main/groovy/VersionPlugin.groovy new file mode 100644 index 00000000..e277404d --- /dev/null +++ b/buildSrc/src/main/groovy/VersionPlugin.groovy @@ -0,0 +1,44 @@ +import org.gradle.api.Action +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * provides a "generateVersion" task to a create a simple Version.java class: + * + *
{@code
+ * package io.openapiprocessor.spring;
+ *
+ * public class Version {
+ *     public static final String version = "${project.version}";
+ * }
+ * }
+ * + * + * The io/openapiprocessor/spring/Version.java file is generated to: + * + * $(project.buildDir}/main/java + * + * Add it as a source directory to include it in compilation. + */ +class VersionPlugin implements Plugin { + + void apply(Project project) { + project.afterEvaluate (new Action () { + + @Override + void execute (Project prj) { + prj.tasks.register ('generateVersion', VersionTask , new Action() { + + @Override + void execute (VersionTask task) { + task.targetDir = prj.buildDir + task.version = prj.version + } + + }) + } + + }) + } + +} diff --git a/buildSrc/src/main/groovy/VersionTask.groovy b/buildSrc/src/main/groovy/VersionTask.groovy new file mode 100644 index 00000000..91446c1f --- /dev/null +++ b/buildSrc/src/main/groovy/VersionTask.groovy @@ -0,0 +1,54 @@ +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction + +import java.nio.file.Files +import java.nio.file.Path +import java.time.Instant + +/** + * simple task to create a Version class. + */ +class VersionTask extends DefaultTask { + + /** + * Target directory for the generated version class. + * + * Used by gradle for the up-to-date check. + */ + @OutputDirectory + String targetDir + + @Internal + String version + + /** + * generate the version class. + */ + @TaskAction + void generateVersion () { + def path = Path.of ( + targetDir, "version", "main", "java", "io", "openapiprocessor", "spring") + + Files.createDirectories(path) + + def target = path.resolve ("Version.java") + + target.text = """\ +/* + * DO NOT MODIFY - this file was auto generated by buildSrc/src/main/groovy/VersionPlugin.groovy + * + * ${Instant.now ().toString ()} + */ + +package io.openapiprocessor.spring; + +public class Version { + public static final String version = "${version}"; +} + +""" + } + +} From 215964467c0ffa11b166b3dc14b73ea12bc7f410 Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 10:38:15 +0200 Subject: [PATCH 2/6] simpler target path --- build.gradle | 2 +- buildSrc/src/main/groovy/VersionTask.groovy | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 47326e98..3b53bbea 100644 --- a/build.gradle +++ b/build.gradle @@ -58,7 +58,7 @@ repositories { sourceSets { main { java { - srcDirs += "${buildDir}/version/java/main" + srcDirs "${buildDir}/version" } } } diff --git a/buildSrc/src/main/groovy/VersionTask.groovy b/buildSrc/src/main/groovy/VersionTask.groovy index 91446c1f..e12d7f74 100644 --- a/buildSrc/src/main/groovy/VersionTask.groovy +++ b/buildSrc/src/main/groovy/VersionTask.groovy @@ -28,9 +28,7 @@ class VersionTask extends DefaultTask { */ @TaskAction void generateVersion () { - def path = Path.of ( - targetDir, "version", "main", "java", "io", "openapiprocessor", "spring") - + def path = Path.of (targetDir, "version", "io", "openapiprocessor", "spring") Files.createDirectories(path) def target = path.resolve ("Version.java") From ea4841555b7bda9eef71437ff7e2161fa2d20a2e Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 10:38:51 +0200 Subject: [PATCH 3/6] improve generated header text --- .../openapi/spring/writer/java/HeaderWriter.groovy | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/github/hauner/openapi/spring/writer/java/HeaderWriter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/writer/java/HeaderWriter.groovy index 72764086..8a9df5b2 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/writer/java/HeaderWriter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/writer/java/HeaderWriter.groovy @@ -17,6 +17,9 @@ package com.github.hauner.openapi.spring.writer.java import com.github.hauner.openapi.core.writer.java.SimpleWriter +import io.openapiprocessor.spring.Version + +import java.time.Instant /** * Writer for a simple header of the generated interfaces & classes. @@ -27,8 +30,11 @@ class HeaderWriter implements SimpleWriter { static String HEADER = """\ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * ${Version.version} + * ${Instant.now().toString()} + * https://docs.openapiprocessor.io/spring */ """ From 566c68789e7b5ecfa4f3362951ba1ab4fb147b16 Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 11:09:44 +0200 Subject: [PATCH 4/6] use fixed header for integration tests --- .../processor/spring/EndToEndBase.groovy | 46 +++++++++++++++++++ .../spring/ProcessorEndToEndTest.groovy | 3 +- .../spring/ProcessorJimsFileSystemTest.groovy | 2 +- .../spring/ProcessorPendingTest.groovy | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/testInt/groovy/com/github/hauner/openapi/processor/spring/EndToEndBase.groovy diff --git a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/EndToEndBase.groovy b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/EndToEndBase.groovy new file mode 100644 index 00000000..0eab06e0 --- /dev/null +++ b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/EndToEndBase.groovy @@ -0,0 +1,46 @@ +/* + * Copyright 2020 the original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.hauner.openapi.processor.spring + +import com.github.hauner.openapi.spring.writer.java.HeaderWriter +import com.github.hauner.openapi.test.ProcessorTestBase +import com.github.hauner.openapi.test.TestSet +import org.junit.BeforeClass + +class EndToEndBase extends ProcessorTestBase { + + EndToEndBase (TestSet testSet) { + super (testSet) + } + + @BeforeClass + static void setConstantHeaderWriterText () { + // set a "fixed" header, we don't want moving version/date/time parts + + HeaderWriter.HEADER = """\ +/* + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring + */ + +""" + } + +} diff --git a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorEndToEndTest.groovy b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorEndToEndTest.groovy index 82e09c63..c3300bfa 100644 --- a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorEndToEndTest.groovy +++ b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorEndToEndTest.groovy @@ -18,7 +18,6 @@ package com.github.hauner.openapi.processor.spring import com.github.hauner.openapi.core.parser.ParserType import com.github.hauner.openapi.spring.processor.SpringProcessor -import com.github.hauner.openapi.test.ProcessorTestBase import com.github.hauner.openapi.test.TestSet import org.junit.Test import org.junit.runner.RunWith @@ -28,7 +27,7 @@ import org.junit.runners.Parameterized * using Junit so IDEA adds a "" when using assertEquals(). */ @RunWith(Parameterized) -class ProcessorEndToEndTest extends ProcessorTestBase { +class ProcessorEndToEndTest extends EndToEndBase { @Parameterized.Parameters(name = "{0}") static Collection sources () { diff --git a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorJimsFileSystemTest.groovy b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorJimsFileSystemTest.groovy index 53b2754a..b1c7672b 100644 --- a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorJimsFileSystemTest.groovy +++ b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorJimsFileSystemTest.groovy @@ -30,7 +30,7 @@ import org.junit.runners.Parameterized * using Junit so IDEA adds a "" when using assertEquals(). */ @RunWith(Parameterized) -class ProcessorJimsFileSystemTest extends ProcessorTestBase { +class ProcessorJimsFileSystemTest extends EndToEndBase { @Parameterized.Parameters(name = "{0}") static Collection sources () { diff --git a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorPendingTest.groovy b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorPendingTest.groovy index c159a5b8..be8d0ad4 100644 --- a/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorPendingTest.groovy +++ b/src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorPendingTest.groovy @@ -27,7 +27,7 @@ import org.junit.runners.Parameterized //@Ignore @RunWith(Parameterized) -class ProcessorPendingTest extends ProcessorTestBase { +class ProcessorPendingTest extends EndToEndBase { @Parameterized.Parameters(name = "{0}") static Collection sources () { From 5f68d9025cb4635dac54d2debcae200107874767 Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 11:10:04 +0200 Subject: [PATCH 5/6] update to new header --- .../endpoint-http-mapping/generated/api/EndpointApi.java | 7 +++++-- .../tests/params-complex-data-types/generated/api/Api.java | 7 +++++-- .../params-complex-data-types/generated/model/Props.java | 7 +++++-- .../tests/params-pageable-mapping/generated/api/Api.java | 7 +++++-- .../generated/api/EndpointApi.java | 7 +++++-- .../generated/api/Api.java | 7 +++++-- .../tests/params-request-body/generated/api/Api.java | 7 +++++-- .../tests/params-request-body/generated/model/Book.java | 7 +++++-- .../generated/api/EndpointApi.java | 7 +++++-- 9 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/testInt/resources/tests/endpoint-http-mapping/generated/api/EndpointApi.java b/src/testInt/resources/tests/endpoint-http-mapping/generated/api/EndpointApi.java index 94572252..bbf27e74 100644 --- a/src/testInt/resources/tests/endpoint-http-mapping/generated/api/EndpointApi.java +++ b/src/testInt/resources/tests/endpoint-http-mapping/generated/api/EndpointApi.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-complex-data-types/generated/api/Api.java b/src/testInt/resources/tests/params-complex-data-types/generated/api/Api.java index 1c4ab69b..c169b38d 100644 --- a/src/testInt/resources/tests/params-complex-data-types/generated/api/Api.java +++ b/src/testInt/resources/tests/params-complex-data-types/generated/api/Api.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-complex-data-types/generated/model/Props.java b/src/testInt/resources/tests/params-complex-data-types/generated/model/Props.java index 9e1fe07c..6aeeb62e 100644 --- a/src/testInt/resources/tests/params-complex-data-types/generated/model/Props.java +++ b/src/testInt/resources/tests/params-complex-data-types/generated/model/Props.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.model; diff --git a/src/testInt/resources/tests/params-pageable-mapping/generated/api/Api.java b/src/testInt/resources/tests/params-pageable-mapping/generated/api/Api.java index 0308f12d..1ef975ce 100644 --- a/src/testInt/resources/tests/params-pageable-mapping/generated/api/Api.java +++ b/src/testInt/resources/tests/params-pageable-mapping/generated/api/Api.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-path-simple-data-types/generated/api/EndpointApi.java b/src/testInt/resources/tests/params-path-simple-data-types/generated/api/EndpointApi.java index 7c9cc548..d2134fc0 100644 --- a/src/testInt/resources/tests/params-path-simple-data-types/generated/api/EndpointApi.java +++ b/src/testInt/resources/tests/params-path-simple-data-types/generated/api/EndpointApi.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-request-body-multipart-mapping/generated/api/Api.java b/src/testInt/resources/tests/params-request-body-multipart-mapping/generated/api/Api.java index 4f054c2e..27134725 100644 --- a/src/testInt/resources/tests/params-request-body-multipart-mapping/generated/api/Api.java +++ b/src/testInt/resources/tests/params-request-body-multipart-mapping/generated/api/Api.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-request-body/generated/api/Api.java b/src/testInt/resources/tests/params-request-body/generated/api/Api.java index 148c89bd..dc8771be 100644 --- a/src/testInt/resources/tests/params-request-body/generated/api/Api.java +++ b/src/testInt/resources/tests/params-request-body/generated/api/Api.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; diff --git a/src/testInt/resources/tests/params-request-body/generated/model/Book.java b/src/testInt/resources/tests/params-request-body/generated/model/Book.java index b0590499..9289aafd 100644 --- a/src/testInt/resources/tests/params-request-body/generated/model/Book.java +++ b/src/testInt/resources/tests/params-request-body/generated/model/Book.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.model; diff --git a/src/testInt/resources/tests/params-simple-data-types/generated/api/EndpointApi.java b/src/testInt/resources/tests/params-simple-data-types/generated/api/EndpointApi.java index 72f79542..8fda2639 100644 --- a/src/testInt/resources/tests/params-simple-data-types/generated/api/EndpointApi.java +++ b/src/testInt/resources/tests/params-simple-data-types/generated/api/EndpointApi.java @@ -1,6 +1,9 @@ /* - * This class is auto generated by https://github.com/hauner/openapi-processor-spring. - * DO NOT EDIT. + * DO NOT MODIFY - this class was auto generated by openapi-processor-spring + * + * test + * time + * https://docs.openapiprocessor.io/spring */ package generated.api; From c5d69a98b900742110dc7a7017cd5669ebad6241 Mon Sep 17 00:00:00 2001 From: Martin Hauner Date: Wed, 29 Jul 2020 11:28:19 +0200 Subject: [PATCH 6/6] fix build, avoids missing directory warning --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 3b53bbea..8fe7a368 100644 --- a/build.gradle +++ b/build.gradle @@ -63,7 +63,7 @@ sourceSets { } } -compileJava.dependsOn "generateVersion" +compileKotlin.dependsOn "generateVersion" test {