Skip to content

Commit

Permalink
apply proper resource filter to refresh mojo when sourceDocumentName …
Browse files Browse the repository at this point in the history
…is set
  • Loading branch information
abelsromero committed Aug 2, 2020
1 parent f9a4809 commit 254af74
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.Scanner;
import java.util.StringJoiner;

import static org.apache.commons.lang3.StringUtils.isBlank;

@Mojo(name = "auto-refresh")
public class AsciidoctorRefreshMojo extends AsciidoctorMojo {

Expand Down Expand Up @@ -131,16 +133,24 @@ private void startPolling() throws MojoExecutionException {
}

private FileFilter buildResourcesFileFilter() {

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

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

private IOFileFilter buildSourcesFileFilter() {
if (sourceDocumentName != null)
if (!isBlank(sourceDocumentName))
return new NameFileFilter(sourceDocumentName);

if (!sourceDocumentExtensions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,64 @@ public void should_ignore_resource_file_when_matches_custom_source_file_extensio
awaitTermination(mojoThread);
}

@Test
public void should_ignore_resource_file_when_matches_custom_source_sourceDocumentName() throws IOException {
// given
final ConsoleHolder consoleHolder = ConsoleHolder.start();

final File srcDir = newOutputTestDirectory("refresh-mojo");
final File outputDir = newOutputTestDirectory("refresh-mojo");

final File sourceFile = new File(srcDir, "sourceFile.adoc");
final File resourceFile1 = new File(srcDir, "fakeImage.jpg");
final File resourceFile2 = new File(srcDir, "fakeImage.gif");

// when:
FileUtils.write(sourceFile, "= Document Title\n\nThis is test, only a test.", UTF_8);
FileUtils.write(resourceFile1, "= Not an image\n\nThis is reality a Adoc source with jpg extension", UTF_8);
FileUtils.write(resourceFile2, "Supposedly image content", UTF_8);

Thread mojoThread = runMojoAsynchronously(mojo -> {
mojo.backend = "html5";
mojo.sourceDirectory = srcDir;
mojo.outputDirectory = outputDir;
mojo.sourceDocumentName = "fakeImage.jpg";
});

// then: default source extensions and custom extensions are not copied as resources
consoleHolder.awaitProcessingAllSources();
final File targetSource = new File(outputDir, sourceFile.getName().replace("adoc", "html"));
assertThat(targetSource)
.doesNotExist();
final File targetResource1 = new File(outputDir, resourceFile1.getName().replace("jpg", "html"));
assertThat(FileUtils.readFileToString(targetResource1, UTF_8))
.contains("This is reality a Adoc source with jpg extension");
assertThat(new File(outputDir, resourceFile1.getName()))
.doesNotExist();
final File targetResource2 = new File(outputDir, resourceFile2.getName());
assertThat(targetResource2)
.exists();

// and when
FileUtils.write(resourceFile1, "= Not an image\n\nWow, this will be auto refreshed !", UTF_8);

// then: custom file extensions is processed as source
consoleHolder.awaitProcessingSource();
assertThat(FileUtils.readFileToString(targetResource1, UTF_8))
.contains("Wow, this will be auto refreshed");
assertThat(new File(outputDir, resourceFile1.getName()))
.doesNotExist();
assertThat(targetResource2)
.exists();
assertThat(targetSource)
.doesNotExist();

// cleanup
consoleHolder.input("exit");
consoleHolder.release();
awaitTermination(mojoThread);
}

@Test
public void should_copy_resource_when_new_resource_is_created() throws IOException {
// given
Expand Down

0 comments on commit 254af74

Please sign in to comment.