From b6c6db6a4e328875a2b714e32ee76783b16d2ff0 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sun, 28 Aug 2022 20:33:18 +0200 Subject: [PATCH] Remove unused class AsciidoctorFileScanner (#600) --- .../maven/AsciidoctorRefreshMojo.java | 6 +- .../maven/io/AsciidoctorFileScanner.java | 125 ------------------ .../maven/process/CopyResourcesProcessor.java | 34 ++++- .../maven/process/SourceDocumentFinder.java | 18 ++- .../refresh/ResourcesPatternBuilder.java | 9 +- .../java/org/asciidoctor/maven/TestUtils.java | 9 +- .../process/CopyResourcesProcessorTest.java | 2 +- 7 files changed, 54 insertions(+), 149 deletions(-) delete mode 100644 asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/io/AsciidoctorFileScanner.java diff --git a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java index 6cb57be0..4ad42903 100644 --- a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java +++ b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/AsciidoctorRefreshMojo.java @@ -18,9 +18,7 @@ import java.util.*; import static org.asciidoctor.maven.commons.StringUtils.isBlank; -import static org.asciidoctor.maven.io.AsciidoctorFileScanner.ASCIIDOC_NON_INTERNAL_REG_EXP; -import static org.asciidoctor.maven.process.SourceDocumentFinder.CUSTOM_FILE_EXTENSIONS_PATTERN_PREFIX; -import static org.asciidoctor.maven.process.SourceDocumentFinder.CUSTOM_FILE_EXTENSIONS_PATTERN_SUFFIX; +import static org.asciidoctor.maven.process.SourceDocumentFinder.*; @Mojo(name = "auto-refresh") public class AsciidoctorRefreshMojo extends AsciidoctorMojo { @@ -156,7 +154,7 @@ private IOFileFilter buildSourcesFileFilter() { return FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), new RegexFileFilter(stringJoiner.toString())); } - return FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), new RegexFileFilter(ASCIIDOC_NON_INTERNAL_REG_EXP)); + return FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), new RegexFileFilter(STANDARD_FILE_EXTENSIONS_PATTERN)); } } diff --git a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/io/AsciidoctorFileScanner.java b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/io/AsciidoctorFileScanner.java deleted file mode 100644 index 3f905b44..00000000 --- a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/io/AsciidoctorFileScanner.java +++ /dev/null @@ -1,125 +0,0 @@ -package org.asciidoctor.maven.io; - -import org.apache.maven.model.Resource; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codehaus.plexus.util.Scanner; - -import java.io.File; -import java.util.*; - -/** - * Recursively traverses directories returning the list of AsciiDoc files that match the applied filters. - * If no filters are set, AsciiDoc documents with extensions .adoc, .ad, .asc and .asciidoc are returned. - */ -public class AsciidoctorFileScanner { - - // 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[] 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 - 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" - }; - - /** - * Scans a resource directory (and sub-subdirectories) returning all AsciiDoc documents found. - * - * @param resource {@link Resource} to scan (the directory property is mandatory) - * @return List of found documents matching the resource properties - */ - public List scan(Resource resource) { - DirectoryScanner scanner = new DirectoryScanner(); - scanner.setBasedir(new File(resource.getDirectory())); - setupScanner(scanner, resource); - scanner.scan(); - List files = new ArrayList<>(); - for (String file : scanner.getIncludedFiles()) { - files.add(new File(resource.getDirectory(), file)); - } - return files; - } - - /** - * Scans a list of resources returning all AsciiDoc documents found. - * - * @param resources List of {@link Resource} to scan (the directory property is mandatory) - * @return List of found documents matching the resources properties - */ - public List scan(List resources) { - final List files = new ArrayList<>(); - for (Resource resource : resources) { - files.addAll(scan(resource)); - } - return files; - } - - /** - * Initializes the Scanner with the default values. - *
- * By default: - *
    - *
  • includes adds extension .adoc, .ad, .asc and .asciidoc - *
  • excludes adds filters to avoid hidden files and directoris beginning with undersore - *
- *

- * NOTE: Patterns both in inclusions and exclusions are automatically excluded. - */ - private void setupScanner(Scanner scanner, Resource resource) { - - if (isEmpty(resource.getIncludes())) { - scanner.setIncludes(DEFAULT_ASCIIDOC_EXTENSIONS); - } else { - scanner.setIncludes(resource.getIncludes().toArray(new String[]{})); - } - - if (isEmpty(resource.getExcludes())) { - scanner.setExcludes(IGNORED_FILE_NAMES); - } else { - scanner.setExcludes(mergeAndConvert(resource.getExcludes(), IGNORED_FILE_NAMES)); - } - // adds exclusions like SVN or GIT files - scanner.addDefaultExcludes(); - } - - private boolean isEmpty(List 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 array Array of String - * @return Array of String with all values - */ - private String[] mergeAndConvert(List list, String[] array) { - Set set = new HashSet<>(Arrays.asList(array)); - set.addAll(list); - return set.toArray(new String[set.size()]); - } - -} diff --git a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/CopyResourcesProcessor.java b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/CopyResourcesProcessor.java index 24d1eb4e..2029ab59 100644 --- a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/CopyResourcesProcessor.java +++ b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/CopyResourcesProcessor.java @@ -2,7 +2,6 @@ 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; @@ -25,6 +24,32 @@ */ public class CopyResourcesProcessor implements ResourcesProcessor { + // 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 static String[] DEFAULT_ASCIIDOC_EXTENSIONS = {"**/*.adoc", "**/*.ad", "**/*.asc", "**/*.asciidoc"}; + + // Files and directories beginning with underscore are ignored + private static String[] INTERNAL_FOLDERS_AND_FILES_PATTERNS = { + "**/_*.*", + "**/_*", + "**/.*", + "**/_*/**/*.*", + }; + /* * (non-Javadoc) * @@ -64,13 +89,14 @@ private List prepareResources(File sourceDirectory, AsciidoctorMojo co for (Resource resource : resources) { List excludes = new ArrayList<>(); - for (String value : AsciidoctorFileScanner.INTERNAL_FOLDERS_AND_FILES_PATTERNS) { + for (String value : INTERNAL_FOLDERS_AND_FILES_PATTERNS) { excludes.add(value); } - for (String value : AsciidoctorFileScanner.IGNORED_FILE_NAMES) { + for (String value : IGNORED_FILE_NAMES) { excludes.add("**/" + value); } - for (String value : AsciidoctorFileScanner.DEFAULT_ASCIIDOC_EXTENSIONS) { + + for (String value : DEFAULT_ASCIIDOC_EXTENSIONS) { excludes.add(value); } // exclude filename extensions if defined diff --git a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java index c746fdb5..25b42f9a 100644 --- a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java +++ b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/process/SourceDocumentFinder.java @@ -20,13 +20,23 @@ */ public class SourceDocumentFinder { - /** Pattern for matching standard file extensions. */ - private static final String STANDARD_FILE_EXTENSIONS_PATTERN = "^[^_.].*\\.a((sc(iidoc)?)|d(oc)?)$"; + // 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)?)"; - /** Prefix for matching custom file extensions. */ + /** + * Pattern for matching standard file extensions. + */ + public static final String STANDARD_FILE_EXTENSIONS_PATTERN = "^[^_.].*\\." + ASCIIDOC_FILE_EXTENSIONS_REG_EXP + "$"; + + /** + * Prefix for matching custom file extensions. + */ public static final String CUSTOM_FILE_EXTENSIONS_PATTERN_PREFIX = "^[^_.].*\\.("; - /** Suffix for matching custom file extensions. */ + /** + * Suffix for matching custom file extensions. + */ public static final String CUSTOM_FILE_EXTENSIONS_PATTERN_SUFFIX = ")$"; /** diff --git a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/refresh/ResourcesPatternBuilder.java b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/refresh/ResourcesPatternBuilder.java index 2bba901e..6eb00545 100644 --- a/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/refresh/ResourcesPatternBuilder.java +++ b/asciidoctor-maven-plugin/src/main/java/org/asciidoctor/maven/refresh/ResourcesPatternBuilder.java @@ -1,14 +1,13 @@ 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.asciidoctor.maven.commons.StringUtils.isBlank; -import static org.asciidoctor.maven.io.AsciidoctorFileScanner.ASCIIDOC_FILE_EXTENSIONS_REG_EXP; +import static org.asciidoctor.maven.process.CopyResourcesProcessor.IGNORED_FILE_NAMES; +import static org.asciidoctor.maven.process.SourceDocumentFinder.ASCIIDOC_FILE_EXTENSIONS_REG_EXP; /** * Builds regular expression to include all valid resources, as well as exclude invalid ones @@ -32,7 +31,7 @@ public String build() { if (!sourceDocumentExtensions.isEmpty()) filePattern.add(String.join("|", sourceDocumentExtensions)); - final String specialFiles = Arrays.stream(AsciidoctorFileScanner.IGNORED_FILE_NAMES) + final String specialFiles = Arrays.stream(IGNORED_FILE_NAMES) .map(pattern -> pattern.replaceAll("\\*", ".*")) .map(pattern -> pattern.replaceAll("\\.", "\\\\.")) .collect(Collectors.joining("|")); @@ -41,7 +40,7 @@ public String build() { .append("^") .append("(?!(" + specialFiles + (isBlank(sourceDocumentName) ? "" : "|" + sourceDocumentName) + "))") .append("[^_.].*\\.(?!(") - .append(filePattern.toString()) + .append(filePattern) .append(")).*$") .toString(); } diff --git a/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/TestUtils.java b/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/TestUtils.java index 4a4d8471..1399fceb 100644 --- a/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/TestUtils.java +++ b/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/TestUtils.java @@ -4,7 +4,6 @@ import org.apache.commons.io.IOUtils; import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.project.MavenProject; -import org.asciidoctor.maven.io.AsciidoctorFileScanner; import org.asciidoctor.maven.log.LogHandler; import org.asciidoctor.maven.model.Resource; import org.assertj.core.api.Assertions; @@ -18,6 +17,7 @@ import java.util.stream.Collectors; import static java.util.Collections.singletonList; +import static org.asciidoctor.maven.process.SourceDocumentFinder.STANDARD_FILE_EXTENSIONS_PATTERN; import static org.codehaus.plexus.util.ReflectionUtils.setVariableValueInObject; import static org.mockito.Mockito.when; @@ -177,11 +177,8 @@ public static void assertEqualsStructure(File[] expected, File[] actual) { File[] htmls = actualFile.listFiles(f -> f.getName().endsWith("html")); if (htmls.length > 0) { - File[] asciidocs = expectedFile.listFiles(f -> { - String asciidocFilePattern = ".*\\." + AsciidoctorFileScanner.ASCIIDOC_FILE_EXTENSIONS_REG_EXP + "$"; - return f.getName().matches(asciidocFilePattern) && !f.getName().startsWith("_") && !f.getName().startsWith("."); - }); - Assertions.assertThat(htmls).hasSize(asciidocs.length); + File[] asciiDocs = expectedFile.listFiles(f -> f.getName().matches(STANDARD_FILE_EXTENSIONS_PATTERN)); + Assertions.assertThat(htmls).hasSize(asciiDocs.length); } File[] actualChildren = actualFile.listFiles(File::isDirectory); assertEqualsStructure(expectedChildren, actualChildren); diff --git a/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/process/CopyResourcesProcessorTest.java b/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/process/CopyResourcesProcessorTest.java index a1c3539d..ddc926be 100644 --- a/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/process/CopyResourcesProcessorTest.java +++ b/asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/process/CopyResourcesProcessorTest.java @@ -14,8 +14,8 @@ import java.util.List; import java.util.UUID; -import static org.asciidoctor.maven.io.AsciidoctorFileScanner.IGNORED_FILE_NAMES; import static org.asciidoctor.maven.io.TestFilesHelper.createFileWithContent; +import static org.asciidoctor.maven.process.CopyResourcesProcessor.IGNORED_FILE_NAMES; import static org.assertj.core.api.Assertions.assertThat; public class CopyResourcesProcessorTest {