Skip to content

Commit

Permalink
Add a markdown builder to simplify common tasks and unify generation
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 22, 2025
1 parent 9204639 commit e19794b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -139,7 +139,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
JrtClasses jrtClassResolver = getJRTClassResolver();
List<ClassUsage> usages = DependencyAnalyzer.analyzeUsage(file, jrtClassResolver);
if (usages.isEmpty()) {
writeReport("No usages detected");
return;
}
Collection<IInstallableUnit> units = artifacts.getInstallableUnits();
Expand Down Expand Up @@ -271,10 +270,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}
if (dependencyProblems.isEmpty()) {
writeReport("No Version Problems detected!");
return;
}
List<String> results = new ArrayList<>();
if (applySuggestions) {
applyLowerBounds(packageWithError, lowestPackageVersion);
}
MarkdownBuilder results = new MarkdownBuilder(reportFileName);
for (DependencyVersionProblem problem : dependencyProblems) {
Collection<String> references = problem.references();
String message;
Expand Down Expand Up @@ -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) {
Expand All @@ -349,6 +337,9 @@ private String getMethodRef(MethodSignature mthd) {

private void applyLowerBounds(Set<String> packageWithError, Map<String, Version> lowestPackageVersion)
throws MojoFailureException {
if (packageWithError.isEmpty()) {
return;
}
try {
MutableBundleManifest manifest = MutableBundleManifest.read(manifestFile);
Map<String, String> exportedPackagesVersion = manifest.getExportedPackagesVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,17 +58,15 @@ protected CleanUp createExecutable() {
@Override
protected void handleResult(CleanupResult result)
throws MojoFailureException {
List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public Stream<String> cleanups() {
return this.cleanups.stream();
}

public boolean isEmpty() {
return cleanups.isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,26 +31,14 @@ public class QuickFixMojo extends AbstractEclipseBuildMojo<QuickFixResult> {

@Override
protected void handleResult(QuickFixResult result) throws MojoFailureException {
List<String> results = new ArrayList<>();
if (result.getMarkers() == 0) {
results.add("Project is clean!");
} else {
List<String> 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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public int getMarkers() {
return markers;
}

public boolean isEmpty() {
return fixed.isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}

}

0 comments on commit e19794b

Please sign in to comment.