From 5cb394544e43a3b1e4d58229fd41f363bc27843e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 12 Dec 2024 18:57:58 +0100 Subject: [PATCH] Drop unused GraphvizResolutionEventListener & dependency (#4778) * Drop unused GraphvizResolutionEventListener & dependency * Drop inadvertent use of logger --- rewrite-maven/build.gradle.kts | 3 - rewrite-maven/dependency-viz.kt | 125 ---------- .../maven/internal/VersionRequirement.java | 9 +- .../tree/GraphvizResolutionEventListener.java | 216 ------------------ 4 files changed, 3 insertions(+), 350 deletions(-) delete mode 100644 rewrite-maven/dependency-viz.kt delete mode 100644 rewrite-maven/src/main/java/org/openrewrite/maven/tree/GraphvizResolutionEventListener.java diff --git a/rewrite-maven/build.gradle.kts b/rewrite-maven/build.gradle.kts index 64b4be8bef4..d6efe37c616 100755 --- a/rewrite-maven/build.gradle.kts +++ b/rewrite-maven/build.gradle.kts @@ -26,8 +26,6 @@ dependencies { // needed by AddDependency implementation(project(":rewrite-java")) - compileOnly("guru.nidi:graphviz-java:latest.release") - compileOnly("org.rocksdb:rocksdbjni:latest.release") compileOnly(project(":rewrite-yaml")) implementation(project(":rewrite-properties")) @@ -41,7 +39,6 @@ dependencies { testImplementation("com.squareup.okhttp3:okhttp-tls:4.+") testImplementation("com.squareup.okio:okio-jvm:3.9.1") testImplementation("org.mapdb:mapdb:latest.release") - testImplementation("guru.nidi:graphviz-java:latest.release") testRuntimeOnly("org.mapdb:mapdb:latest.release") testRuntimeOnly(project(":rewrite-java-17")) diff --git a/rewrite-maven/dependency-viz.kt b/rewrite-maven/dependency-viz.kt deleted file mode 100644 index b9ee2bd1a84..00000000000 --- a/rewrite-maven/dependency-viz.kt +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2021 the original author or 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 org.openrewrite.maven - -import guru.nidi.graphviz.engine.Format -import org.openrewrite.ExecutionContext -import org.openrewrite.InMemoryExecutionContext -import org.openrewrite.maven.tree.GraphvizResolutionEventListener -import org.openrewrite.maven.tree.MavenResolutionResult -import org.openrewrite.maven.tree.Scope -import java.net.URL -import java.nio.file.Path -import java.nio.file.Paths -import kotlin.io.path.exists - -fun visualize( - ctx: ExecutionContext = InMemoryExecutionContext { t -> throw t }, - scope: Scope = Scope.Compile, - outputFileLocation: Path = Paths.get("diagrams"), - outputFilePrefix: String = "maven", - showProperties: Boolean = false, - showManagedDependencies: Boolean = false, - runnable: () -> T -) { - val viz = GraphvizResolutionEventListener(scope, showProperties, showManagedDependencies) - MavenExecutionContextView(ctx).setResolutionListener(viz) - try { - runnable() - } finally { - viz.graphviz().render(Format.DOT).toFile(outputFileLocation.resolve("${outputFilePrefix}.dot").toFile()) - viz.graphviz() - .postProcessor { result, _, _ -> - result.mapString { svg -> - svg.replace("font-family=\"Times,serif\" ", "") - .replace("a xlink:href", "a target=\"_blank\" xlink:href") - } - } - .render(Format.SVG) - .toFile(outputFileLocation.resolve("${outputFilePrefix}.svg").toFile()) - - val panZoom = outputFileLocation.resolve("svg-pan-zoom.min.js") - if (!panZoom.exists()) { - panZoom.toFile().writeText( - URL("https://raw.githubusercontent.com/bumbu/svg-pan-zoom/master/dist/svg-pan-zoom.min.js") - .openStream().bufferedReader().readText() - ) - } - - outputFileLocation.resolve("${outputFilePrefix}.html").toFile().writeText( - //language=html - """ - - - - - - - - - - - """.trimIndent() - ) - } -} - -fun visualize( - gav: String, - ctx: ExecutionContext = InMemoryExecutionContext { t -> throw t }, - scope: Scope = Scope.Compile, - showProperties: Boolean = false, - showManagedDependencies: Boolean = false -) { - visualize(ctx, scope, Paths.get("diagrams"), gav.replace(':', '_'), showProperties, showManagedDependencies) { - parse(gav, ctx) - } -} - -fun parse(gav: String, ctx: ExecutionContext = InMemoryExecutionContext { t -> throw t }): MavenResolutionResult { - val (group, artifact, version) = gav.split(":") - val maven = MavenParser.builder().build().parse( - ctx, """ - - org.openrewrite - dependency-viz - 0.0.1 - - - ${group} - ${artifact} - ${version} - - - - """ - )[0] - - return maven.mavenResolutionResult() -} diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/VersionRequirement.java b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/VersionRequirement.java index f488d30f79b..ee67ff652f4 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/VersionRequirement.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/VersionRequirement.java @@ -30,8 +30,6 @@ import org.openrewrite.maven.tree.MavenMetadata; import org.openrewrite.maven.tree.MavenRepository; import org.openrewrite.maven.tree.Version; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Iterator; import java.util.List; @@ -41,7 +39,6 @@ @RequiredArgsConstructor public class VersionRequirement { - private static final Logger logger = LoggerFactory.getLogger(VersionRequirement.class); @Nullable private final VersionRequirement nearer; @@ -100,7 +97,7 @@ static VersionSpec build(String requested, boolean direct) { CharStreams.fromString(requested)))); parser.removeErrorListeners(); - parser.addErrorListener(new LoggingErrorListener()); + parser.addErrorListener(new PrintingErrorListener()); return new VersionRangeParserBaseVisitor() { @Override @@ -287,11 +284,11 @@ public String toString() { }); } - private static class LoggingErrorListener extends BaseErrorListener { + private static class PrintingErrorListener extends BaseErrorListener { @Override public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { - logger.warn("Syntax error at line {}:{} {}", line, charPositionInLine, msg); + System.out.printf("Syntax error at line %d:%d %s%n", line, charPositionInLine, msg); } } } diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/GraphvizResolutionEventListener.java b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/GraphvizResolutionEventListener.java deleted file mode 100644 index cf94887334e..00000000000 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/GraphvizResolutionEventListener.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright 2021 the original author or 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 org.openrewrite.maven.tree; - -import guru.nidi.graphviz.attribute.Color; -import guru.nidi.graphviz.attribute.Label; -import guru.nidi.graphviz.attribute.Shape; -import guru.nidi.graphviz.attribute.Style; -import guru.nidi.graphviz.engine.Graphviz; -import guru.nidi.graphviz.model.Link; -import guru.nidi.graphviz.model.MutableGraph; -import guru.nidi.graphviz.model.MutableNode; -import lombok.RequiredArgsConstructor; -import org.jspecify.annotations.Nullable; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.stream.Collectors; - -import static guru.nidi.graphviz.model.Factory.mutGraph; -import static guru.nidi.graphviz.model.Factory.mutNode; -import static guru.nidi.graphviz.model.Link.to; -import static java.util.Collections.emptySet; - -@RequiredArgsConstructor -public class GraphvizResolutionEventListener implements ResolutionEventListener { - public static final String GRAPH_NAME = "resolution"; - - private final Map propToNode = new HashMap<>(); - private final Map pomToNode = new HashMap<>(); - private final Map> alreadySeen = new HashMap<>(); - - private MutableGraph g = mutGraph(GRAPH_NAME).setDirected(true); - - private final Scope scope; - private final boolean showProperties; - private final boolean showManagedDependencies; - - private int parentLinks; - private int dependencyLinks; - private int managedDependencies; - - @Override - public void clear() { - g = mutGraph(GRAPH_NAME).setDirected(true); - propToNode.clear(); - pomToNode.clear(); - alreadySeen.clear(); - parentLinks = 0; - dependencyLinks = 0; - managedDependencies = 0; - } - - @Override - public void parent(Pom parent, Pom containing) { - if (alreadySeen.getOrDefault(simplifyGav(containing.getGav()), emptySet()).contains(simplifyGav(parent.getGav()))) { - return; - } - alreadySeen.computeIfAbsent(simplifyGav(containing.getGav()), g -> new HashSet<>()).add(simplifyGav(parent.getGav())); - - Link link = to(gavNode(parent.getGav()).add(Style.FILLED, Color.rgb("deefee"))).with(Style.DASHED); - if (parentLinks++ == 0) { - link = link.with(Label.of("parent")); - } - gavNode(containing.getGav()).addLink(link); - } - - @Override - public void dependency(Scope scope, ResolvedDependency resolvedDependency, ResolvedPom containing) { - if (scope != this.scope) { - return; - } - - Link link = to(gavNode(resolvedDependency.getGav()).add(Style.FILLED, Color.rgb("e6eaff"))); - if (dependencyLinks++ == 0) { - link = link.with(Label.of("dependency")); - } - gavNode(containing.getGav()).addLink(link); - } - - @Override - public void property(String key, String value, Pom containing) { - if (!showProperties) { - return; - } - gavNode(containing.getGav()).addLink(propNode(key, value)); - } - - @Override - public void dependencyManagement(ManagedDependency dependencyManagement, Pom containing) { - if (!showManagedDependencies) { - return; - } - GroupArtifactVersion gav = new GroupArtifactVersion(dependencyManagement.getGroupId(), dependencyManagement.getArtifactId(), dependencyManagement.getVersion()); - - if (alreadySeen.getOrDefault(simplifyGav(containing.getGav()), emptySet()).contains(gav)) { - return; - } - alreadySeen.computeIfAbsent(simplifyGav(containing.getGav()), g -> new HashSet<>()).add(gav); - - Link link = to(dmNode(gav).add(Style.FILLED, Color.rgb("d6d6de"))); - if (managedDependencies++ == 0) { - link = link.with(Label.of("dependencyManagement")); - } - gavNode(containing.getGav()).addLink(link); - } - - @Override - public void downloadError(GroupArtifactVersion gav, List attemptedUris, @Nullable Pom containing) { - Link link = to(gavNode(gav).add(Style.FILLED, Color.rgb("ff1947"))) - .with(Label.of("error")); - if(containing != null) { - gavNode(containing.getGav()) - .addLink(link); - } - } - - @Override - public void bomImport(ResolvedGroupArtifactVersion gav, Pom containing) { - if (alreadySeen.getOrDefault(simplifyGav(containing.getGav()), emptySet()).contains(simplifyGav(gav))) { - return; - } - alreadySeen.computeIfAbsent(simplifyGav(containing.getGav()), g -> new HashSet<>()).add(simplifyGav(gav)); - - Link link = to(gavNode(gav).add(Style.FILLED, Color.rgb("e6eaff"))); - if (dependencyLinks++ == 0) { - link = link.with(Label.of("dependency")); - } - gavNode(containing.getGav()).addLink(link); - } - - @SuppressWarnings("unused") - public Graphviz graphviz() { - return Graphviz.fromGraph(g); - } - - private GroupArtifactVersion simplifyGav(ResolvedGroupArtifactVersion gav) { - return new GroupArtifactVersion(gav.getGroupId(), gav.getArtifactId(), - gav.getDatedSnapshotVersion() == null ? gav.getVersion() : gav.getDatedSnapshotVersion()); - } - - private MutableNode gavNode(ResolvedGroupArtifactVersion gav) { - return gavNode(simplifyGav(gav)); - } - - private MutableNode gavNode(GroupArtifactVersion gav) { - return pomToNode.computeIfAbsent(gav, ignored -> { - MutableNode node = mutNode(Label.lines(gav.getGroupId(), gav.getArtifactId(), gav.getVersion())) - .add(Shape.RECTANGLE, Style.FILLED, Color.rgb("f9a91b")); - String url = gavUrl(gav); - if (url != null) { - node = node.add("URL", url); - } - g.add(node); - return node; - }); - } - - private MutableNode dmNode(GroupArtifactVersion gav) { - return pomToNode.computeIfAbsent(gav, ignored -> { - MutableNode node = mutNode(Label.lines(gav.getGroupId(), gav.getArtifactId(), gav.getVersion())).add(Shape.RECTANGLE); - String url = gavUrl(gav); - if (url != null) { - node = node.add("URL", url); - } - g.add(node); - return node; - }); - } - - private MutableNode propNode(String key, String value) { - return propToNode.computeIfAbsent(key + "=" + value, ignored -> { - MutableNode node = mutNode(Label.lines(key, value)); - g.add(node); - return node; - }); - } - - private @Nullable String gavUrl(GroupArtifactVersion gav) { - if (gav.getGroupId() == null || gav.getArtifactId() == null || gav.getVersion() == null) { - return null; - } - try { - return "https://repo1.maven.org/maven2/" + - Arrays.stream(gav.getGroupId().split("\\.")) - .map(g -> { - try { - return URLEncoder.encode(g, StandardCharsets.UTF_8.name()); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - }) - .collect(Collectors.joining("/")) + '/' + - URLEncoder.encode(gav.getArtifactId(), StandardCharsets.UTF_8.name()) + '/' + - URLEncoder.encode(gav.getVersion(), StandardCharsets.UTF_8.name()) + '/' + - URLEncoder.encode(gav.getArtifactId() + '-' + gav.getVersion() + ".pom", StandardCharsets.UTF_8.name()); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } -}