Skip to content

Commit

Permalink
Refactored to use new Maven resolution model
Browse files Browse the repository at this point in the history
  • Loading branch information
tkvangorder committed Feb 9, 2022
1 parent 8bedb76 commit 25b6066
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
package org.openrewrite.java.quarkus;

import org.openrewrite.ExecutionContext;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.tree.Xml;

import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

public class AddQuarkusMavenPluginGoalVisitor extends MavenVisitor {
public class AddQuarkusMavenPluginGoalVisitor extends MavenIsoVisitor<ExecutionContext> {
private static final Predicate<? super Xml.Tag> TAG_KEY_NAME_MATCHES = tag -> "goal".equals(tag.getName());
private static final Predicate<? super Xml.Tag> TAG_HAS_CONTENT = tag -> tag.getContent() != null && tag.getContent().size() == 1;
private static final Predicate<? super Xml.Tag> TAG_CONTENT_IS_CHAR_DATA = tag -> tag.getContent().get(0) instanceof Xml.CharData;
Expand All @@ -38,14 +38,15 @@ public AddQuarkusMavenPluginGoalVisitor(String goalName) {
this.goalName = goalName;
}


@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
/**
public Xml.Document visitDocument(Xml.Document document, ExecutionContext executionContext) {
/*
* There must be room for making this better. It feels like this shouldn't need so much ceremony around building up a path. Or what would
* really be helpful is having this walk a configured (fully-defined) path, and try to add any new nodes if none exist.
* fixme
*/
FindPlugin.find(maven, "io.quarkus", "quarkus-maven-plugin").forEach(plugin -> {
FindPlugin.find(document, "io.quarkus", "quarkus-maven-plugin").forEach(plugin -> {
Optional<Xml.Tag> maybeExecutions = plugin.getChild("executions");
if (!maybeExecutions.isPresent()) {
Xml.Tag executionsTag = Xml.Tag.build("<executions/>");
Expand Down Expand Up @@ -81,7 +82,6 @@ public Maven visitMaven(Maven maven, ExecutionContext ctx) {
}
}
});

return super.visitMaven(maven, ctx);
return super.visitDocument(document, executionContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.tree.Xml;
Expand All @@ -47,14 +47,14 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new ConfigureQuarkusMavenPluginWithReasonableDefaultsVisitor();
}

private static class ConfigureQuarkusMavenPluginWithReasonableDefaultsVisitor extends MavenVisitor {
private static class ConfigureQuarkusMavenPluginWithReasonableDefaultsVisitor extends MavenIsoVisitor<ExecutionContext> {
@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
public Xml.Document visitDocument(Xml.Document document, ExecutionContext executionContext) {
doAfterVisit(new AddQuarkusMavenPluginGoalVisitor("build"));
doAfterVisit(new AddQuarkusMavenPluginGoalVisitor("generate-code"));
doAfterVisit(new AddQuarkusMavenPluginGoalVisitor("generate-code-tests"));

FindPlugin.find(maven, "io.quarkus", "quarkus-maven-plugin").forEach(plugin -> {
FindPlugin.find(document, "io.quarkus", "quarkus-maven-plugin").forEach(plugin -> {
Optional<Xml.Tag> maybeExtensions = plugin.getChild("extensions");
if (!maybeExtensions.isPresent()) {
Xml.Tag extensionsTag = Xml.Tag.build("<extensions>true</extensions>");
Expand All @@ -64,8 +64,7 @@ public Maven visitMaven(Maven maven, ExecutionContext ctx) {
doAfterVisit(new ChangeTagValueVisitor<>(maybeExtensions.get(), "true"));
}
});

return super.visitMaven(maven, ctx);
return document;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.RemoveContentVisitor;
import org.openrewrite.xml.search.FindTags;
Expand All @@ -43,32 +43,28 @@ public String getDescription() {

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new MigrateQuarkusMavenPluginNativeImageGoalVisitor();
}

private static class MigrateQuarkusMavenPluginNativeImageGoalVisitor extends MavenVisitor {
@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
FindPlugin.find(maven, "io.quarkus", "quarkus-maven-plugin").forEach(plugin ->
FindTags.find(plugin, "//executions/execution/goals/goal").forEach(goal -> {
if (goal.getContent() != null && goal.getContent().size() == 1 && goal.getContent().get(0) instanceof Xml.CharData) {
Xml.CharData existingValue = (Xml.CharData) goal.getContent().get(0);
if ("native-image".equalsIgnoreCase(existingValue.getText())) {
doAfterVisit(new RemoveContentVisitor<>(goal, true));
doAfterVisit(new AddQuarkusPackageTypePropertyToNativeProfile());
return new MavenIsoVisitor<ExecutionContext>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
FindPlugin.find(document, "io.quarkus", "quarkus-maven-plugin").forEach(plugin ->
FindTags.find(plugin, "//executions/execution/goals/goal").forEach(goal -> {
if (goal.getContent() != null && goal.getContent().size() == 1 && goal.getContent().get(0) instanceof Xml.CharData) {
Xml.CharData existingValue = (Xml.CharData) goal.getContent().get(0);
if ("native-image".equalsIgnoreCase(existingValue.getText())) {
doAfterVisit(new RemoveContentVisitor<>(goal, true));
doAfterVisit(new AddQuarkusPackageTypePropertyToNativeProfile());
}
}
}
}));

return super.visitMaven(maven, ctx);
}

}));
return super.visitDocument(document, ctx);
}
};
}

private static class AddQuarkusPackageTypePropertyToNativeProfile extends MavenVisitor {
private static class AddQuarkusPackageTypePropertyToNativeProfile extends MavenIsoVisitor<ExecutionContext> {
@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
FindTags.find(maven, "/project/profiles/profile").forEach(profile -> {
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
FindTags.find(document, "/project/profiles/profile").forEach(profile -> {
Optional<Xml.Tag> maybeId = profile.getChild("id");
if (maybeId.isPresent()) {
String profileId = maybeId.get().getValue().orElse(null);
Expand All @@ -95,9 +91,8 @@ public Maven visitMaven(Maven maven, ExecutionContext ctx) {
}
});

return super.visitMaven(maven, ctx);
return super.visitDocument(document, ctx);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.RemovePlugin;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.xml.tree.Xml;

public class RemoveAvroMavenPlugin extends Recipe {
@Override
Expand All @@ -37,28 +37,26 @@ public String getDescription() {

@Override
protected @Nullable TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new MavenVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {

@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
if (!FindPlugin.find(maven, "io.quarkus", "quarkus-maven-plugin").isEmpty()) {
maven = maven.withMarkers(maven.getMarkers().searchResult());
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
if (!FindPlugin.find(document, "io.quarkus", "quarkus-maven-plugin").isEmpty()) {
document = document.withMarkers(document.getMarkers().searchResult());
}
return super.visitMaven(maven, ctx);
return super.visitDocument(document, ctx);
}
};
}

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new RemoveAvroMavenPluginVisitor();
}

private static class RemoveAvroMavenPluginVisitor extends MavenVisitor {
@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
doAfterVisit(new RemovePlugin("org.apache.avro", "avro-maven-plugin"));
return super.visitMaven(maven, ctx);
}
return new MavenIsoVisitor<ExecutionContext>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
doAfterVisit(new RemovePlugin("org.apache.avro", "avro-maven-plugin"));
return document;
}
};
}

}

0 comments on commit 25b6066

Please sign in to comment.