Skip to content
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

Move project matcher to MavenVisitor #4675

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.RemoveContentVisitor;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.Optional;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddProfile extends Recipe {
private static final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");

@Option(displayName = "id",
description = "The profile id.",
Expand Down Expand Up @@ -81,7 +79,7 @@ private class AddProfileVisitor extends MavenIsoVisitor<ExecutionContext> {
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);

if (PROJECT_MATCHER.matches(getCursor())) {
if (isProjectTag()) {
Optional<Xml.Tag> maybeProfiles = t.getChild("profiles");
Xml.Tag profiles;
if (maybeProfiles.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.Optional;
Expand All @@ -38,7 +37,6 @@
@Value
@EqualsAndHashCode(callSuper = false)
public class ChangePackaging extends Recipe {
private static final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");

@Option(displayName = "Group",
description = "The groupId of the project whose packaging should be changed. Accepts glob patterns.",
Expand Down Expand Up @@ -102,7 +100,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);
if (PROJECT_MATCHER.matches(getCursor())) {
if (isProjectTag()) {
Optional<Xml.Tag> maybePackaging = t.getChild("packaging");
if (!maybePackaging.isPresent() || oldPackaging == null || oldPackaging.equals(maybePackaging.get().getValue().orElse(null))) {
if (packaging == null || "jar".equals(packaging)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.Arrays;
Expand Down Expand Up @@ -83,13 +82,11 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {

return new MavenIsoVisitor<ExecutionContext>() {
private final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);

if (PROJECT_MATCHER.matches(getCursor())) {
if (isProjectTag()) {
ResolvedPom resolvedPom = getResolutionResult().getPom();

if (matchesGlob(resolvedPom.getValue(t.getChildValue("groupId").orElse(null)), groupId) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openrewrite.maven.tree.GroupArtifact;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.xml.ChangeTagValue;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.*;
Expand Down Expand Up @@ -81,15 +80,14 @@ public Map<GroupArtifact, String> getInitialValue(ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Map<GroupArtifact, String> acc) {
final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");
final Pattern SEMVER_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)\\.?(\\d+)?(-.+)?$");

return new MavenIsoVisitor<ExecutionContext>() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);

if (!PROJECT_MATCHER.matches(getCursor())) {
if (!isProjectTag()) {
return t;
}
ResolvedPom resolvedPom = getResolutionResult().getPom();
Expand Down Expand Up @@ -158,14 +156,11 @@ private String incrementSemverDigit(String oldVersion) {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Map<GroupArtifact, String> acc) {
return new MavenIsoVisitor<ExecutionContext>() {
final XPathMatcher PARENT_MATCHER = new XPathMatcher("/project/parent");
final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);

if ((!(PROJECT_MATCHER.matches(getCursor()) || PARENT_MATCHER.matches(getCursor()))) ||
if (!(isProjectTag() || isParentTag()) ||
t.getMarkers().findFirst(AlreadyIncremented.class).isPresent()) {
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class MavenVisitor<P> extends XmlVisitor<P> {
static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin");
static final XPathMatcher MANAGED_PLUGIN_MATCHER = new XPathMatcher("//pluginManagement/plugins/plugin");
static final XPathMatcher PARENT_MATCHER = new XPathMatcher("/project/parent");
static final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");

private transient Xml.@Nullable Document document;

Expand Down Expand Up @@ -259,6 +260,10 @@ public boolean isParentTag() {
return isTag("parent") && PARENT_MATCHER.matches(getCursor());
}

public boolean isProjectTag() {
return isTag("project") && PROJECT_MATCHER.matches(getCursor());
}

private boolean isTag(String name) {
// `XPathMatcher` is still a bit expensive
return getCursor().getValue() instanceof Xml.Tag && name.equals(getCursor().<Xml.Tag>getValue().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.nio.file.Path;
Expand Down Expand Up @@ -202,7 +201,6 @@ private void storeParentPomProperty(
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator accumulator) {
return new MavenIsoVisitor<ExecutionContext>() {
private final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");
private final VersionComparator versionComparator =
requireNonNull(Semver.validate(newVersion, versionPattern).getValue());

Expand Down Expand Up @@ -242,7 +240,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
return e.warn(t);
}

if (t != tag && PROJECT_MATCHER.matches(getCursor())) {
if (t != tag && isProjectTag()) {
maybeUpdateModel();
doAfterVisit(new RemoveRedundantDependencyVersions(groupId, artifactId, (RemoveRedundantDependencyVersions.Comparator) null, null).getVisitor());
}
Expand Down