Skip to content

Commit

Permalink
Resource filtering fix (asciidoctor#226)
Browse files Browse the repository at this point in the history
* Fixed crash when using filtering from 'maven-filtering' module

* Added small change in README for styling reasons

* fixed 'resources' section documentation in README

* replaced usage of 'Runs' for 'Converts' in integration tests poms' names
  • Loading branch information
abelsromero authored Jul 15, 2016
1 parent 76db0da commit 46c97dc
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 14 deletions.
19 changes: 14 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,28 @@ There are several configuration options that the asciidoctor-maven-plugin uses,
sourceDirectory:: defaults to `${basedir}/src/main/asciidoc`
sourceDocumentName:: an override to process a single source file; defaults to all files in `${sourceDirectory}`
sourceDocumentExtensions:: (named `extensions` in v1.5.3 and below) a `List<String>` of non-standard file extensions to render. Currently ad, adoc, and asciidoc will be rendered by default
resources:: list of resource files to copy to the output directory (e.g., images, css). By default everything is copied
resources:: list of resource files to copy to the output directory (e.g., images, css). The configuration follows the same patterns as the `maven-resources-plugin`. If not set, all resources inside `sourceDirectory` are copied
+
[source, xml]
----
<resources>
<resource>
<!-- (Mandatory) Directory to copy from. Paths are relative to maven's ${baseDir} -->
<directory>DIRECTORY</directory>
<include>**/*.jpg</include>
<include>**/*.gif</include>
<exclude>**/.svn</exclude>
<!-- (Optional) Directory to copy to. By default uses the option `outputDirectory` -->
<targetPath>OUTPUT_DIR</targetPath>
<!-- (Optional) NOTE: SVN, GIT and other version control files are excluded by default, there's no need to add them -->
<excludes>
<exclude>**/.txt</exclude>
</excludes>
<!-- (Optional) If not set, includes all files but default exceptions mentioned -->
<includes>
<include>**/*.jpg</include>
<include>**/*.gif</include>
</includes>
</resource>
<resource>
...
...
<resources>
----
outputDirectory:: defaults to `${project.build.directory}/generated-docs`
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
<artifactId>maven-filtering</artifactId>
<version>${maven.filtering.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
<version>1.22</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/it/article-html-command/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Run Asciidoctor Article to Html with command line arguments</name>
<name>Converts Asciidoctor Article to Html with command line arguments</name>
<description>Runs asciidoctor-maven-plugin:process-asciidoc</description>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion src/it/article-html/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Run Asciidoctor Article to Html</name>
<name>Converts Asciidoctor Article to Html</name>
<description>Runs asciidoctor-maven-plugin:process-asciidoc</description>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion src/it/book-html/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Run Asciidoctor Article to Html</name>
<name>Converts Asciidoctor Article to Html</name>
<description>Runs asciidoctor-maven-plugin:process-asciidoc</description>

<properties>
Expand Down
1 change: 1 addition & 0 deletions src/it/resource-filtering/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean asciidoctor:process-asciidoc -Dcommand.property=command_value
49 changes: 49 additions & 0 deletions src/it/resource-filtering/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.asciidoctor</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Converts Asciidoctor Article to Html adding selected resources</name>
<description>Runs asciidoctor-maven-plugin:process-asciidoc</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<pom.property>pom_value</pom.property>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<sourceDirectory>src/main/doc</sourceDirectory>
<outputDirectory>target/docs</outputDirectory>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.build.sourceDirectory}</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>*.properties</include>
</includes>
<excludes>
<exclude>.*</exclude>
</excludes>
</resource>
</resources>
<backend>html</backend>
</configuration>
</plugin>
</plugins>
</build>
</project>
25 changes: 25 additions & 0 deletions src/it/resource-filtering/src/main/doc/sample.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Document Title
==============
Doc Writer <thedoc@asciidoctor.org>
:idprefix: id_

Preamble paragraph.

NOTE: This is test, only a test.

== Section A

*Section A* paragraph.

=== Section A Subsection

*Section A* 'subsection' paragraph.

== Section B

*Section B* paragraph.

.Section B list
* Item 1
* Item 2
* Item 3
15 changes: 15 additions & 0 deletions src/it/resource-filtering/src/main/java/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package example;

/**
* Using ${java.vendor} ${java.version}
*
* Command property: ${command.property}
* POM property: ${pom.property}
*/
public class StringUtils {
// tag::contains[]
public boolean contains(String haystack, String needle) {
return haystack.contains(needle);
}
// end::contains[]
}
25 changes: 25 additions & 0 deletions src/it/resource-filtering/validate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.*;


File outputDir = new File(basedir, "target/docs")

String[] expectedFiles = ['sample.html', 'invoker.properties', 'StringUtils.java']

// output files should be copied
for (String expectedFile : expectedFiles) {
File file = new File(outputDir, expectedFile)
println("Checking for existence of " + file)
if (!file.isFile()) {
throw new Exception("Missing file " + file)
}
}

String fileText = new File(outputDir, 'StringUtils.java').text
// properties in 'StringUtils.java' should be filtered (replaced)
['java.version', 'command.property', 'pom.property'].each {
if (fileText.contains(it)) {
throw new Exception("Propert " + file)
}
}

return true;
9 changes: 4 additions & 5 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,6 @@ private void prepareResources() {
resource.getExcludes().add(sourceDocumentName);
}
// exclude filename extensions if defined
if (sourceDocumentExtensions == null || sourceDocumentExtensions.isEmpty()) {
for (String docExtension : sourceDocumentExtensions) {
resource.getExcludes().add(docExtension);
}
}
resources.add(resource);
}

