Skip to content

Commit

Permalink
Allow to skip generating empty report
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jun 30, 2024
1 parent 19aa03b commit 3219b78
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 81 deletions.
22 changes: 5 additions & 17 deletions src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -33,7 +34,6 @@

import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.taglist.beans.FileReport;
import org.codehaus.mojo.taglist.beans.TagReport;
import org.codehaus.mojo.taglist.tags.TagClass;
Expand Down Expand Up @@ -105,11 +105,6 @@ public class FileAnalyser {
*/
private final boolean emptyCommentsOn;

/**
* String used to indicate that there is no comment after the tag.
*/
private final String noCommentString;

/**
* ArrayList of tag classes.
*/
Expand All @@ -128,7 +123,6 @@ public FileAnalyser(TagListReport report, List<TagClass> tagClasses) {
sourceDirs = report.getSourceDirs();
encoding = report.getInputEncoding();
locale = report.getLocale();
noCommentString = report.getBundle().getString("report.taglist.nocomment");
this.tagClasses = tagClasses;
this.includes = report.getIncludesCommaSeparated();
this.excludes = report.getExcludesCommaSeparated();
Expand All @@ -138,9 +132,8 @@ public FileAnalyser(TagListReport report, List<TagClass> tagClasses) {
* Execute the analysis for the configuration given by the TagListReport.
*
* @return a collection of TagReport objects.
* @throws MavenReportException the Maven report exception.
*/
public Collection<TagReport> execute() throws MavenReportException {
public Collection<TagReport> execute() {
List<File> fileList = findFilesToScan();

for (File file : fileList) {
Expand All @@ -162,16 +155,15 @@ public Collection<TagReport> execute() throws MavenReportException {
* Gives the list of files to scan.
*
* @return a List of File objects.
* @throws MavenReportException the Maven report exception.
*/
private List<File> findFilesToScan() throws MavenReportException {
private List<File> findFilesToScan() {
List<File> filesList = new ArrayList<>();
try {
for (String sourceDir : sourceDirs) {
filesList.addAll(FileUtils.getFiles(new File(sourceDir), includes, excludes));
}
} catch (IOException e) {
throw new MavenReportException("Error while trying to find the files to scan.", e);
throw new UncheckedIOException("Error while trying to find the files to scan.", e);
}
return filesList;
}
Expand Down Expand Up @@ -219,11 +211,7 @@ public void scanFile(File file) {
firstLine = StringUtils.removeEnd(firstLine, "*/"); // MTAGLIST-35
if (firstLine.isEmpty() || ":".equals(firstLine)) {
// this is not a valid comment tag: nothing is written there
if (emptyCommentsOn) {
comment.append("--");
comment.append(noCommentString);
comment.append("--");
} else {
if (!emptyCommentsOn) {
continue;
}
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/codehaus/mojo/taglist/ReportGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,13 @@ private void doFileDetailedPart(FileReport fileReport) {
private void doCommentLine(FileReport fileReport, Integer lineNumber) {
boolean linked = false;

String comment = fileReport.getComment(lineNumber);
if (comment == null || comment.isEmpty()) {
comment = "--" + bundle.getString("report.taglist.nocomment") + "--";
}
sink.tableRow();
sink.tableCell();
sink.text(fileReport.getComment(lineNumber));
sink.text(comment);
sink.tableCell_();
sink.tableCell();
if (xrefLocation != null) {
Expand Down
112 changes: 67 additions & 45 deletions src/main/java/org/codehaus/mojo/taglist/TagListReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -70,7 +69,9 @@ public class TagListReport extends AbstractMavenReport {
@Parameter(property = "sourceFileLocale", defaultValue = "en")
private String sourceFileLocale;

/** Default locale used if the source file locale is null. */
/**
* Default locale used if the source file locale is null.
*/
private static final String DEFAULT_LOCALE = "en";

/**
Expand Down Expand Up @@ -100,7 +101,6 @@ public class TagListReport extends AbstractMavenReport {
/**
* This parameter indicates whether for simple tags (like "TODO"), the analyzer should look for multiple line
* comments.
*
*/
@Parameter(defaultValue = "true")
private boolean multipleLineComments;
Expand Down Expand Up @@ -191,13 +191,24 @@ public class TagListReport extends AbstractMavenReport {

private String[] tags;

/**
* Skip generating report if no tags found in sources.
*
* @since 3.1.0
*/
@Parameter(property = "taglist.skipEmptyReport", defaultValue = "false")
private boolean skipEmptyReport;

private final AtomicReference<List<String>> sourceDirs = new AtomicReference<>();

private Collection<TagReport> tagReportsResult;

/**
* {@inheritDoc}
*
* @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
*/
@Override
protected void executeReport(Locale locale) throws MavenReportException {
this.currentLocale = locale;

Expand All @@ -206,6 +217,45 @@ protected void executeReport(Locale locale) throws MavenReportException {
+ Charset.defaultCharset().displayName() + ", i.e. build is platform dependent!");
}

executeAnalysis();

// Renders the report
ReportGenerator generator = new ReportGenerator(this, tagReportsResult);
if (linkXRef) {
String relativePath = getRelativePath(xrefLocation);
if (xrefLocation.exists()) {
// XRef was already generated by manual execution of a lifecycle binding
generator.setXrefLocation(relativePath);
generator.setTestXrefLocation(getRelativePath(testXrefLocation));
} else {
// Not yet generated - check if the report is on its way

for (ReportPlugin report :
getProject().getModel().getReporting().getPlugins()) {
String artifactId = report.getArtifactId();
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
getLog().error("Taglist plugin MUST be executed after the JXR plugin."
+ " No links to xref were generated.");
}
}
}

if (generator.getXrefLocation() == null) {
getLog().warn("Unable to locate Source XRef to link to - DISABLED");
}
}
generator.generateReport();

// Generate the XML report
generateXmlReport(tagReportsResult);
}

private void executeAnalysis() {
if (tagReportsResult != null) {
// already analyzed
return;
}

// Create the tag classes
List<TagClass> tagClasses = new ArrayList<>();

Expand Down Expand Up @@ -256,37 +306,7 @@ protected void executeReport(Locale locale) throws MavenReportException {

// let's proceed to the analysis
FileAnalyser fileAnalyser = new FileAnalyser(this, tagClasses);
Collection<TagReport> tagReports = fileAnalyser.execute();

// Renders the report
ReportGenerator generator = new ReportGenerator(this, tagReports);
if (linkXRef) {
String relativePath = getRelativePath(xrefLocation);
if (xrefLocation.exists()) {
// XRef was already generated by manual execution of a lifecycle binding
generator.setXrefLocation(relativePath);
generator.setTestXrefLocation(getRelativePath(testXrefLocation));
} else {
// Not yet generated - check if the report is on its way

for (ReportPlugin report :
getProject().getModel().getReporting().getPlugins()) {
String artifactId = report.getArtifactId();
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
getLog().error("Taglist plugin MUST be executed after the JXR plugin."
+ " No links to xref were generated.");
}
}
}

if (generator.getXrefLocation() == null) {
getLog().warn("Unable to locate Source XRef to link to - DISABLED");
}
}
generator.generateReport();

// Generate the XML report
generateXmlReport(tagReports);
tagReportsResult = fileAnalyser.execute();
}

private TagClass createTagClass(String tag) {
Expand Down Expand Up @@ -372,12 +392,19 @@ private String getRelativePath(File location) {
*
* @see org.apache.maven.reporting.MavenReport#canGenerateReport()
*/
@Override
public boolean canGenerateReport() {
boolean canGenerate = !getSourceDirs().isEmpty();
if (aggregate && !getProject().isExecutionRoot()) {
canGenerate = false;
}
return canGenerate;

if (canGenerate) {
executeAnalysis();
return !skipEmptyReport || tagReportsResult.stream().anyMatch(tagReport -> tagReport.getTagCount() > 0);
}

return false;
}

/**
Expand All @@ -401,7 +428,7 @@ private List<String> pruneSourceDirs(List<String> sourceDirectories) throws IOEx
*
* @param dir the source directory.
* @return true if the folder or one of its subfolders contains at least 1 source file that matches
* includes/excludes.
* includes/excludes.
*/
private boolean hasSources(File dir) throws IOException {
if (dir.exists() && dir.isDirectory()) {
Expand Down Expand Up @@ -537,20 +564,12 @@ public boolean isShowEmptyDetails() {
return showEmptyDetails;
}

/**
* {@inheritDoc}
*
* @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
*/
protected Renderer getSiteRenderer() {
return siteRenderer;
}

/**
* {@inheritDoc}
*
* @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
*/
@Override
protected String getOutputDirectory() {
return outputDirectory.getAbsolutePath();
}
Expand All @@ -569,6 +588,7 @@ protected String getXMLOutputDirectory() {
*
* @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
*/
@Override
public String getDescription(Locale locale) {
return getBundle(locale).getString("report.taglist.description");
}
Expand All @@ -578,6 +598,7 @@ public String getDescription(Locale locale) {
*
* @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
*/
@Override
public String getName(Locale locale) {
return getBundle(locale).getString("report.taglist.name");
}
Expand All @@ -587,6 +608,7 @@ public String getName(Locale locale) {
*
* @see org.apache.maven.reporting.MavenReport#getOutputName()
*/
@Override
public String getOutputName() {
return "taglist";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ public void testEmptyCommentsDisabled() throws Exception {

mojo.execute();

String htmlString = super.getGeneratedOutput(mojo);

// Check to see that there was zero tags found
String expected = "<td>@empty_comment</td><td>0</td>";
assertTrue("Missing tag result.", htmlString.contains(expected));
File outputDirectory = mojo.getReportOutputDirectory();
assertNotNull(outputDirectory);
assertFalse(outputDirectory.exists());
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/codehaus/mojo/taglist/TaglistMojoSkipTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.codehaus.mojo.taglist;

import java.io.File;

public class TaglistMojoSkipTest extends AbstractTaglistMojoTestCase {

public void testNoSourcesDirectory() throws Exception {
File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/no-sources-test/default-pom.xml");

TagListReport mojo = super.getTagListReport(pluginXmlFile);

// Run the TagList mojo
mojo.execute();

// output should not be generated
File outputDirectory = mojo.getReportOutputDirectory();
assertNotNull(outputDirectory);
assertFalse(outputDirectory.exists());
}
}
16 changes: 6 additions & 10 deletions src/test/java/org/codehaus/mojo/taglist/TaglistMojoTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,9 @@ public void testNotAtStartOfLineTags() throws Exception {
// Run the TagList mojo
mojo.execute();

String htmlString = super.getGeneratedOutput(mojo);

// Check to see a tag not at the start of a line does not show up.
String expected = "<td>not_start_of_line_tag</td><td>0</td>";
assertTrue("Incorrect tag not at start of line tag result.", htmlString.contains(expected));
File outputDirectory = mojo.getReportOutputDirectory();
assertNotNull(outputDirectory);
assertFalse(outputDirectory.exists());
}

/**
Expand All @@ -193,10 +191,8 @@ public void testSourceCodeVariablesTags() throws Exception {
// Run the TagList mojo
mojo.execute();

String htmlString = super.getGeneratedOutput(mojo);

// Check to see a source code variable does not show up.
String expected = "<td>source_code_variable_tag</td><td>0</td>";
assertTrue("Incorrect source code variable tag result.", htmlString.contains(expected));
File outputDirectory = mojo.getReportOutputDirectory();
assertNotNull(outputDirectory);
assertFalse(outputDirectory.exists());
}
}
Loading

0 comments on commit 3219b78

Please sign in to comment.