Skip to content

Commit

Permalink
Capture current file being converted and used if logRecord.cursor is …
Browse files Browse the repository at this point in the history
…null
  • Loading branch information
abelsromero committed Mar 8, 2025
1 parent 2bf959a commit 7c801c0
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.asciidoctor.maven.log;

import java.io.File;
import java.util.Optional;

import org.asciidoctor.ast.Cursor;
import org.asciidoctor.log.LogRecord;

/**
* {@link LogRecord} proxy that allows capturing the source file being
* processed.
* Important: the {@link #sourceFile} and the actual source where an error is present
* may not be the same. For example if the source is being included.
*
* @since 3.1.2
*/
final class CapturedLogRecord extends LogRecord {

private final File sourceFile;

CapturedLogRecord(LogRecord record, File sourceFile) {
super(record.getSeverity(), record.getCursor(), record.getMessage(), record.getSourceFileName(), record.getSourceMethodName());
this.sourceFile = sourceFile;
}

public Cursor getCursor() {
if (sourceFile == null)
return null;

return Optional.ofNullable(super.getCursor())
.orElse(new FileCursor(sourceFile));
}

public File getSourceFile() {
return sourceFile;
}

class FileCursor implements Cursor {

private final File file;

public FileCursor(File file) {
this.file = file;
}

@Override
public int getLineNumber() {
return 0;
}

@Override
public String getPath() {
return file.getName();
}

@Override
public String getDir() {
return file.getParent();
}

@Override
public String getFile() {
return file.getAbsolutePath();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.maven.log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -24,16 +25,26 @@ public class MemoryLogHandler implements LogHandler {
private final Boolean outputToConsole;
private final Consumer<LogRecord> recordConsumer;

/**
* Provides simple way to inject the current file being processes.
* Will need re-work in concurrent scenarios.
*
* @since 3.1.2
*/
private File currentFile;

public MemoryLogHandler(Boolean outputToConsole, Consumer<LogRecord> recordConsumer) {
this.outputToConsole = outputToConsole == null ? Boolean.FALSE : outputToConsole;
this.recordConsumer = recordConsumer;
}

@Override
public void log(LogRecord logRecord) {
records.add(logRecord);
final CapturedLogRecord record = new CapturedLogRecord(logRecord, currentFile);

records.add(record);
if (outputToConsole)
recordConsumer.accept(logRecord);
recordConsumer.accept(record);
}

public void clear() {
Expand Down Expand Up @@ -108,4 +119,8 @@ private static boolean messageContains(LogRecord record, String text) {
}
}

public void setCurrentFile(File currentFile) {
this.currentFile = currentFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.List;

import static org.asciidoctor.log.Severity.*;
import static org.asciidoctor.maven.log.TestLogRecords.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

import org.asciidoctor.log.LogRecord;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -256,16 +256,4 @@ private static MemoryLogHandler testMemoryLogHandler() {
return memoryLogHandler;
}

private static LogRecord errorMessage(int index) {
return new LogRecord(ERROR, "error message " + index);
}

private static LogRecord getInfoMessage(int index) {
return new LogRecord(INFO, "info message " + index);
}

private static LogRecord warningMessage(int index) {
return new LogRecord(WARN, "warning message " + index);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import static org.asciidoctor.log.Severity.*;
import static org.asciidoctor.maven.log.TestLogRecords.*;
import static org.assertj.core.api.Assertions.assertThat;

import org.asciidoctor.log.LogRecord;
Expand Down Expand Up @@ -162,15 +163,4 @@ private static MemoryLogHandler testMemoryLogHandler() {
return memoryLogHandler;
}

private static LogRecord errorMessage() {
return new LogRecord(ERROR, "error message");
}

private static LogRecord getInfoMessage() {
return new LogRecord(INFO, "info message");
}

private static LogRecord warningMessage() {
return new LogRecord(WARN, "warning message");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.asciidoctor.maven.log;

import java.util.Optional;

import static org.asciidoctor.log.Severity.*;

import org.asciidoctor.log.LogRecord;

class TestLogRecords {

static LogRecord errorMessage() {
return errorMessage(null);
}

static LogRecord errorMessage(Integer index) {
LogRecord logRecord = new LogRecord(ERROR, buildMessage("error", index));
return new CapturedLogRecord(logRecord, null);
}

static LogRecord getInfoMessage() {
return getInfoMessage(null);
}

static LogRecord getInfoMessage(Integer index) {
LogRecord logRecord = new LogRecord(INFO, buildMessage("info", index));
return new CapturedLogRecord(logRecord, null);
}

static LogRecord warningMessage() {
return warningMessage(null);
}

static LogRecord warningMessage(Integer index) {
LogRecord logRecord = new LogRecord(WARN, buildMessage("warning", index));
return new CapturedLogRecord(logRecord, null);
}

private static String buildMessage(String type, Integer index) {
return Optional.ofNullable(index)
.map(i -> type + " message " + index)
.orElse(type + " message");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP

final Set<File> uniquePaths = new HashSet<>();
for (int i = 0; i < sourceFiles.size(); i++) {
// for (final File source : sourceFiles) {
final File source = sourceFiles.get(i);
final Destination destination = setDestinationPaths(source, optionsBuilder, sourceDir, this);
final File destinationPath = destination.path;
Expand All @@ -255,8 +254,8 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
getLog().warn("Duplicated destination found: overwriting file: " + destinationFile);
}

boolean lastFile = i == (sourceFiles.size() - 1);
convertFile(asciidoctor, optionsBuilder.build(), source, sourceDir, memoryLogHandler, lastFile);
boolean processLogRecords = logHandler.getFailFast() || (i == (sourceFiles.size() - 1));
convertFile(asciidoctor, optionsBuilder.build(), source, sourceDir, memoryLogHandler, processLogRecords);
}
}

Expand Down Expand Up @@ -354,10 +353,11 @@ protected List<File> findSourceFiles(File sourceDirectory) {
finder.find(sourceDirectoryPath, sourceDocumentExtensions);
}

private void convertFile(Asciidoctor asciidoctor, Options options, File f, File sourceDir, MemoryLogHandler memoryLogHandler, boolean lastFile) throws MojoExecutionException {
private void convertFile(Asciidoctor asciidoctor, Options options, File f, File sourceDir, MemoryLogHandler memoryLogHandler, boolean processLogRecords) throws MojoExecutionException {
memoryLogHandler.setCurrentFile(f);
asciidoctor.convertFile(f, options);
logConvertedFile(f);
if (logHandler.getFailFast() || lastFile) {
if (processLogRecords) {
processLogRecords(sourceDir, memoryLogHandler);
}
}
Expand Down
Loading

0 comments on commit 7c801c0

Please sign in to comment.