Expand All @@ -262,6 +257,9 @@ private void prepareResources() {
for (String value : AsciidoctorFileScanner.DEFAULT_FILE_EXTENSIONS) {
excludes.add(value);
}
for (String docExtension : sourceDocumentExtensions) {
resource.getExcludes().add("**/*." + docExtension);
}
// in case someone wants to include some of the default excluded files (.e.g., AsciiDoc docs)
excludes.removeAll(resource.getIncludes());
resource.getExcludes().addAll(excludes);
Expand All @@ -278,6 +276,7 @@ private void copyResources() throws MojoExecutionException {
new MavenResourcesExecution(resources, outputDirectory, project, FILE_ENCODING,
Collections.<String>emptyList(), Collections.<String>emptyList(), session);
resourcesExecution.setIncludeEmptyDirs(true);
resourcesExecution.setAddDefaultExcludes(true);
outputResourcesFiltering.filterResources(resourcesExecution);
} catch (MavenFilteringException e) {
throw new MojoExecutionException("Could not copy resources", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ class AsciidoctorMojoTest extends Specification {
// all folders and files are created because only image files are excluded
assertEqualsStructure(new File(relativeTestsPath).listFiles(DIRECTORY_FILTER), outputDir.listFiles(DIRECTORY_FILTER))
// images are excluded but not the rest of files
FileUtils.listFiles(outputDir, ['groovy'] as String[], true).size == 5
FileUtils.listFiles(outputDir, ['groovy'] as String[], true).size() == 5
FileUtils.listFiles(outputDir, ["jpg"] as String[], true).size() == 0
}
Expand Down Expand Up @@ -1071,4 +1071,55 @@ class AsciidoctorMojoTest extends Specification {
}
}
def "should not crash when enabling maven-resource filtering"() {
setup:
File outputDir = new File("$MULTIPLE_RESOURCES_OUTPUT/readme/${System.currentTimeMillis()}")
if (!outputDir.exists())
outputDir.mkdir()
when:
AsciidoctorMojo mojo = new AsciidoctorMojo()
mojo.sourceDirectory = new File('.')
mojo.sourceDocumentName = 'README.adoc'
mojo.resources = [[
directory: ".",
excludes : ['**/**'],
filtering: true
] as Resource]
mojo.backend = 'html5'
mojo.outputDirectory = outputDir
mojo.execute()
then:
def files = outputDir.listFiles({File f -> f.isFile()} as FileFilter)
// includes only 1 rendered AsciiDoc document
files.size() == 1
files.first().text.contains('Asciidoctor Maven Plugin')
}
def "should exclude custom source document Extensions by default"() {
setup:
File outputDir = new File("$MULTIPLE_RESOURCES_OUTPUT/readme/${System.currentTimeMillis()}")
if (!outputDir.exists())
outputDir.mkdir()
when:
AsciidoctorMojo mojo = new AsciidoctorMojo()
mojo.sourceDirectory = new File(DEFAULT_SOURCE_DIRECTORY)
mojo.sourceDocumentExtensions = ['ext']
mojo.resources = [[
directory: DEFAULT_SOURCE_DIRECTORY,
includes: ['**/*.adoc'],
excludes: ['**/**']
] as Resource]
mojo.backend = 'html5'
mojo.outputDirectory = outputDir
mojo.execute()
then:
def files = outputDir.listFiles({File f -> f.isFile()} as FileFilter)
FileUtils.listFiles(outputDir, ['ext'] as String[], true).isEmpty()
// includes only 1 rendered AsciiDoc document
def file = new File(outputDir, 'sample.html')
file.text.contains('Asciidoctor default stylesheet')
}
}
28 changes: 28 additions & 0 deletions src/test/resources/src/asciidoctor/sample.ext
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Document Title
==============
Doc Writer <thedoc@asciidoctor.org>
:idprefix: id_

Preamble paragraph.

NOTE: This is test, only a test.

== Section A

*Section A* paragraph.

=== Section A Subsection

*Section A* 'subsection' paragraph.

== Section B

*Section B* paragraph.

.Section B list
* Item 1
* Item 2
* Item 3

[source,ruby]
require 'asciidoctor'

0 comments on commit 46c97dc

Please sign in to comment.