generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Generate build plugin sources for JSON schema generator #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andriy-dmytruk
wants to merge
2
commits into
1.5.x
Choose a base branch
from
andriy/build-plugin-sourcegen
base: 1.5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...rator/src/main/java/io/micronaut/jsonschema/generator/plugin/JsonSchemaGeneratorTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright 2017-2023 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 | ||
* | ||
* https://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 io.micronaut.jsonschema.generator.plugin; | ||
|
||
import io.micronaut.inject.visitor.VisitorContext; | ||
import io.micronaut.jsonschema.generator.SourceGenerator; | ||
import io.micronaut.jsonschema.generator.loaders.UrlLoader; | ||
import io.micronaut.jsonschema.generator.utils.GeneratorContext; | ||
import io.micronaut.jsonschema.generator.utils.SourceGeneratorConfig; | ||
import io.micronaut.jsonschema.generator.utils.SourceGeneratorConfig.JavadocConfig; | ||
import io.micronaut.jsonschema.generator.utils.SourceGeneratorConfig.RecordAdoptionStrategy; | ||
import io.micronaut.jsonschema.generator.utils.SourceGeneratorConfigBuilder; | ||
import io.micronaut.sourcegen.annotations.PluginTask; | ||
import io.micronaut.sourcegen.annotations.PluginTaskExecutable; | ||
import io.micronaut.sourcegen.annotations.PluginTaskParameter; | ||
import io.micronaut.sourcegen.annotations.PluginTaskParameter.OutputType; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* A configuration class for the JSON schema source generator, encapsulating the configuration | ||
* properties related to the input and output paths, file names, and URL. | ||
* | ||
* @param inputURL The URL of the JSON schema file to be downloaded and processed | ||
* @param inputFile The JSON schema file, typically ending in {@code .schema.json} | ||
* @param inputDirectory The path to the folder where the input files (JSON schemas) are located. | ||
* Each of the files in the directory will be used for generation | ||
* @param language The programming language to generate the output types in. | ||
* @param outputPackageName The package name to be applied to the generated source files | ||
* This field is optional and can be {@code null} if no package name is needed | ||
* @param outputFileName The name of the file where the generated source code will be written | ||
* if there is a single output source. | ||
* @param acceptedUrlPatterns The URL patterns that are allowed. | ||
* Used to filter or validate input resources specified by URL. | ||
* URLs matching at least one pattern will be accepted. The default | ||
* value is {@code "^https://.* /.*.json"} | ||
* @param outputDirectory The path where generated source files will be saved | ||
* The path should be a valid, writable directory path | ||
* @param javaOutputDirectory Internal output path for Java sources | ||
* @param groovyOutputDirectory Internal output path for Groovy sources | ||
* @param kotlinOutputDirectory Internal output path for Kotlin sources | ||
* @author Andriy Dmytruk | ||
* @version 1.5 | ||
*/ | ||
@PluginTask | ||
public record JsonSchemaGeneratorTask( | ||
String inputURL, | ||
File inputFile, | ||
@PluginTaskParameter(directory = true) | ||
File inputDirectory, | ||
@PluginTaskParameter(defaultValue = "io.micronaut.jsonschema") | ||
String outputPackageName, | ||
String outputFileName, | ||
@PluginTaskParameter(defaultValue = "JAVA") | ||
Language language, | ||
@PluginTaskParameter | ||
List<String> acceptedUrlPatterns, | ||
JavadocConfig javadoc, | ||
RecordAdoptionStrategy recordAdoptionStrategy, | ||
@PluginTaskParameter(directory = true, output = OutputType.CUSTOM, required = true) | ||
File outputDirectory, | ||
@PluginTaskParameter(directory = true, output = OutputType.JAVA_SOURCES, internal = true) | ||
File javaOutputDirectory, | ||
@PluginTaskParameter(directory = true, output = OutputType.GROOVY_SOURCES, internal = true) | ||
File groovyOutputDirectory, | ||
@PluginTaskParameter(directory = true, output = OutputType.KOTLIN_SOURCES, internal = true) | ||
File kotlinOutputDirectory | ||
) { | ||
|
||
@PluginTaskExecutable | ||
public void generate() { | ||
if (inputDirectory == null && inputURL == null && inputFile == null) { | ||
throw new IllegalArgumentException("Must provide oneOf: inputDirectory, inputURL, inputFile"); | ||
} | ||
if (acceptedUrlPatterns != null) { | ||
UrlLoader.setAllowedUrlPatterns(acceptedUrlPatterns); | ||
} | ||
|
||
File languageDir = switch (language) { | ||
case JAVA -> javaOutputDirectory; | ||
case GROOVY -> groovyOutputDirectory; | ||
case KOTLIN -> kotlinOutputDirectory; | ||
}; | ||
andriy-dmytruk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SourceGenerator generator = new SourceGenerator(language.name()); | ||
try { | ||
SourceGeneratorConfig config = new SourceGeneratorConfigBuilder() | ||
.withJsonUrl(inputURL) | ||
.withJsonFile(inputFile) | ||
.withInputFolder(inputDirectory == null ? null : inputDirectory.toPath()) | ||
.withOutputFolder(languageDir.toPath()) | ||
.withOutputPackageName(outputPackageName) | ||
.withOutputFileName(outputFileName) | ||
.withJavadoc(javadoc) | ||
.withRecordAdoptionStrategy(recordAdoptionStrategy) | ||
.build(); | ||
generator.generate(config); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Could not generate based on JSON schema", e); | ||
} | ||
} | ||
|
||
/** | ||
* A language to generated sources in. | ||
*/ | ||
public enum Language { | ||
/** The Java language. */ | ||
JAVA, | ||
/** The Groovy language. */ | ||
GROOVY, | ||
/** The Kotlin language. */ | ||
KOTLIN | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit confused by the outputDirectory here.
In the previous version, it was not required and its default value was
generated/jsonschema
. It is also not used in the guide examples and docs etc. And we added the language source directories after the given outputDirectory in the plugins. So the final address used to be:outputDirectory + languageDir + outputPackageName + outputFileName
.But here, we are setting the outputDirectory as the languageDir. Is this what is desired?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I added the different language folders, so that is more explicit where the files are generated.
The folders have to be added to the classpath in different ways (like groovy to the groovy source sets if groovy plugin exists).
This is a change from the previous behavior when output directory was overridden, but also means that the directory is added to the appropriate classpath.