Skip to content

Commit

Permalink
Refactor asciidoctor-maven-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Nov 3, 2024
1 parent 5c842f3 commit 759f65f
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 146 deletions.
1 change: 1 addition & 0 deletions asciidoctor-maven-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<!-- Required: see https://issues.apache.org/jira/browse/MNG-6965 -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.asciidoctor.maven;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.jruby.Ruby;

import javax.inject.Singleton;

@Singleton
public class AsciidoctorJFactory {

Asciidoctor create(String gemPath, Log log) throws MojoExecutionException {
Asciidoctor asciidoctor;
if (gemPath == null) {
asciidoctor = AsciidoctorJRuby.Factory.create();
} else {
// Replace Windows path separator to avoid paths with mixed \ and /.
// This happens for instance when setting: <gemPath>${project.build.directory}/gems-provided</gemPath>
// because the project's path is converted to string.
String normalizedGemPath = (File.separatorChar == '\\') ? gemPath.replaceAll("\\\\", "/") : gemPath;
asciidoctor = AsciidoctorJRuby.Factory.create(normalizedGemPath);
}

Ruby rubyInstance = null;
try {
rubyInstance = (Ruby) JRubyRuntimeContext.class.getMethod("get")
.invoke(null);
} catch (NoSuchMethodException e) {
if (rubyInstance == null) {
try {
rubyInstance = (Ruby) JRubyRuntimeContext.class.getMethod("get", Asciidoctor.class).invoke(null, asciidoctor);
} catch (Exception e1) {
throw new MojoExecutionException("Failed to invoke get(AsciiDoctor) for JRubyRuntimeContext", e1);
}

}
} catch (Exception e) {
throw new MojoExecutionException("Failed to invoke get for JRubyRuntimeContext", e);
}

String gemHome = rubyInstance.evalScriptlet("ENV['GEM_HOME']").toString();
String gemHomeExpected = (gemPath == null || "".equals(gemPath)) ? "" : gemPath.split(java.io.File.pathSeparator)[0];

if (!"".equals(gemHome) && !gemHomeExpected.equals(gemHome)) {
log.warn("Using inherited external environment to resolve gems (" + gemHome + "), i.e. build is platform dependent!");
}

return asciidoctor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -22,14 +21,8 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.asciidoctor.maven.commons.AsciidoctorHelper;
import org.asciidoctor.maven.extensions.AsciidoctorJExtensionRegistry;
import org.asciidoctor.maven.extensions.ExtensionConfiguration;
import org.asciidoctor.maven.extensions.ExtensionRegistry;
Expand All @@ -38,14 +31,10 @@
import org.asciidoctor.maven.log.LogRecordsProcessors;
import org.asciidoctor.maven.log.MemoryLogHandler;
import org.asciidoctor.maven.model.Resource;
import org.asciidoctor.maven.process.CopyResourcesProcessor;
import org.asciidoctor.maven.process.ResourcesProcessor;
import org.asciidoctor.maven.process.SourceDirectoryFinder;
import org.asciidoctor.maven.process.SourceDocumentFinder;
import org.jruby.Ruby;

import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;


Expand Down Expand Up @@ -145,9 +134,14 @@ public class AsciidoctorMojo extends AbstractMojo {

@Inject
protected MavenProject project;


protected final ResourcesProcessor defaultResourcesProcessor = new CopyResourcesProcessor();
@Inject
private AsciidoctorJFactory asciidoctorJFactory;
@Inject
private SourceDocumentFinder finder;
@Inject
protected ResourcesProcessor defaultResourcesProcessor;
@Inject
private AsciidoctorOptionsFactory asciidoctorOptionsFactory;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down Expand Up @@ -209,7 +203,8 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
}
}

final Asciidoctor asciidoctor = getAsciidoctorInstance(gemPath);
final Asciidoctor asciidoctor = asciidoctorJFactory.create(gemPath, getLog());

if (enableVerbose) {
asciidoctor.requireLibrary("enable_verbose.rb");
}
Expand All @@ -224,8 +219,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
}
}

