From e19794b4a75a28dbba7cd87b36b3ce6ea0ef82f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 22 Jan 2025 09:35:01 +0100 Subject: [PATCH] Add a markdown builder to simplify common tasks and unify generation --- .../tycho/baseline/DependencyCheckMojo.java | 27 +++------ .../eclipse/tycho/cleancode/CleanUpMojo.java | 22 +++---- .../tycho/cleancode/CleanupResult.java | 4 ++ .../eclipse/tycho/cleancode/QuickFixMojo.java | 31 +++------- .../tycho/cleancode/QuickFixResult.java | 4 ++ .../eclipse/tycho/core/MarkdownBuilder.java | 60 +++++++++++++++++++ 6 files changed, 93 insertions(+), 55 deletions(-) create mode 100644 tycho-core/src/main/java/org/eclipse/tycho/core/MarkdownBuilder.java diff --git a/tycho-baseline-plugin/src/main/java/org/eclipse/tycho/baseline/DependencyCheckMojo.java b/tycho-baseline-plugin/src/main/java/org/eclipse/tycho/baseline/DependencyCheckMojo.java index eb3b60d243..5209769441 100644 --- a/tycho-baseline-plugin/src/main/java/org/eclipse/tycho/baseline/DependencyCheckMojo.java +++ b/tycho-baseline-plugin/src/main/java/org/eclipse/tycho/baseline/DependencyCheckMojo.java @@ -14,7 +14,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; @@ -53,6 +52,7 @@ import org.eclipse.tycho.baseline.analyze.DependencyAnalyzer; import org.eclipse.tycho.baseline.analyze.JrtClasses; import org.eclipse.tycho.baseline.analyze.MethodSignature; +import org.eclipse.tycho.core.MarkdownBuilder; import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.maven.OSGiJavaToolchain; import org.eclipse.tycho.core.maven.ToolchainProvider; @@ -139,7 +139,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { JrtClasses jrtClassResolver = getJRTClassResolver(); List usages = DependencyAnalyzer.analyzeUsage(file, jrtClassResolver); if (usages.isEmpty()) { - writeReport("No usages detected"); return; } Collection units = artifacts.getInstallableUnits(); @@ -271,10 +270,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } if (dependencyProblems.isEmpty()) { - writeReport("No Version Problems detected!"); return; } - List results = new ArrayList<>(); + if (applySuggestions) { + applyLowerBounds(packageWithError, lowestPackageVersion); + } + MarkdownBuilder results = new MarkdownBuilder(reportFileName); for (DependencyVersionProblem problem : dependencyProblems) { Collection references = problem.references(); String message; @@ -322,22 +323,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { log.info(suggestion); results.add(suggestion); } - } - if (results.isEmpty()) { - return; - } - writeReport(results.stream().collect(Collectors.joining(System.lineSeparator()))); - if (applySuggestions) { - applyLowerBounds(packageWithError, lowestPackageVersion); - } - } - private void writeReport(String report) throws MojoFailureException { - try { - Files.writeString(reportFileName.toPath(), report); - } catch (IOException e) { - throw new MojoFailureException(e); } + results.write(); } private String getMethodRef(MethodSignature mthd) { @@ -349,6 +337,9 @@ private String getMethodRef(MethodSignature mthd) { private void applyLowerBounds(Set packageWithError, Map lowestPackageVersion) throws MojoFailureException { + if (packageWithError.isEmpty()) { + return; + } try { MutableBundleManifest manifest = MutableBundleManifest.read(manifestFile); Map exportedPackagesVersion = manifest.getExportedPackagesVersion(); diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java index c76a103bed..03601b0bdd 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java @@ -13,18 +13,14 @@ package org.eclipse.tycho.cleancode; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.eclipse.tycho.core.MarkdownBuilder; import org.eclipse.tycho.eclipsebuild.AbstractEclipseBuildMojo; import org.eclipse.tycho.model.project.EclipseProject; @@ -62,17 +58,15 @@ protected CleanUp createExecutable() { @Override protected void handleResult(CleanupResult result) throws MojoFailureException { - List results = new ArrayList<>(); - results.add("The following cleanups where applied:"); + if (result.isEmpty()) { + return; + } + MarkdownBuilder builder = new MarkdownBuilder(reportFileName); + builder.add("The following cleanups where applied:"); result.cleanups().forEach(cleanup -> { - results.add("- " + cleanup); + builder.addListItem(cleanup); }); - try { - Files.writeString(reportFileName.toPath(), - results.stream().collect(Collectors.joining(System.lineSeparator()))); - } catch (IOException e) { - throw new MojoFailureException(e); - } + builder.write(); } @Override diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanupResult.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanupResult.java index 11b2d45623..20d700a16a 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanupResult.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanupResult.java @@ -30,4 +30,8 @@ public Stream cleanups() { return this.cleanups.stream(); } + public boolean isEmpty() { + return cleanups.isEmpty(); + } + } diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixMojo.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixMojo.java index 5b4f5b96fc..d26f8e9c26 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixMojo.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixMojo.java @@ -13,17 +13,14 @@ package org.eclipse.tycho.cleancode; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.eclipse.tycho.core.MarkdownBuilder; import org.eclipse.tycho.eclipsebuild.AbstractEclipseBuildMojo; @Mojo(name = "quickfix", defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME) @@ -34,26 +31,14 @@ public class QuickFixMojo extends AbstractEclipseBuildMojo { @Override protected void handleResult(QuickFixResult result) throws MojoFailureException { - List results = new ArrayList<>(); - if (result.getMarkers() == 0) { - results.add("Project is clean!"); - } else { - List fixes = result.fixes().toList(); - if (fixes.isEmpty()) { - results.add("Nothing has been resolved in this project."); - } else { - results.add("The following " + (fixes.size() > 0 ? "warnings" : "warning") + " has been resolved:"); - fixes.forEach(fix -> { - results.add("- " + fix); - }); - } - } - try { - Files.writeString(reportFileName.toPath(), - results.stream().collect(Collectors.joining(System.lineSeparator()))); - } catch (IOException e) { - throw new MojoFailureException(e); + if (result.isEmpty()) { + return; } + MarkdownBuilder builder = new MarkdownBuilder(reportFileName); + List fixes = result.fixes().toList(); + builder.add("The following " + (fixes.size() > 0 ? "warnings" : "warning") + " has been resolved:"); + fixes.forEach(fix -> builder.addListItem(fix)); + builder.write(); } @Override diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixResult.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixResult.java index 8bacea8902..4e59a0851c 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixResult.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/QuickFixResult.java @@ -39,4 +39,8 @@ public int getMarkers() { return markers; } + public boolean isEmpty() { + return fixed.isEmpty(); + } + } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/MarkdownBuilder.java b/tycho-core/src/main/java/org/eclipse/tycho/core/MarkdownBuilder.java new file mode 100644 index 0000000000..1d60a70b02 --- /dev/null +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/MarkdownBuilder.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2025 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.core; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.maven.plugin.MojoFailureException; + +public class MarkdownBuilder { + + private final List lines = new ArrayList<>(); + private Path output; + + public MarkdownBuilder(File output) { + this(output == null ? (Path) null : output.toPath()); + } + + public MarkdownBuilder(Path output) { + this.output = output; + } + + public MarkdownBuilder add(String string) { + lines.add(string); + return this; + } + + public MarkdownBuilder addListItem(String item) { + lines.add("- " + item); + return this; + } + + public void write() throws MojoFailureException { + if (output == null) { + return; + } + try { + Files.createDirectories(output.getParent()); + Files.writeString(output, lines.stream().collect(Collectors.joining(System.lineSeparator()))); + } catch (IOException e) { + throw new MojoFailureException(e); + } + } + +}