-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove unused encoding from resource processing * Created CopyResourcesProcessor * ResourcesProcessor no longer throws MojoExecutionException
- Loading branch information
1 parent
6d578a9
commit f2cdb1e
Showing
10 changed files
with
596 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...ctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/CopyResourcesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package org.asciidoctor.maven.process; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.asciidoctor.maven.AsciidoctorMojo; | ||
import org.asciidoctor.maven.io.AsciidoctorFileScanner; | ||
import org.asciidoctor.maven.model.Resource; | ||
import org.codehaus.plexus.util.DirectoryScanner; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.asciidoctor.maven.commons.StringUtils.isBlank; | ||
|
||
/** | ||
* {@link ResourcesProcessor} implementation that copies all valid resources from | ||
* a source directory to an output one. | ||
* <p/> | ||
* Following resources are not valid: | ||
* - AsciiDoc documents: based on file extension. | ||
* - Asciidoctor Docinfo files. | ||
* - Internal files and folders: those not starting with underscore '_'. | ||
*/ | ||
public class CopyResourcesProcessor implements ResourcesProcessor { | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see | ||
* org.asciidoctor.maven.process.ResourcesProcessor#process(File sourceRootDirectory, File outputRootDirectory, AsciidoctorMojo configuration) | ||
*/ | ||
@Override | ||
public void process(File sourceRootDirectory, File outputRootDirectory, AsciidoctorMojo configuration) { | ||
final List<Resource> finalResources = prepareResources(sourceRootDirectory, configuration); | ||
copyResources(finalResources, outputRootDirectory); | ||
} | ||
|
||
/** | ||
* Initializes resources attribute excluding AsciiDoc documents, internal directories/files (those prefixed with | ||
* underscore), and docinfo files. | ||
* By default everything in the sources directories is copied. | ||
* | ||
* @return Collection of resources with properly configured includes and excludes conditions. | ||
*/ | ||
private List<Resource> prepareResources(File sourceDirectory, AsciidoctorMojo configuration) { | ||
final List<Resource> resources = configuration.getResources() != null | ||
? configuration.getResources() | ||
: new ArrayList<>(); | ||
if (resources.isEmpty()) { | ||
// we don't want to copy files considered sources | ||
Resource resource = new Resource(); | ||
resource.setDirectory(sourceDirectory.getAbsolutePath()); | ||
// exclude sourceDocumentName if defined | ||
if (!isBlank(configuration.getSourceDocumentName())) { | ||
resource.getExcludes() | ||
.add(configuration.getSourceDocumentName()); | ||
} | ||
resources.add(resource); | ||
} | ||
|
||
// All resources must exclude AsciiDoc documents and folders beginning with underscore | ||
for (Resource resource : resources) { | ||
|
||
List<String> excludes = new ArrayList<>(); | ||
for (String value : AsciidoctorFileScanner.INTERNAL_FOLDERS_AND_FILES_PATTERNS) { | ||
excludes.add(value); | ||
} | ||
for (String value : AsciidoctorFileScanner.IGNORED_FILE_NAMES) { | ||
excludes.add("**/" + value); | ||
} | ||
for (String value : AsciidoctorFileScanner.DEFAULT_ASCIIDOC_EXTENSIONS) { | ||
excludes.add(value); | ||
} | ||
// exclude filename extensions if defined | ||
for (String docExtension : configuration.getSourceDocumentExtensions()) { | ||
resource.getExcludes().add("**/*." + docExtension); | ||
} | ||
// in case someone wants to include some of the default excluded files (e.g. AsciiDoc sources) | ||
excludes.removeAll(resource.getIncludes()); | ||
resource.getExcludes().addAll(excludes); | ||
} | ||
return resources; | ||
} | ||
|
||
/** | ||
* Copies the resources defined in the 'resources' attribute. | ||
* | ||
* @param resources Collection of {@link Resource} defining what resources to {@code outputDirectory}. | ||
* @param outputDirectory Directory where to copy resources. | ||
*/ | ||
private void copyResources(List<Resource> resources, File outputDirectory) { | ||
|
||
resources.stream() | ||
.filter(resource -> new File(resource.getDirectory()).exists()) | ||
.forEach(resource -> { | ||
DirectoryScanner directoryScanner = new DirectoryScanner(); | ||
directoryScanner.setBasedir(resource.getDirectory()); | ||
|
||
if (resource.getIncludes().isEmpty()) | ||
directoryScanner.setIncludes(new String[]{"**/*.*", "**/*"}); | ||
else | ||
directoryScanner.setIncludes(resource.getIncludes().toArray(new String[0])); | ||
|
||
directoryScanner.setExcludes(resource.getExcludes().toArray(new String[0])); | ||
directoryScanner.setFollowSymlinks(false); | ||
directoryScanner.scan(); | ||
|
||
for (String includedFile : directoryScanner.getIncludedFiles()) { | ||
File source = new File(directoryScanner.getBasedir(), includedFile); | ||
copyFileToDirectory(source, resource, outputDirectory); | ||
} | ||
}); | ||
} | ||
|
||
|
||
private void copyFileToDirectory(File source, Resource resource, File outputDirectory) { | ||
try { | ||
final File target = resource.getTargetPath() == null | ||
? outputDirectory | ||
: composeTargetPath(resource, outputDirectory); | ||
|
||
Path sourceDirectoryPath = new File(resource.getDirectory()).toPath(); | ||
Path sourcePath = source.toPath(); | ||
Path relativize = sourceDirectoryPath.relativize(sourcePath); | ||
|
||
if (relativize.getParent() == null) { | ||
FileUtils.copyFileToDirectory(source, target); | ||
} else { | ||
Path realTarget = target.toPath().resolve(relativize.getParent()); | ||
File realTargetFile = realTarget.toFile(); | ||
FileUtils.forceMkdir(realTargetFile); | ||
FileUtils.copyFileToDirectory(source, realTargetFile); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static File composeTargetPath(Resource resource, File outputDirectory) { | ||
final File targetFile = new File(resource.getTargetPath()); | ||
return targetFile.isAbsolute() | ||
? targetFile | ||
: new File(outputDirectory, resource.getTargetPath()); | ||
} | ||
} |
11 changes: 8 additions & 3 deletions
11
asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/ResourcesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package org.asciidoctor.maven.process; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.asciidoctor.maven.AsciidoctorMojo; | ||
|
||
import java.io.File; | ||
|
||
public interface ResourcesProcessor { | ||
|
||
void process(File sourcesRootDirectory, File outputRootDirectory, | ||
String encoding, AsciidoctorMojo configuration) throws MojoExecutionException; | ||
/** | ||
* Identifies requires resources and prepares them based on configuration. | ||
* | ||
* @param sourceRootDirectory starting directory to search resources. 'configuration' may add or modify it. | ||
* @param outputRootDirectory target directory to place final resources when copying is required. | ||
* @param configuration Asciidoctor conversion configuration | ||
*/ | ||
void process(File sourceRootDirectory, File outputRootDirectory, AsciidoctorMojo configuration); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.