Skip to content

Commit

Permalink
Reduce appearance of duplicate entries in dependency list report.
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 27, 2024
1 parent 52401d9 commit d009531
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
42 changes: 31 additions & 11 deletions src/main/java/org/openrewrite/java/dependencies/DependencyList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.java.dependencies.table.DependencyListReport;
import org.openrewrite.marker.Markers;
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;

import java.util.HashSet;
import java.util.Set;

@Value
@EqualsAndHashCode(callSuper = false)
Expand All @@ -40,10 +45,16 @@ public class DependencyList extends Recipe {

@Option(displayName = "Include transitive dependencies",
description = "Whether or not to include transitive dependencies in the report. " +
"Defaults to including only direct dependencies.",
"Defaults to including only direct dependencies.",
example = "true")
boolean includeTransitive;

/**
* Freestanding gradle script plugins get assigned the same GradleProject marker with the build script in the project.
* Keep track of the ones which have been seen to minimize duplicate entries in the report.
*/
transient Set<GroupArtifactVersion> seenGradleProjects = new HashSet<>();

@Override
public String getDisplayName() {
return "Dependency report";
Expand All @@ -52,7 +63,7 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Emits a data table detailing all Gradle and Maven dependencies." +
"This recipe makes no changes to any source file.";
"This recipe makes no changes to any source file.";
}

@Override
Expand All @@ -64,43 +75,52 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return null;
}
Markers m = tree.getMarkers();
m.findFirst(GradleProject.class).ifPresent(gradle -> {
Set<ResolvedGroupArtifactVersion> seen = new HashSet<>();
m.findFirst(GradleProject.class)
.filter(gradle -> seenGradleProjects.add(new GroupArtifactVersion(gradle.getGroup(), gradle.getName(), gradle.getVersion())))
.ifPresent(gradle -> {
GradleDependencyConfiguration conf = gradle.getConfiguration(scope.asGradleConfigurationName());
if (conf != null) {
for (ResolvedDependency dep : conf.getResolved()) {
insertDependency(ctx, gradle, dep, true);
insertDependency(ctx, gradle, seen, dep, true);
}
}
});
m.findFirst(MavenResolutionResult.class).ifPresent(maven -> {
for (ResolvedDependency dep : maven.getDependencies().get(scope.asMavenScope())) {
insertDependency(ctx, maven, dep, true);
insertDependency(ctx, maven, seen, dep, true);
}
});
return tree;
}
};
}

private void insertDependency(ExecutionContext ctx, GradleProject gradle, ResolvedDependency dep, boolean direct) {
private void insertDependency(ExecutionContext ctx, GradleProject gradle, Set<ResolvedGroupArtifactVersion> seen, ResolvedDependency dep, boolean direct) {
if (!seen.add(dep.getGav())) {
return;
}
report.insertRow(ctx, new DependencyListReport.Row(
"Gradle",
"",
gradle.getGroup(),
gradle.getName(),
"",
gradle.getVersion(),
dep.getGroupId(),
dep.getArtifactId(),
dep.getVersion(),
direct
));
if (includeTransitive) {
for (ResolvedDependency transitive : dep.getDependencies()) {
insertDependency(ctx, gradle, transitive, false);
insertDependency(ctx, gradle, seen, transitive, false);
}
}
}

private void insertDependency(ExecutionContext ctx, MavenResolutionResult maven, ResolvedDependency dep, boolean direct) {
private void insertDependency(ExecutionContext ctx, MavenResolutionResult maven, Set<ResolvedGroupArtifactVersion> seen, ResolvedDependency dep, boolean direct) {
if (!seen.add(dep.getGav())) {
return;
}
report.insertRow(ctx, new DependencyListReport.Row(
"Maven",
maven.getPom().getGroupId(),
Expand All @@ -113,7 +133,7 @@ private void insertDependency(ExecutionContext ctx, MavenResolutionResult maven,
));
if (includeTransitive) {
for (ResolvedDependency transitive : dep.getDependencies()) {
insertDependency(ctx, maven, transitive, false);
insertDependency(ctx, maven, seen, transitive, false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ void basic() {
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation('org.openrewrite:rewrite-core:7.39.0')
}
Expand Down

0 comments on commit d009531

Please sign in to comment.