AttributesBuilder attributesBuilder = createAttributesBuilder(this, project);
OptionsBuilder optionsBuilder = createOptionsBuilder(this, attributesBuilder);
OptionsBuilder optionsBuilder = asciidoctorOptionsFactory.create(this, project, getLog());

// Copy output resources
final File sourceDir = sourceDirectoryCandidate.get();
Expand All @@ -234,7 +228,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
// register LogHandler to capture asciidoctor messages
final Boolean outputToConsole = logHandler.getOutputToConsole() == null ? Boolean.TRUE : logHandler.getOutputToConsole();
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(outputToConsole,
logRecord -> getLog().info(LogRecordFormatter.format(logRecord, sourceDir)));
logRecord -> getLog().info(LogRecordFormatter.format(logRecord, sourceDir)));
asciidoctor.registerLogHandler(memoryLogHandler);
// disable default console output of AsciidoctorJ
Logger.getLogger("asciidoctor").setUseParentHandlers(false);
Expand All @@ -257,7 +251,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP
try {
// process log messages according to mojo configuration
new LogRecordsProcessors(logHandler, sourceDir, errorMessage -> getLog().error(errorMessage))
.processLogRecords(memoryLogHandler);
.processLogRecords(memoryLogHandler);
} catch (Exception exception) {
throw new MojoExecutionException(exception.getMessage());
}
Expand All @@ -266,12 +260,12 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP

public Optional<File> findSourceDirectory(File initialSourceDirectory, File baseDir) {
Optional<File> sourceDirCandidate = new SourceDirectoryFinder(initialSourceDirectory, baseDir,
candidate -> {
String candidateName = candidate.toString();
if (isRelativePath(candidateName)) candidateName = candidateName.substring(2);
getLog().info("sourceDirectory " + candidateName + " does not exist");
})
.find();
candidate -> {
String candidateName = candidate.toString();
if (isRelativePath(candidateName)) candidateName = candidateName.substring(2);
getLog().info("sourceDirectory " + candidateName + " does not exist");
})
.find();

return sourceDirCandidate;
}
Expand Down Expand Up @@ -317,8 +311,8 @@ public Destination setDestinationPaths(final File sourceFile, final OptionsBuild
// allow overriding the output file name
optionsBuilder.toFile(outputFile);
return outputFile.isAbsolute()
? new Destination(outputFile, true)
: new Destination(new File(toDir, outputFile.getPath()), true);
? new Destination(outputFile, true)
: new Destination(new File(toDir, outputFile.getPath()), true);
} else {
return new Destination(new File(toDir, sourceFile.getName()), false);
}
Expand All @@ -338,59 +332,14 @@ class Destination {
}
}

protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutionException {
Asciidoctor asciidoctor;
if (gemPath == null) {
asciidoctor = AsciidoctorJRuby.Factory.create();
} else {
// Replace Windows path separator to avoid paths with mixed \ and /.
// This happens for instance when setting: <gemPath>${project.build.directory}/gems-provided</gemPath>
// because the project's path is converted to string.
String normalizedGemPath = (File.separatorChar == '\\') ? gemPath.replaceAll("\\\\", "/") : gemPath;
asciidoctor = AsciidoctorJRuby.Factory.create(normalizedGemPath);
}

Ruby rubyInstance = null;
try {
rubyInstance = (Ruby) JRubyRuntimeContext.class.getMethod("get")
.invoke(null);
} catch (NoSuchMethodException e) {
if (rubyInstance == null) {
try {
rubyInstance = (Ruby) JRubyRuntimeContext.class.getMethod(
"get", Asciidoctor.class).invoke(null, asciidoctor);
} catch (Exception e1) {
throw new MojoExecutionException(
"Failed to invoke get(AsciiDoctor) for JRubyRuntimeContext",
e1);
}

}
} catch (Exception e) {
throw new MojoExecutionException(
"Failed to invoke get for JRubyRuntimeContext", e);
}

String gemHome = rubyInstance.evalScriptlet("ENV['GEM_HOME']").toString();
String gemHomeExpected = (gemPath == null || "".equals(gemPath)) ? "" : gemPath.split(java.io.File.pathSeparator)[0];

if (!"".equals(gemHome) && !gemHomeExpected.equals(gemHome)) {
getLog().warn("Using inherited external environment to resolve gems (" + gemHome + "), i.e. build is platform dependent!");
}

return asciidoctor;
}

