Skip to content

Commit 8a446ad

Browse files
authored
Merge pull request #114 from openapi-processor/header
improve heaer of generated source files
2 parents 4c0d4f1 + c5d69a9 commit 8a446ad

File tree

17 files changed

+210
-24
lines changed

17 files changed

+210
-24
lines changed

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ repositories {
5555
}
5656
}
5757

58+
sourceSets {
59+
main {
60+
java {
61+
srcDirs "${buildDir}/version"
62+
}
63+
}
64+
}
65+
66+
compileKotlin.dependsOn "generateVersion"
67+
68+
5869
test {
5970
useJUnitPlatform()
6071
}
@@ -156,4 +167,5 @@ dokka {
156167
outputDirectory = "$buildDir/docs/kotlin"
157168
}
158169

170+
apply plugin: VersionPlugin
159171
apply from: "${rootProject.rootDir}/gradle/publishing.gradle"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.gradle.api.Action
2+
import org.gradle.api.Plugin
3+
import org.gradle.api.Project
4+
5+
/**
6+
* provides a "generateVersion" task to a create a simple Version.java class:
7+
*
8+
* <pre>{@code
9+
* package io.openapiprocessor.spring;
10+
*
11+
* public class Version {
12+
* public static final String version = "${project.version}";
13+
* }
14+
* }</pre>
15+
*
16+
*
17+
* The io/openapiprocessor/spring/Version.java file is generated to:
18+
*
19+
* $(project.buildDir}/main/java
20+
*
21+
* Add it as a source directory to include it in compilation.
22+
*/
23+
class VersionPlugin implements Plugin<Project> {
24+
25+
void apply(Project project) {
26+
project.afterEvaluate (new Action<Project> () {
27+
28+
@Override
29+
void execute (Project prj) {
30+
prj.tasks.register ('generateVersion', VersionTask , new Action<VersionTask>() {
31+
32+
@Override
33+
void execute (VersionTask task) {
34+
task.targetDir = prj.buildDir
35+
task.version = prj.version
36+
}
37+
38+
})
39+
}
40+
41+
})
42+
}
43+
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import org.gradle.api.DefaultTask
2+
import org.gradle.api.tasks.Internal
3+
import org.gradle.api.tasks.OutputDirectory
4+
import org.gradle.api.tasks.TaskAction
5+
6+
import java.nio.file.Files
7+
import java.nio.file.Path
8+
import java.time.Instant
9+
10+
/**
11+
* simple task to create a Version class.
12+
*/
13+
class VersionTask extends DefaultTask {
14+
15+
/**
16+
* Target directory for the generated version class.
17+
*
18+
* Used by gradle for the up-to-date check.
19+
*/
20+
@OutputDirectory
21+
String targetDir
22+
23+
@Internal
24+
String version
25+
26+
/**
27+
* generate the version class.
28+
*/
29+
@TaskAction
30+
void generateVersion () {
31+
def path = Path.of (targetDir, "version", "io", "openapiprocessor", "spring")
32+
Files.createDirectories(path)
33+
34+
def target = path.resolve ("Version.java")
35+
36+
target.text = """\
37+
/*
38+
* DO NOT MODIFY - this file was auto generated by buildSrc/src/main/groovy/VersionPlugin.groovy
39+
*
40+
* ${Instant.now ().toString ()}
41+
*/
42+
43+
package io.openapiprocessor.spring;
44+
45+
public class Version {
46+
public static final String version = "${version}";
47+
}
48+
49+
"""
50+
}
51+
52+
}

src/main/groovy/com/github/hauner/openapi/spring/writer/java/HeaderWriter.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.github.hauner.openapi.spring.writer.java
1818

1919
import com.github.hauner.openapi.core.writer.java.SimpleWriter
20+
import io.openapiprocessor.spring.Version
21+
22+
import java.time.Instant
2023

2124
/**
2225
* Writer for a simple header of the generated interfaces & classes.
@@ -27,8 +30,11 @@ class HeaderWriter implements SimpleWriter {
2730

2831
static String HEADER = """\
2932
/*
30-
* This class is auto generated by https://github.com/hauner/openapi-processor-spring.
31-
* DO NOT EDIT.
33+
* DO NOT MODIFY - this class was auto generated by openapi-processor-spring
34+
*
35+
* ${Version.version}
36+
* ${Instant.now().toString()}
37+
* https://docs.openapiprocessor.io/spring
3238
*/
3339
3440
"""
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.processor.spring
18+
19+
import com.github.hauner.openapi.spring.writer.java.HeaderWriter
20+
import com.github.hauner.openapi.test.ProcessorTestBase
21+
import com.github.hauner.openapi.test.TestSet
22+
import org.junit.BeforeClass
23+
24+
class EndToEndBase extends ProcessorTestBase {
25+
26+
EndToEndBase (TestSet testSet) {
27+
super (testSet)
28+
}
29+
30+
@BeforeClass
31+
static void setConstantHeaderWriterText () {
32+
// set a "fixed" header, we don't want moving version/date/time parts
33+
34+
HeaderWriter.HEADER = """\
35+
/*
36+
* DO NOT MODIFY - this class was auto generated by openapi-processor-spring
37+
*
38+
* test
39+
* time
40+
* https://docs.openapiprocessor.io/spring
41+
*/
42+
43+
"""
44+
}
45+
46+
}

src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorEndToEndTest.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.github.hauner.openapi.processor.spring
1818

1919
import com.github.hauner.openapi.core.parser.ParserType
2020
import com.github.hauner.openapi.spring.processor.SpringProcessor
21-
import com.github.hauner.openapi.test.ProcessorTestBase
2221
import com.github.hauner.openapi.test.TestSet
2322
import org.junit.Test
2423
import org.junit.runner.RunWith
@@ -28,7 +27,7 @@ import org.junit.runners.Parameterized
2827
* using Junit so IDEA adds a "<Click to see difference>" when using assertEquals().
2928
*/
3029
@RunWith(Parameterized)
31-
class ProcessorEndToEndTest extends ProcessorTestBase {
30+
class ProcessorEndToEndTest extends EndToEndBase {
3231

3332
@Parameterized.Parameters(name = "{0}")
3433
static Collection<TestSet> sources () {

src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorJimsFileSystemTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.junit.runners.Parameterized
3030
* using Junit so IDEA adds a "<Click to see difference>" when using assertEquals().
3131
*/
3232
@RunWith(Parameterized)
33-
class ProcessorJimsFileSystemTest extends ProcessorTestBase {
33+
class ProcessorJimsFileSystemTest extends EndToEndBase {
3434

3535
@Parameterized.Parameters(name = "{0}")
3636
static Collection<TestSet> sources () {

src/testInt/groovy/com/github/hauner/openapi/processor/spring/ProcessorPendingTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.junit.runners.Parameterized
2727

2828
//@Ignore
2929
@RunWith(Parameterized)
30-
class ProcessorPendingTest extends ProcessorTestBase {
30+
class ProcessorPendingTest extends EndToEndBase {
3131

3232
@Parameterized.Parameters(name = "{0}")
3333
static Collection<TestSet> sources () {

src/testInt/resources/tests/endpoint-http-mapping/generated/api/EndpointApi.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
2-
* This class is auto generated by https://github.com/hauner/openapi-processor-spring.
3-
* DO NOT EDIT.
2+
* DO NOT MODIFY - this class was auto generated by openapi-processor-spring
3+
*
4+
* test
5+
* time
6+
* https://docs.openapiprocessor.io/spring
47
*/
58

69
package generated.api;

src/testInt/resources/tests/params-complex-data-types/generated/api/Api.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
2-
* This class is auto generated by https://github.com/hauner/openapi-processor-spring.
3-
* DO NOT EDIT.
2+
* DO NOT MODIFY - this class was auto generated by openapi-processor-spring
3+
*
4+
* test
5+
* time
6+
* https://docs.openapiprocessor.io/spring
47
*/
58

69
package generated.api;

0 commit comments

Comments
 (0)