Skip to content

Commit

Permalink
makes auto-refresh only convert modified and created sources (asciido…
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Jul 28, 2020
1 parent 10b2899 commit df3a582
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 199 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
Improvements::

* Inject Maven properties as attributes in `process-asciidoc` mojo (#459)
* Make `auto-refresh` (and `http` by inheritance) only convert modified and created sources (#474)

Bug Fixes::

Expand Down
219 changes: 144 additions & 75 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,52 @@ public class AsciidoctorMojo extends AbstractMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
processAllSources();
}

/**
* Converts all found AsciiDoc sources according to mojo rules.
*
* @throws MojoExecutionException If requirements are not met
*/
public void processAllSources() throws MojoExecutionException {
processSources(null);
}

/**
* Converts a collection of AsciiDoc sources.
*
* @param sourceFiles Collection of source files to convert.
* @throws MojoExecutionException If requirements are not met
*/
public void processSources(List<File> sourceFiles) throws MojoExecutionException {
if (skip) {
getLog().info("AsciiDoc processing is skipped.");
return;
}

if (sourceDirectory == null) {
throw new MojoExecutionException("Required parameter 'asciidoctor.sourceDirectory' not set.");
throw new MojoExecutionException("Required parameter 'asciidoctor.alternateSourceDir' not set.");
}

Optional<File> sourceDirectoryCandidate = findSourceDirectory(sourceDirectory, project.getBasedir());
if (!sourceDirectoryCandidate.isPresent()) {
getLog().info("No sourceDirectory found. Skipping processing");
return;
}

if (sourceFiles == null) {
sourceFiles = calculateSourceFiles(sourceDirectoryCandidate.get());
}
if (sourceFiles.isEmpty()) {
getLog().info("No sources found. Skipping processing");
return;
}

if (!initSourceDirectory()) return;
ensureOutputExists();
if (!ensureOutputExists()) {
getLog().error("Can't create " + outputDirectory.getPath());
return;
}

// Validate resources to avoid errors later on
if (resources != null) {
Expand All @@ -187,17 +222,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (enableVerbose) {
asciidoctor.requireLibrary("enable_verbose.rb");
}

asciidoctor.requireLibraries(requires);

final OptionsBuilder optionsBuilder = OptionsBuilder.options();
setOptionsOnBuilder(optionsBuilder);

final AttributesBuilder attributesBuilder = AttributesBuilder.attributes();
setAttributesOnBuilder(attributesBuilder);

optionsBuilder.attributes(attributesBuilder);

ExtensionRegistry extensionRegistry = new AsciidoctorJExtensionRegistry(asciidoctor);
for (ExtensionConfiguration extension : extensions) {
try {
Expand All @@ -207,57 +233,50 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

AttributesBuilder attributesBuilder = createAttributesBuilder(this, project);
OptionsBuilder optionsBuilder = createOptionsBuilder(this, attributesBuilder);

// Copy output resources
prepareResources();
final File sourceDir = sourceDirectoryCandidate.get();
prepareResources(sourceDir);
copyResources();

// Prepare sources
final List<File> sourceFiles = calculateSourceFiles();

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

final Set<File> uniqueDirs = new HashSet<>();
final Set<File> uniquePaths = new HashSet<>();
for (final File source : sourceFiles) {
final File destinationPath = setDestinationPaths(optionsBuilder, source);
if (!uniqueDirs.add(destinationPath))
final File destinationPath = setDestinationPaths(source, optionsBuilder, sourceDir);
if (!uniquePaths.add(destinationPath))
getLog().warn("Duplicated destination found: overwriting file: " + destinationPath.getAbsolutePath());

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

try {
// process log messages according to mojo configuration
new LogRecordsProcessors(logHandler, sourceDirectory, errorMessage -> getLog().error(errorMessage))
new LogRecordsProcessors(logHandler, sourceDir, errorMessage -> getLog().error(errorMessage))
.processLogRecords(memoryLogHandler);
} catch (Exception exception) {
throw new MojoExecutionException(exception.getMessage());
}
}
}

private boolean initSourceDirectory() {
Optional<File> sourceDirCandidate = new SourceDirectoryFinder(sourceDirectory, project.getBasedir(),
private 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();

if (sourceDirCandidate.isPresent()) {
this.sourceDirectory = sourceDirCandidate.get();
} else {
getLog().info("No sourceDirectory found. Skipping processing");
return false;
}
return true;
return sourceDirCandidate;
}

private boolean isRelativePath(String candidateName) {
Expand All @@ -269,13 +288,13 @@ private boolean isRelativePath(String candidateName) {
* underscore).
* By default everything in the sources directories is copied.
*/
private void prepareResources() {
private void prepareResources(File sourceDirectory) {
if (resources == null || resources.isEmpty()) {
resources = new ArrayList<Resource>();
resources = new ArrayList<>();
// we don't want to copy files considered sources
Resource resource = new Resource();
resource.setDirectory(sourceDirectory.getAbsolutePath());
resource.setExcludes(new ArrayList<String>());
resource.setExcludes(new ArrayList<>());
// exclude sourceDocumentName if defined
if (sourceDocumentName != null && sourceDocumentName.isEmpty()) {
resource.getExcludes().add(sourceDocumentName);
Expand All @@ -287,9 +306,9 @@ private void prepareResources() {
// All resources must exclude AsciiDoc documents and folders beginning with underscore
for (Resource resource : resources) {
if (resource.getExcludes() == null || resource.getExcludes().isEmpty()) {
resource.setExcludes(new ArrayList<String>());
resource.setExcludes(new ArrayList<>());
}
List<String> excludes = new ArrayList<String>();
List<String> excludes = new ArrayList<>();
for (String value : AsciidoctorFileScanner.IGNORED_FOLDERS_AND_FILES) {
excludes.add(value);
}
Expand All @@ -313,7 +332,7 @@ private void copyResources() throws MojoExecutionException {
// Right now it's not used at all, but could be used to apply resource filters/replacements
MavenResourcesExecution resourcesExecution =
new MavenResourcesExecution(resources, outputDirectory, project, encoding,
Collections.<String>emptyList(), Collections.<String>emptyList(), session);
Collections.emptyList(), Collections.emptyList(), session);
resourcesExecution.setIncludeEmptyDirs(true);
resourcesExecution.setAddDefaultExcludes(true);
outputResourcesFiltering.filterResources(resourcesExecution);
Expand All @@ -325,11 +344,12 @@ private void copyResources() throws MojoExecutionException {
/**
* Updates optionsBuilder object's baseDir and destination(s) accordingly to the options.
*
* @param optionsBuilder AsciidoctorJ options to be updated.
* @param sourceFile AsciiDoc source file to process.
* @param sourceFile AsciiDoc source file to process.
* @param optionsBuilder Asciidoctor options to be updated.
* @param sourceDirectory Source directory configured (`sourceFile` may include relative path).
* @return the final destination file path.
*/
private File setDestinationPaths(OptionsBuilder optionsBuilder, final File sourceFile) throws MojoExecutionException {
private File setDestinationPaths(final File sourceFile, final OptionsBuilder optionsBuilder, final File sourceDirectory) throws MojoExecutionException {
try {
if (baseDir != null) {
optionsBuilder.baseDir(baseDir);
Expand All @@ -349,7 +369,7 @@ private File setDestinationPaths(OptionsBuilder optionsBuilder, final File sourc
optionsBuilder.toDir(outputDirectory).destinationDir(outputDirectory);
}
if (outputFile != null) {
//allow overriding the output file name
// allow overriding the output file name
optionsBuilder.toFile(outputFile);
}
// return destination file path
Expand Down Expand Up @@ -406,14 +426,15 @@ protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutio
return asciidoctor;
}

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

// Both DirectoryWalkers filter out internal sources and path (_ prefix)
final String sourceDirectoryAbsolutePath = sourceDirectory.getAbsolutePath();
final DirectoryWalker directoryWalker = sourceDocumentExtensions.isEmpty()
? new AsciiDocDirectoryWalker(sourceDirectory.getAbsolutePath())
: new CustomExtensionDirectoryWalker(sourceDirectory.getAbsolutePath(), sourceDocumentExtensions);
? new AsciiDocDirectoryWalker(sourceDirectoryAbsolutePath)
: new CustomExtensionDirectoryWalker(sourceDirectoryAbsolutePath, sourceDocumentExtensions);

return directoryWalker.scan();
}
Expand All @@ -427,61 +448,78 @@ protected void logConvertedFile(File f) {
getLog().info("Converted " + f.getAbsolutePath());
}

protected void ensureOutputExists() {
protected boolean ensureOutputExists() {
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
getLog().error("Can't create " + outputDirectory.getPath());
}
return outputDirectory.mkdirs();
}
return true;
}

/**
* Updates and OptionsBuilder instance with the options defined in the configuration.
* Creates an OptionsBuilder instance with the options defined in the configuration.
*
* @param optionsBuilder AsciidoctorJ options to be updated.
* @param configuration AsciidoctorMojo containing conversion configuration.
* @param attributesBuilder
* @return initialized optionsBuilder.
*/
protected void setOptionsOnBuilder(OptionsBuilder optionsBuilder) {
optionsBuilder
.backend(backend)
protected OptionsBuilder createOptionsBuilder(AsciidoctorMojo configuration, AttributesBuilder attributesBuilder) {

final OptionsBuilder optionsBuilder = OptionsBuilder.options()
.backend(configuration.getBackend())
.safe(SafeMode.UNSAFE)
.headerFooter(headerFooter)
.eruby(eruby)
.headerFooter(configuration.isHeaderFooter())
.eruby(configuration.getEruby())
.mkDirs(true);

// Following options are only set when the value is different than the default
if (sourcemap)
optionsBuilder.option("sourcemap", true);
if (configuration.isSourcemap())
optionsBuilder.option(Options.SOURCEMAP, true);

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

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

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

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

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

if (!attributesBuilder.asMap().isEmpty())
optionsBuilder.attributes(attributesBuilder);

return optionsBuilder;
}

protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) {
if (embedAssets) {

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

final AttributesBuilder attributesBuilder = AttributesBuilder.attributes();

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

AsciidoctorHelper.addMavenProperties(project, attributesBuilder);
AsciidoctorHelper.addAttributes(attributes, attributesBuilder);
AsciidoctorHelper.addMavenProperties(maven, attributesBuilder);
AsciidoctorHelper.addAttributes(configuration.getAttributes(), attributesBuilder);

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

return attributesBuilder;
}

public File getSourceDirectory() {
Expand Down Expand Up @@ -651,4 +689,35 @@ public void setResources(List<Resource> resources) {
this.resources = resources;
}

public boolean isEnableVerbose() {
return enableVerbose;
}

public List<String> getRequires() {
return requires;
}

public void setEnableVerbose(boolean enableVerbose) {
this.enableVerbose = enableVerbose;
}

public boolean isSourcemap() {
return sourcemap;
}

public boolean isCatalogAssets() {
return catalogAssets;
}

public boolean isTemplateCache() {
return templateCache;
}

public List<File> getTemplateDirs() {
return templateDirs;
}

public String getAttributesChain() {
return attributesChain;
}
}
Loading

0 comments on commit df3a582

Please sign in to comment.