-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature for micronaut-sourcegen. (#2470)
closes #2469
- Loading branch information
Showing
4 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
...-core/src/main/java/io/micronaut/starter/feature/sourcegen/SourcegenFeatureValidator.java
This file contains 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,56 @@ | ||
/* | ||
* Copyright 2017-2024 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.starter.feature.sourcegen; | ||
|
||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.starter.application.ApplicationType; | ||
import io.micronaut.starter.feature.Feature; | ||
import io.micronaut.starter.feature.validation.FeatureValidator; | ||
import io.micronaut.starter.options.Language; | ||
import io.micronaut.starter.options.Options; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
@Singleton | ||
public class SourcegenFeatureValidator implements FeatureValidator { | ||
|
||
@Override | ||
public void validatePreProcessing(Options options, ApplicationType applicationType, Set<Feature> features) { | ||
|
||
} | ||
|
||
@Override | ||
public void validatePostProcessing(Options options, ApplicationType applicationType, Set<Feature> features) { | ||
if (features.stream().anyMatch(f -> f instanceof SourcegenJava)) { | ||
if (!supports(options.getLanguage())) { | ||
throw new IllegalArgumentException("sourcegen-generator is not supported in " + StringUtils.capitalize(options.getLanguage().getName()) + " applications"); | ||
} | ||
} | ||
} | ||
|
||
public static List<Language> supportedLanguages() { | ||
return Stream.of(Language.values()) | ||
.filter(SourcegenFeatureValidator::supports) | ||
.toList(); | ||
} | ||
|
||
public static boolean supports(Language language) { | ||
return language != Language.GROOVY; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
starter-core/src/main/java/io/micronaut/starter/feature/sourcegen/SourcegenJava.java
This file contains 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,93 @@ | ||
/* | ||
* Copyright 2017-2024 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.starter.feature.sourcegen; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.starter.application.ApplicationType; | ||
import io.micronaut.starter.application.generator.GeneratorContext; | ||
import io.micronaut.starter.build.dependencies.Dependency; | ||
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils; | ||
import io.micronaut.starter.build.dependencies.Scope; | ||
import io.micronaut.starter.feature.Category; | ||
import io.micronaut.starter.feature.Feature; | ||
import io.micronaut.starter.options.Language; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class SourcegenJava implements Feature { | ||
|
||
public static final String NAME = "sourcegen-generator"; | ||
|
||
private static final Dependency DEPENDENCY_MICRONAUT_SOURCEGEN_ANNOTATIONS = MicronautDependencyUtils.sourcegenDependency() | ||
.artifactId("micronaut-sourcegen-annotations") | ||
.scope(Scope.COMPILE) | ||
.build(); | ||
|
||
private static final Dependency DEPENDENCY_MICRONAUT_SOURCEGEN_GENERATOR_JAVA = MicronautDependencyUtils.sourcegenDependency() | ||
.artifactId("micronaut-sourcegen-generator-java") | ||
.scope(Scope.ANNOTATION_PROCESSOR) | ||
.build(); | ||
|
||
private static final Dependency DEPENDENCY_MICRONAUT_SOURCEGEN_GENERATOR_KOTLIN = MicronautDependencyUtils.sourcegenDependency() | ||
.artifactId("micronaut-sourcegen-generator-kotlin") | ||
.scope(Scope.ANNOTATION_PROCESSOR) | ||
.build(); | ||
|
||
@Override | ||
@NonNull | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public String getTitle() { | ||
return "Micronaut source code generator for Java and Kotlin"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Micronaut SourceGen exposes a language-neutral API for source code generation"; | ||
} | ||
|
||
@Override | ||
public String getCategory() { | ||
return Category.API; | ||
} | ||
|
||
@Override | ||
public String getMicronautDocumentation() { | ||
return "https://micronaut-projects.github.io/micronaut-sourcegen/latest/guide/"; | ||
} | ||
|
||
@Override | ||
public boolean supports(ApplicationType applicationType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void apply(GeneratorContext generatorContext) { | ||
addDependencies(generatorContext); | ||
} | ||
|
||
private void addDependencies(GeneratorContext generatorContext) { | ||
generatorContext.addDependency(DEPENDENCY_MICRONAUT_SOURCEGEN_ANNOTATIONS); | ||
if (generatorContext.getLanguage() == Language.JAVA) { | ||
generatorContext.addDependency(DEPENDENCY_MICRONAUT_SOURCEGEN_GENERATOR_JAVA); | ||
} else if (generatorContext.getLanguage() == Language.KOTLIN) { | ||
generatorContext.addDependency(DEPENDENCY_MICRONAUT_SOURCEGEN_GENERATOR_KOTLIN); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
starter-core/src/test/groovy/io/micronaut/starter/feature/sourcegen/SourcegenJavaSpec.groovy
This file contains 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,75 @@ | ||
package io.micronaut.starter.feature.sourcegen | ||
|
||
import io.micronaut.starter.ApplicationContextSpec | ||
import io.micronaut.starter.BuildBuilder | ||
import io.micronaut.starter.application.ApplicationType | ||
import io.micronaut.starter.build.BuildTestUtil | ||
import io.micronaut.starter.build.BuildTestVerifier | ||
import io.micronaut.starter.build.dependencies.Scope | ||
import io.micronaut.starter.feature.Category | ||
import io.micronaut.starter.fixture.CommandOutputFixture | ||
import io.micronaut.starter.options.BuildTool | ||
import io.micronaut.starter.options.Language | ||
import spock.lang.Shared | ||
|
||
class SourcegenJavaSpec extends ApplicationContextSpec implements CommandOutputFixture { | ||
|
||
@Shared | ||
SourcegenJava sourcegenJava = beanContext.getBean(SourcegenJava) | ||
|
||
void 'test readme.md with feature sourcegen-generator contains links to micronaut docs'() { | ||
when: | ||
Map<String, String> output = generate([SourcegenJava.NAME]) | ||
String readme = output["README.md"] | ||
|
||
then: | ||
readme | ||
readme.contains("https://micronaut-projects.github.io/micronaut-sourcegen/latest/guide/") | ||
} | ||
|
||
void "feature sourcegen-generator supports applicationType=#applicationType"(ApplicationType applicationType) { | ||
expect: | ||
sourcegenJava.supports(applicationType) | ||
|
||
where: | ||
applicationType << ApplicationType.values() | ||
} | ||
|
||
void "feature sourcegen-generator feature is API category"() { | ||
expect: | ||
sourcegenJava.category == Category.API | ||
} | ||
|
||
void 'sourcegen-generator feature not supported for groovy'() { | ||
when: | ||
new BuildBuilder(beanContext, BuildTool.GRADLE) | ||
.features([SourcegenJava.NAME]) | ||
.language(Language.GROOVY) | ||
.render() | ||
|
||
then: | ||
IllegalArgumentException e = thrown() | ||
e.message == 'sourcegen-generator is not supported in Groovy applications' | ||
} | ||
|
||
|
||
void "test dependencies are present for #buildTool and #language"(BuildTool buildTool, Language language) { | ||
when: | ||
String template = new BuildBuilder(beanContext, buildTool) | ||
.features([SourcegenJava.NAME]) | ||
.language(language) | ||
.render() | ||
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template) | ||
|
||
then: | ||
verifier.hasDependency("io.micronaut.sourcegen", "micronaut-sourcegen-annotations", Scope.COMPILE) | ||
if (language == Language.JAVA) { | ||
assert verifier.hasAnnotationProcessor("io.micronaut.sourcegen", "micronaut-sourcegen-generator-java") | ||
} else if (language == Language.KOTLIN) { | ||
assert verifier.hasAnnotationProcessor("io.micronaut.sourcegen", "micronaut-sourcegen-generator-kotlin") | ||
} | ||
|
||
where: | ||
[buildTool, language] << [BuildTool.values(), [Language.JAVA, Language.KOTLIN]].combinations() | ||
} | ||
} |