Skip to content

Commit

Permalink
Added new feature to fail based on different conditions (asciidoctor#363
Browse files Browse the repository at this point in the history
)

added new feature to fail build based on Asciidoctor messages
  • Loading branch information
abelsromero authored Sep 12, 2018
1 parent 48d1142 commit f38a472
Show file tree
Hide file tree
Showing 25 changed files with 814 additions and 68 deletions.
39 changes: 39 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,45 @@ Here follows a configuration example:
NOTE: Extensions can also be integrated through the SPI interface implementation.
This method does not require any configuration in the [.path]_pom.xml_, see link:https://github.com/asciidoctor/asciidoctorj#extension-spi[Extension SPI] for details.

logHandler:: enables processing of Asciidoctor messages (e.g. errors on missing included files), to hide messages as well setup build fail conditions based on them.
Contains the following configuration elements:

* `outputToConsole`: `Boolean`, defaults to `true`.
Redirects all Asciidoctor messages to Maven's console logger as INFO during renderization.
* `failIf`: build fail conditions, disabled by default.
Allows setting one or many conditions that when met, abort the Maven build with `BUILD FAILURE` status.
+
[NOTE]
====
Note that the plugin matches that all conditions are met together.
Unless you are controlling a very specific case, setting one condition should be enough. +
Also, messages matching fail conditions will be sent to Maven's logger as ERROR.
So, when enabling `outputToConsole`, some messages will appear duplicated as both INFO and ERROR.
====
+
Currently, two conditions can be defined:

** `severity`: severity of the Asciidoctor message, in order: `INFO`,`WARN`,`ERROR`,`FATAL`,`UNKNOWN`.
Build will fail if a message is found of severity equal or higher.

** `containsText`: text to search inside messages.
Build will fail if the text is found. +
For example, set `include` to fail on any issue related to included files regardless the severity level.
+
[source,xml]
.example: fail on any message
----
<logHandler>
<outputToConsole>false</outputToConsole> <!--1-->
<failIf>
<severity>DEBUG</severity> <!--2-->
</failIf>
</logHandler>
----
<1> Do not show messages as INFO in Maven output
<2> Build will fail on any message of severity `DEBUG` or higher, that includes all.
All matching messages will appear as ERROR in Maven output.

==== Built-in attributes

There are various attributes Asciidoctor recognizes.
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
<project.execution.environment>JavaSE-1.7</project.execution.environment>
<spock.version>0.7-groovy-2.0</spock.version>
<groovy.version>2.1.3</groovy.version>
<asciidoctorj.version>1.5.6</asciidoctorj.version>
<asciidoctorj.version>1.5.7</asciidoctorj.version>
<maven.coveralls.plugin.version>4.3.0</maven.coveralls.plugin.version>
<maven.jacoco.plugin.version>0.7.9</maven.jacoco.plugin.version>
<jruby.version>1.7.26</jruby.version>
<maven.plugin.plugin.version>3.4</maven.plugin.plugin.version>
<jruby.version>9.1.16.0</jruby.version>
<maven.plugin.plugin.version>3.5</maven.plugin.plugin.version>
<maven.project.version>3.0.5</maven.project.version>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<maven.filtering.version>3.1.1</maven.filtering.version>
Expand Down
52 changes: 52 additions & 0 deletions src/it/spi-registered-log/asciidoctor-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-project</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.asciidoctor</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<description>Runs asciidoctor-maven-plugin:process-asciidoc</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>html</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceDirectory>src/main/doc</sourceDirectory>
<outputDirectory>target/docs</outputDirectory>
<backend>html</backend>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>log-handler</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Document Title
==============
Doc Writer <thedoc@asciidoctor.org>
:idprefix: id_

Preamble paragraph.

NOTE: This is test, only a test.

== Section A

*Section A* paragraph.

=== Section A Subsection

*Section A* 'subsection' paragraph.

== Section B

*Section B* paragraph.

.Section B list
* Item 1
* Item 2
* Item 3

== Missing include

