Skip to content

Commit

Permalink
example of feature contributing configuration and dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Feb 17, 2025
1 parent c8f6746 commit d71a1e7
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 12 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ rewrite-recipe-bom = "3.2.0"
[libraries]
rewrite-recipe-bom = { module = "org.openrewrite.recipe:rewrite-recipe-bom", version.ref = "rewrite-recipe-bom" }
rewrite-core = { module = "org.openrewrite:rewrite-core" }
rewrite-properties = { module = "org.openrewrite:rewrite-properties" }
rewrite-gradle = { module = "org.openrewrite:rewrite-gradle" }
rewrite-java-dependencies = { module = "org.openrewrite.recipe:rewrite-java-dependencies" }
rewrite-maven = { module = "org.openrewrite:rewrite-maven" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.micronaut.starter.feature.validation.ProjectNameValidator;
import io.micronaut.starter.io.ConsoleOutput;
import io.micronaut.starter.openrewrite.RecipeDependencyFetcher;
import io.micronaut.starter.openrewrite.RecipePropertiesFetcher;
import io.micronaut.starter.sdk.BuildTool;
import io.micronaut.starter.options.Language;
import io.micronaut.starter.options.Options;
Expand All @@ -46,15 +47,18 @@ public class ContextFactory {

private final DefaultCoordinateResolver coordinateResolver;
private final RecipeDependencyFetcher recipeDependencyFetcher;
private final RecipePropertiesFetcher recipePropertiesFetcher;

public ContextFactory(FeatureValidator featureValidator,
DefaultCoordinateResolver coordinateResolver,
ProjectNameValidator projectNameValidator,
RecipeDependencyFetcher recipeDependencyFetcher) {
RecipeDependencyFetcher recipeDependencyFetcher,
RecipePropertiesFetcher recipePropertiesFetcher) {
this.featureValidator = featureValidator;
this.coordinateResolver = coordinateResolver;
this.projectNameValidator = projectNameValidator;
this.recipeDependencyFetcher = recipeDependencyFetcher;
this.recipePropertiesFetcher = recipePropertiesFetcher;
}

public FeatureContext createFeatureContext(AvailableFeatures availableFeatures,
Expand Down Expand Up @@ -97,7 +101,7 @@ public GeneratorContext createGeneratorContext(Project project,

featureValidator.validatePostProcessing(featureContext.getOptions(), featureContext.getApplicationType(), featureList);

return new GeneratorContext(project, featureContext.getApplicationType(), featureContext.getOptions(), featureContext.getOperatingSystem(), featureList, coordinateResolver, recipeDependencyFetcher);
return new GeneratorContext(project, featureContext.getApplicationType(), featureContext.getOptions(), featureContext.getOperatingSystem(), featureList, coordinateResolver, recipeDependencyFetcher, recipePropertiesFetcher);
}

Language determineLanguage(Language language, Set<Feature> features) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.micronaut.starter.feature.config.Configuration;
import io.micronaut.starter.feature.other.template.markdownLink;
import io.micronaut.starter.openrewrite.RecipeDependencyFetcher;
import io.micronaut.starter.openrewrite.RecipePropertiesFetcher;
import io.micronaut.starter.sdk.BuildTool;
import io.micronaut.starter.options.JdkVersion;
import io.micronaut.starter.options.Language;
Expand Down Expand Up @@ -87,14 +88,16 @@ public class GeneratorContext implements DependencyContext {
private final Set<Profile> profiles = new HashSet<>();
private final Set<BuildPlugin> buildPlugins = new HashSet<>();
private final RecipeDependencyFetcher recipeDependencyFetcher;
private final RecipePropertiesFetcher recipePropertiesFetcher;

public GeneratorContext(Project project,
ApplicationType type,
Options options,
@Nullable OperatingSystem operatingSystem,
Set<Feature> features,
CoordinateResolver coordinateResolver,
RecipeDependencyFetcher recipeDependencyFetcher) {
RecipeDependencyFetcher recipeDependencyFetcher,
RecipePropertiesFetcher recipePropertiesFetcher) {
this.command = type;
this.project = project;
this.operatingSystem = operatingSystem;
Expand All @@ -111,6 +114,7 @@ public GeneratorContext(Project project,
this.coordinateResolver = coordinateResolver;
this.dependencyContext = new DependencyContextImpl(coordinateResolver);
this.recipeDependencyFetcher = recipeDependencyFetcher;
this.recipePropertiesFetcher = recipePropertiesFetcher;
}

/**
Expand Down Expand Up @@ -460,4 +464,13 @@ public void addDependenciesByRecipeName(String recipeName) {
}
}
}

public void addConfigurationByRecipeName(@NonNull String recipeName) {
Configuration config = getConfiguration();
recipePropertiesFetcher.findPropertiesByRecipeName(recipeName).ifPresent(properties -> {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
config.addNested(entry.getKey().toString(), entry.getValue());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.StringUtils;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.openrewrite.OpenRewriteFeature;
import io.micronaut.starter.sdk.dependency.Dependency;
import io.micronaut.starter.feature.FeatureContext;
import io.micronaut.starter.feature.logging.LiquibaseSlf4j;
Expand All @@ -29,7 +30,7 @@

@Requires(property = "micronaut.starter.feature.liquibase.enabled", value = StringUtils.TRUE, defaultValue = StringUtils.TRUE)
@Singleton
public class Liquibase implements MigrationFeature {
public class Liquibase implements MigrationFeature, OpenRewriteFeature {

public static final String NAME = "liquibase";

Expand Down Expand Up @@ -74,11 +75,9 @@ public void apply(GeneratorContext generatorContext) {
liquibaseChangelog.template()));
generatorContext.addTemplate("liquibaseSchema", new RockerTemplate("src/main/resources/db/changelog/01-schema.xml",
liquibaseSchema.template()));
generatorContext.addDependency(Dependency.builder()
.groupId("io.micronaut.liquibase")
.artifactId("micronaut-liquibase")
.compile());
generatorContext.getConfiguration().addNested(
"liquibase.datasources.default.change-log", "classpath:db/liquibase-changelog.xml");

generatorContext.addDependenciesByRecipeName(getRecipeName());
generatorContext.addConfigurationByRecipeName(getRecipeName());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.micronaut.starter.feature.build.maven.templates.pom
import io.micronaut.starter.fixture.ContextFixture
import io.micronaut.starter.fixture.ProjectFixture
import io.micronaut.starter.openrewrite.RecipeDependencyFetcher
import io.micronaut.starter.openrewrite.RecipePropertiesFetcher
import io.micronaut.starter.options.*
import io.micronaut.starter.sdk.BuildTool
import io.micronaut.starter.sdk.Project
Expand Down Expand Up @@ -139,7 +140,7 @@ class BuildBuilder implements ProjectFixture, ContextFixture {
}

GeneratorContext createGeneratorContextAndApplyFeatures(Options options, Features features, Project project, ApplicationType type) {
GeneratorContext ctx = new GeneratorContext(project, type, options, null, features.features, ctx.getBean(CoordinateResolver), ctx.getBean(RecipeDependencyFetcher))
GeneratorContext ctx = new GeneratorContext(project, type, options, null, features.features, ctx.getBean(CoordinateResolver), ctx.getBean(RecipeDependencyFetcher), ctx.getBean(RecipePropertiesFetcher))
ctx.applyFeatures()
ctx
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.micronaut.starter.application.ApplicationType
import io.micronaut.starter.application.OperatingSystem
import io.micronaut.starter.application.generator.GeneratorContext
import io.micronaut.starter.openrewrite.RecipeDependencyFetcher
import io.micronaut.starter.openrewrite.RecipePropertiesFetcher
import io.micronaut.starter.sdk.BuildTool
import io.micronaut.starter.sdk.dependency.Dependency
import io.micronaut.starter.sdk.dependency.DependencyCoordinate
Expand Down Expand Up @@ -90,7 +91,12 @@ class FeatureSpec extends BeanContextSpec {
List<Dependency> findAllByRecipeNameAndBuildTool(@NonNull String recipe, @NonNull BuildTool b) {
return Collections.emptyList()
}
}
}, new RecipePropertiesFetcher() {
@Override
Optional<Properties> findPropertiesByRecipeName(@NonNull String recipe) {
return Optional.empty()
}
}
)
commandCtx.applyFeatures()

Expand Down
1 change: 1 addition & 0 deletions starter-openrewrite-recipes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation(libs.rewrite.java.dependencies) {
exclude(group = "org.openrewrite", module = "rewrite-groovy")
}
implementation(libs.rewrite.properties)
testAnnotationProcessor(platform("io.micronaut.platform:micronaut-platform:${micronautVersion}"))
testAnnotationProcessor("io.micronaut:micronaut-inject-java")
testImplementation(platform("io.micronaut.platform:micronaut-platform:${micronautVersion}"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.micronaut.starter.openrewrite;

import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.NonNull;
import jakarta.inject.Singleton;
import org.openrewrite.Recipe;
import org.openrewrite.RecipeException;
import org.openrewrite.config.Environment;

import java.util.Optional;
import java.util.Properties;

import org.openrewrite.properties.AddProperty;

@Singleton
public class DefaultRecipePropertiesFetcher implements RecipePropertiesFetcher {
private final Environment env;

public DefaultRecipePropertiesFetcher(Environment env) {
this.env = env;
}

@Override
@NonNull
public Optional<Properties> findPropertiesByRecipeName(@NonNull String recipeName) {
try {
var recipe = env.activateRecipes(recipeName);
return findProperties(recipe);
} catch (RecipeException e) {
throw new ConfigurationException("Error activating recipe: " + recipeName, e);
}
}

private @NonNull Optional<Properties> findProperties(@NonNull Recipe recipe) {
Properties properties = new Properties();
for (Recipe r : recipe.getRecipeList()) {
if (r instanceof AddProperty addProperty) {
properties.put(addProperty.getProperty(), addProperty.getValue());
}
}
if (properties.isEmpty()) {
return Optional.empty();
}
return Optional.of(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2017-2022 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.openrewrite;

import io.micronaut.core.annotation.NonNull;
import java.util.Optional;
import java.util.Properties;

public interface RecipePropertiesFetcher {

@NonNull
Optional<Properties> findPropertiesByRecipeName(@NonNull String recipe);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: io.micronaut.starter.feature.liquibase
displayName: Adds Micronaut liquibase dependency in the compile classpath
recipeList:
- org.openrewrite.java.dependencies.AddDependency:
groupId: io.micronaut.liquibase
artifactId: micronaut-liquibase
configuration: implementation
scope: compile
- org.openrewrite.properties.AddProperty: #TODO create a recipe to add a property to a Micronaut configuration file
property: liquibase.datasources.default.change-log
value: classpath:db/liquibase-changelog.xml
---
type: specs.openrewrite.org/v1beta/recipe
name: io.micronaut.starter.feature.mockito
displayName: Add Mockito dependency in the test configuration
recipeList:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.micronaut.starter.openrewrite;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

@MicronautTest(startApplication = false)
class RecipePropertiesFetcherTest {
private static final String NAME = "io.micronaut.starter.feature.liquibase";
@Test
void testFetchProperties(RecipePropertiesFetcher fetcher) {
Optional<Properties> propertiesOptional = fetcher.findPropertiesByRecipeName(NAME);
assertTrue(propertiesOptional.isPresent());
Properties properties = propertiesOptional.get();
assertEquals("classpath:db/liquibase-changelog.xml", properties.get("liquibase.datasources.default.change-log"));
}
}

0 comments on commit d71a1e7

Please sign in to comment.