Skip to content

Commit

Permalink
avoids docInfo files from being copied in refresh mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Aug 5, 2020
1 parent 460e1b9 commit 9605dc0
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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)
* Make `auto-refresh` only copy modified and created resources + taking into consideration <resources> options (#478)
* Make `auto-refresh` ignore docInfo files to avoid copying them into output (#480)

Bug Fixes::

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
*/
@Mojo(name = "process-asciidoc", threadSafe = true)
public class AsciidoctorMojo extends AbstractMojo {
// copied from org.asciidoctor.AsciiDocDirectoryWalker.ASCIIDOC_REG_EXP_EXTENSION
// should probably be configured in AsciidoctorMojo through @Parameter 'extension'
protected static final String ASCIIDOC_FILE_EXTENSIONS_REG_EXP = "a((sc(iidoc)?)|d(oc)?)";
protected static final String ASCIIDOC_NON_INTERNAL_REG_EXP = "^[^_.].*\\." + ASCIIDOC_FILE_EXTENSIONS_REG_EXP + "$";

@Parameter(defaultValue = "${project.build.sourceEncoding}")
protected String encoding;
Expand Down Expand Up @@ -319,10 +315,13 @@ private List<Resource> prepareResources(File sourceDirectory, AsciidoctorMojo co
// All resources must exclude AsciiDoc documents and folders beginning with underscore
for (Resource resource : resources) {
List<String> excludes = new ArrayList<>();
for (String value : AsciidoctorFileScanner.IGNORED_FOLDERS_AND_FILES) {
for (String value : AsciidoctorFileScanner.INTERNAL_FOLDERS_AND_FILES_PATTERNS) {
excludes.add(value);
}
for (String value : AsciidoctorFileScanner.DEFAULT_FILE_EXTENSIONS) {
for (String value : AsciidoctorFileScanner.IGNORED_FILE_NAMES) {
excludes.add("**/" + value);
}
for (String value : AsciidoctorFileScanner.DEFAULT_ASCIIDOC_EXTENSIONS) {
excludes.add(value);
}
for (String docExtension : configuration.getSourceDocumentExtensions()) {
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.asciidoctor.maven.refresh.AsciidoctorConverterFileAlterationListenerAdaptor;
import org.asciidoctor.maven.refresh.ResourceCopyFileAlterationListenerAdaptor;
import org.asciidoctor.maven.refresh.ResourcesPatternBuilder;
import org.asciidoctor.maven.refresh.TimeCounter;

import java.io.File;
import java.io.FileFilter;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.asciidoctor.maven.io.AsciidoctorFileScanner.ASCIIDOC_NON_INTERNAL_REG_EXP;

@Mojo(name = "auto-refresh")
public class AsciidoctorRefreshMojo extends AsciidoctorMojo {
Expand Down Expand Up @@ -138,20 +140,8 @@ private void startPolling() throws MojoExecutionException {
}

private FileFilter buildResourcesFileFilter() {

final StringJoiner filePattern = new StringJoiner("|")
.add(ASCIIDOC_FILE_EXTENSIONS_REG_EXP);
if (!sourceDocumentExtensions.isEmpty())
filePattern.add(String.join("|", sourceDocumentExtensions));

final String includedResourcesPattern = new StringBuilder()
.append("^")
.append(isBlank(sourceDocumentName) ? "" : "(?!(" + sourceDocumentName + "))")
.append("[^_.].*\\.(?!(")
.append(filePattern.toString())
.append(")).*$")
.toString();
return FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), new RegexFileFilter(includedResourcesPattern));
final String resourcesRegexPattern = new ResourcesPatternBuilder(sourceDocumentName, sourceDocumentExtensions).build();
return FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), new RegexFileFilter(resourcesRegexPattern));
}

private IOFileFilter buildSourcesFileFilter() {
Expand Down
77 changes: 45 additions & 32 deletions src/main/java/org/asciidoctor/maven/io/AsciidoctorFileScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,39 @@
*/
public class AsciidoctorFileScanner {

public static String[] DEFAULT_FILE_EXTENSIONS = {"**/*.adoc", "**/*.ad", "**/*.asc","**/*.asciidoc"};
// copied from org.asciidoctor.AsciiDocDirectoryWalker.ASCIIDOC_REG_EXP_EXTENSION
// should probably be configured in AsciidoctorMojo through @Parameter 'extension'
public static final String ASCIIDOC_FILE_EXTENSIONS_REG_EXP = "a((sc(iidoc)?)|d(oc)?)";
public static final String ASCIIDOC_NON_INTERNAL_REG_EXP = "^[^_.].*\\." + ASCIIDOC_FILE_EXTENSIONS_REG_EXP + "$";

public static String[] IGNORED_FOLDERS_AND_FILES = {
// Files and directories beginning with underscore are ignored
public static String[] DEFAULT_ASCIIDOC_EXTENSIONS = {"**/*.adoc", "**/*.ad", "**/*.asc", "**/*.asciidoc"};

// Files and directories beginning with underscore are ignored
public static String[] INTERNAL_FOLDERS_AND_FILES_PATTERNS = {
"**/_*.*",
"**/_*",
"**/_*/**/*.*",
// docinfo snippets should not be copied
"**/docinfo.html",
"**/docinfo-header.html",
"**/docinfo-footer.html",
"**/*-docinfo.html",
"**/*-docinfo-header.html",
"**/*-docinfo-footer.html",
"**/docinfo.xml",
"**/docinfo-header.xml",
"**/docinfo-footer.xml",
"**/*-docinfo.xml",
"**/*-docinfo-header.xml",
"**/*-docinfo-footer.xml"};

private BuildContext buildContext;

public AsciidoctorFileScanner(BuildContext buildContext) {
};

// docinfo snippets should not be copied
public static String[] IGNORED_FILE_NAMES = {
"docinfo.html",
"docinfo-header.html",
"docinfo-footer.html",
"*-docinfo.html",
"*-docinfo-header.html",
"*-docinfo-footer.html",
"docinfo.xml",
"docinfo-header.xml",
"docinfo-footer.xml",
"*-docinfo.xml",
"*-docinfo-header.xml",
"*-docinfo-footer.xml"};


private final BuildContext buildContext;

public AsciidoctorFileScanner(final BuildContext buildContext) {
this.buildContext = buildContext;
}

Expand All @@ -50,7 +59,7 @@ public List<File> scan(Resource resource) {
Scanner scanner = buildContext.newScanner(new File(resource.getDirectory()), true);
setupScanner(scanner, resource);
scanner.scan();
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
for (String file : scanner.getIncludedFiles()) {
files.add(new File(resource.getDirectory(), file));
}
Expand All @@ -64,8 +73,8 @@ public List<File> scan(Resource resource) {
* @return List of found documents matching the resources properties
*/
public List<File> scan(List<Resource> resources) {
List<File> files = new ArrayList<File>();
for (Resource resource: resources) {
final List<File> files = new ArrayList<>();
for (Resource resource : resources) {
files.addAll(scan(resource));
}
return files;
Expand All @@ -79,36 +88,40 @@ public List<File> scan(List<Resource> resources) {
* <li>includes adds extension .adoc, .ad, .asc and .asciidoc
* <li>excludes adds filters to avoid hidden files and directoris beginning with undersore
* </ul>
*
* <p>
* NOTE: Patterns both in inclusions and exclusions are automatically excluded.
*/
private void setupScanner(Scanner scanner, Resource resource) {

if (resource.getIncludes() == null || resource.getIncludes().isEmpty()) {
scanner.setIncludes(DEFAULT_FILE_EXTENSIONS);
if (isEmpty(resource.getIncludes())) {
scanner.setIncludes(DEFAULT_ASCIIDOC_EXTENSIONS);
} else {
scanner.setIncludes(resource.getIncludes().toArray(new String[] {}));
scanner.setIncludes(resource.getIncludes().toArray(new String[]{}));
}

if (resource.getExcludes() == null || resource.getExcludes().isEmpty()) {
scanner.setExcludes(IGNORED_FOLDERS_AND_FILES);
if (isEmpty(resource.getExcludes())) {
scanner.setExcludes(IGNORED_FILE_NAMES);
} else {
scanner.setExcludes(mergeAndConvert(resource.getExcludes(), IGNORED_FOLDERS_AND_FILES));
scanner.setExcludes(mergeAndConvert(resource.getExcludes(), IGNORED_FILE_NAMES));
}
// adds exclusions like SVN or GIT files
scanner.addDefaultExcludes();
}

private boolean isEmpty(List<String> excludes) {
return excludes == null || excludes.isEmpty();
}

/**
* Returns a String[] with the values of both input parameters.
* Duplicated values are inserted only once.
*
* @param list List of string
* @param list List of string
* @param array Array of String
* @return Array of String with all values
*/
private String[] mergeAndConvert(List<String> list, String[] array) {
Set<String> set = new HashSet<String>(Arrays.asList(array));
Set<String> set = new HashSet<>(Arrays.asList(array));
set.addAll(list);
return set.toArray(new String[set.size()]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.asciidoctor.maven.refresh;

import org.asciidoctor.maven.io.AsciidoctorFileScanner;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.asciidoctor.maven.io.AsciidoctorFileScanner.ASCIIDOC_FILE_EXTENSIONS_REG_EXP;

/**
* Builds regular expression to include all valid resources to be copied for `auto-refresh` mojo.
*
* @author abelsromero
*/
public class ResourcesPatternBuilder {

private final String sourceDocumentName;
private final List<String> sourceDocumentExtensions;

public ResourcesPatternBuilder(final String sourceDocumentName, final List<String> sourceDocumentExtensions) {
this.sourceDocumentName = sourceDocumentName;
this.sourceDocumentExtensions = sourceDocumentExtensions;
}

public String build() {
final StringJoiner filePattern = new StringJoiner("|")
.add(ASCIIDOC_FILE_EXTENSIONS_REG_EXP);
if (!sourceDocumentExtensions.isEmpty())
filePattern.add(String.join("|", sourceDocumentExtensions));

final String specialFiles = Arrays.stream(AsciidoctorFileScanner.IGNORED_FILE_NAMES)
.map(pattern -> pattern.replaceAll("\\*", ".*"))
.map(pattern -> pattern.replaceAll("\\.", "\\\\."))
.collect(Collectors.joining("|"));

return new StringBuilder()
.append("^")
.append("(?!(" + specialFiles + (isBlank(sourceDocumentName) ? "" : "|" + sourceDocumentName) + "))")
.append("[^_.].*\\.(?!(")
.append(filePattern.toString())
.append(")).*$")
.toString();
}

}
Loading

0 comments on commit 9605dc0

Please sign in to comment.