Skip to content

Commit

Permalink
Merge with ParentPomInsight
Browse files Browse the repository at this point in the history
  • Loading branch information
DidierLoiseau committed Oct 19, 2024
1 parent 4062676 commit ec60efb
Show file tree
Hide file tree
Showing 4 changed files with 755 additions and 700 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.table.ParentPomsInUse;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Parent;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.tree.Xml;

import static java.util.Collections.emptyList;
import static org.openrewrite.internal.StringUtils.matchesGlob;

@EqualsAndHashCode(callSuper = false)
Expand All @@ -52,6 +58,12 @@ public class ParentPomInsight extends Recipe {
@Nullable
String version;

@Option(displayName = "Recursive",
description = "Whether to search recursively through the parents. True by default.",
required = false)
@Nullable
Boolean recursive;

@Override
public String getDisplayName() {
return "Maven parent insight";
Expand Down Expand Up @@ -79,26 +91,42 @@ public Validated<Object> validate() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {
@Nullable
final VersionComparator versionComparator = version == null ? null : Semver.validate(version, null).getValue();

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);
if (isParentTag()) {
ResolvedPom resolvedPom = getResolutionResult().getPom();
String groupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(null));
String artifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(null));
if (matchesGlob(groupId, groupIdPattern) && matchesGlob(artifactId, artifactIdPattern)) {
String parentVersion = resolvedPom.getValue(tag.getChildValue("version").orElse(null));
if (version != null) {
if (!Semver.validate(version, null).getValue()
.isValid(null, parentVersion)) {
return t;
MavenResolutionResult mrr = getResolutionResult();
ResolvedPom resolvedPom = mrr.getPom();
MavenPomDownloader mpd = new MavenPomDownloader(mrr.getProjectPoms(), ctx, mrr.getMavenSettings(), mrr.getActiveProfiles());

Parent ancestor = mrr.getPom().getRequested().getParent();
String relativePath = tag.getChildValue("relativePath").orElse(null);
while (ancestor != null) {
String groupId = ancestor.getGroupId();
String artifactId = ancestor.getArtifactId();
if (matchesGlob(groupId, groupIdPattern) && matchesGlob(artifactId, artifactIdPattern)) {
String parentVersion = ancestor.getVersion();
if (versionComparator == null || versionComparator.isValid(null, parentVersion)) {
// Found a parent pom that matches the criteria
inUse.insertRow(ctx, new ParentPomsInUse.Row(
resolvedPom.getArtifactId(), groupId, artifactId, parentVersion, relativePath));
return SearchResult.found(t);
}
}
// Found a parent pom that matches the criteria
String relativePath = tag.getChildValue("relativePath").orElse(null);
inUse.insertRow(ctx, new ParentPomsInUse.Row(
resolvedPom.getArtifactId(), groupId, artifactId, parentVersion, relativePath));
return SearchResult.found(t);
if (Boolean.FALSE.equals(recursive)) {
return t;
}
try {
ResolvedPom ancestorPom = mpd.download(ancestor.getGav(), null, null, mrr.getPom().getRepositories())
.resolve(emptyList(), mpd, ctx);
ancestor = ancestorPom.getRequested().getParent();
relativePath = null;
} catch (MavenDownloadingException e) {
return e.warn(t);
}
}
}
return t;
Expand Down
Loading

0 comments on commit ec60efb

Please sign in to comment.