protected List<File> findSourceFiles(File sourceDirectory) {
if (sourceDocumentName != null)
return Arrays.asList(new File(sourceDirectory, sourceDocumentName));

Path sourceDirectoryPath = sourceDirectory.toPath();
SourceDocumentFinder finder = new SourceDocumentFinder();
return List.of(new File(sourceDirectory, sourceDocumentName));

final Path sourceDirectoryPath = sourceDirectory.toPath();
return sourceDocumentExtensions.isEmpty() ?
finder.find(sourceDirectoryPath) :
finder.find(sourceDirectoryPath, sourceDocumentExtensions);
finder.find(sourceDirectoryPath) :
finder.find(sourceDirectoryPath, sourceDocumentExtensions);
}

protected void convertFile(Asciidoctor asciidoctor, Options options, File f) {
Expand All @@ -409,73 +358,6 @@ protected boolean ensureOutputExists() {
return true;
}

/**
* Creates an OptionsBuilder instance with the options defined in the configuration.
*
* @param configuration AsciidoctorMojo containing conversion configuration.
* @param attributesBuilder If not null, Asciidoctor attributes to add to the OptionsBuilder created.
* @return initialized optionsBuilder.
*/
protected OptionsBuilder createOptionsBuilder(AsciidoctorMojo configuration, AttributesBuilder attributesBuilder) {

final OptionsBuilder optionsBuilder = Options.builder()
.backend(configuration.getBackend())
.safe(SafeMode.UNSAFE)
.standalone(configuration.standalone)
.mkDirs(true);

if (!isBlank(configuration.getEruby()))
optionsBuilder.eruby(configuration.getEruby());

if (configuration.isSourcemap())
optionsBuilder.option(Options.SOURCEMAP, true);

if (configuration.isCatalogAssets())
optionsBuilder.option(Options.CATALOG_ASSETS, true);

if (!configuration.isTemplateCache())
optionsBuilder.option(Options.TEMPLATE_CACHE, false);

if (configuration.getDoctype() != null)
optionsBuilder.docType(doctype);

if (configuration.getTemplateEngine() != null)
optionsBuilder.templateEngine(templateEngine);

if (!configuration.getTemplateDirs().isEmpty())
optionsBuilder.templateDirs(templateDirs.toArray(new File[]{}));

optionsBuilder.attributes(attributesBuilder.build());
return optionsBuilder;
}

/**
* Creates an AttributesBuilder instance with the attributes defined in the configuration.
*
* @param configuration AsciidoctorMojo containing conversion configuration.
* @param mavenProject Current {@link MavenProject} instance.
* @return initialized attributesBuilder.
*/
protected AttributesBuilder createAttributesBuilder(AsciidoctorMojo configuration, MavenProject mavenProject) {

final AttributesBuilder attributesBuilder = Attributes.builder();

if (configuration.isEmbedAssets()) {
attributesBuilder.linkCss(false);
attributesBuilder.dataUri(true);
}

AsciidoctorHelper.addProperties(mavenProject.getProperties(), attributesBuilder);
AsciidoctorHelper.addAttributes(configuration.getAttributes(), attributesBuilder);

if (isNotBlank(configuration.getAttributesChain())) {
getLog().info("Attributes: " + attributesChain);
attributesBuilder.arguments(attributesChain);
}

return attributesBuilder;
}

public File getSourceDirectory() {
return sourceDirectory;
}
Expand Down
Loading

0 comments on commit 759f65f

Please sign in to comment.