include::something.adoc[]
1 change: 1 addition & 0 deletions src/it/spi-registered-log/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean compile
52 changes: 52 additions & 0 deletions src/it/spi-registered-log/log-handler/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.asciidoctor</groupId>
<artifactId>log-handler</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.asciidoctor</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<description>Implements a custom AsciidoctorJ LogHandler</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.java.version>1.7</project.java.version>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<asciidoctorj.version>1.5.7</asciidoctorj.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${project.java.version}</source>
<target>${project.java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>${asciidoctorj.version}</version>
</dependency>
<!-- Include to use MojoExecutionException, MojoFailureException and cause Build failures integrated with maven -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.asciidoctor.maven.test;

import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestLogHandlerService implements LogHandler {

public static final String CUSTOM_LOG = "custom_log.log";

final FileOutputStream logFile;

private static List<LogRecord> logRecords = new ArrayList<>();

public static List<LogRecord> getLogRecords() {
return logRecords;
}

public static void clear() {
logRecords.clear();
}

public TestLogHandlerService() {
try {
logFile = new FileOutputStream(new File(CUSTOM_LOG));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}

@Override
public void log(LogRecord logRecord) {
writeLine("Logging from TestLogHandlerService: " + logRecord.getMessage());
logRecords.add(logRecord);
}

private void writeLine(String message) {
try {
logFile.write(message.getBytes());
logFile.flush();
logFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.asciidoctor.maven.test.processors.AutoregisteredProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.asciidoctor.maven.test.TestLogHandlerService
21 changes: 21 additions & 0 deletions src/it/spi-registered-log/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.asciidoctor</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<description>Tests SPI registration of an AsciidoctorJ LogHandler</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>log-handler</module>
<module>asciidoctor-project</module>
</modules>

</project>
9 changes: 9 additions & 0 deletions src/it/spi-registered-log/validate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
final def file = new File(basedir, "custom_log.log")

if (!file.exists())
throw new Exception("Log file not initialized")

if (!file.text.contains("Logging from TestLogHandlerService: include file not found:"))
throw new Exception("Expected LogHandler message not found in log file")

return true
51 changes: 51 additions & 0 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.asciidoctor.*;
import org.asciidoctor.internal.JRubyRuntimeContext;
import org.asciidoctor.log.LogRecord;
import org.asciidoctor.log.Severity;
import org.asciidoctor.maven.extensions.AsciidoctorJExtensionRegistry;
import org.asciidoctor.maven.extensions.ExtensionConfiguration;
import org.asciidoctor.maven.extensions.ExtensionRegistry;
import org.asciidoctor.maven.io.AsciidoctorFileScanner;
import org.asciidoctor.maven.log.LogHandler;
import org.asciidoctor.maven.log.LogRecordHelper;
import org.asciidoctor.maven.log.MemoryLogHandler;
import org.jruby.Ruby;
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;


/**
Expand Down Expand Up @@ -157,6 +163,9 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true, required = true)
protected MavenSession session;

@Parameter
private LogHandler logHandler = new LogHandler();

@Component
protected MavenResourcesFiltering outputResourcesFiltering;

Expand Down Expand Up @@ -220,12 +229,54 @@ public void execute() throws MojoExecutionException, MojoFailureException {
scanSourceFiles() : Arrays.asList(new File(sourceDirectory, sourceDocumentName));

final Set<File> dirs = new HashSet<File>();

// register LogHandler to capture asciidoctor messages
final Boolean outputToConsole = logHandler == null ? Boolean.TRUE : logHandler.getOutputToConsole();
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(outputToConsole, sourceDirectory, getLog());
if (!sourceFiles.isEmpty()) {
asciidoctor.registerLogHandler(memoryLogHandler);
// disable default console output of AsciidoctorJ
Logger.getLogger("asciidoctor").setUseParentHandlers(false);
}

for (final File source : sourceFiles) {
final File destinationPath = setDestinationPaths(optionsBuilder, source);
if (!dirs.add(destinationPath))
getLog().warn("Duplicated destination found: overwriting file: " + destinationPath.getAbsolutePath());

renderFile(asciidoctor, optionsBuilder.asMap(), source);

// process log messages according to mojo configuration
if (logHandler.isSeveritySet() && logHandler.isContainsTextNotBlank()) {
final Severity severity = logHandler.getFailIf().getSeverity();
final String textToSearch = logHandler.getFailIf().getContainsText();

final List<LogRecord> records = memoryLogHandler.filter(severity, textToSearch);
if (records.size() > 0) {
for (LogRecord record : records) {
getLog().error(LogRecordHelper.format(record, sourceDirectory));
}
throw new MojoExecutionException(String.format("Found %s issue(s) matching severity %s or higher and text '%s'", records.size(), severity, textToSearch));
}
} else if (logHandler.isSeveritySet()) {
final Severity severity = logHandler.getFailIf().getSeverity();
final List<LogRecord> records = memoryLogHandler.filter(severity);
if (records.size() > 0) {
for (LogRecord record : records) {
getLog().error(LogRecordHelper.format(record, sourceDirectory));
}
throw new MojoExecutionException(String.format("Found %s issue(s) of severity %s or higher during rendering", records.size(), severity));
}
} else if (logHandler.isContainsTextNotBlank()) {
final String textToSearch = logHandler.getFailIf().getContainsText();
final List<LogRecord> records = memoryLogHandler.filter(textToSearch);
if (records.size() > 0) {
for (LogRecord record : records) {
getLog().error(LogRecordHelper.format(record, sourceDirectory));
}
throw new MojoExecutionException(String.format("Found %s issue(s) containing '%s'", records.size(), textToSearch));
}
}
}

if (synchronizations != null && !synchronizations.isEmpty()) {
Expand Down
Loading

0 comments on commit f38a472

Please sign in to comment.