From a0bc4e4d0587b7e35a58a74f922f97bf3bdfc1d3 Mon Sep 17 00:00:00 2001 From: Clement Escoffier Date: Mon, 7 Apr 2014 10:29:36 +0200 Subject: [PATCH] Fix #98 and refactoring Signed-off-by: Clement Escoffier --- .../wisdom-asciidoc-maven-plugin/pom.xml | 5 + .../org/wisdom/asciidoc/AsciidocMojo.java | 111 +++++++++----- .../CustomExtensionDirectoryWalker.java | 12 +- .../org/wisdom/asciidoc/AsciidocMojoTest.java | 137 ++++++++++++++++++ .../src/test/resources/hello.ad | 12 ++ 5 files changed, 232 insertions(+), 45 deletions(-) create mode 100644 extensions/wisdom-asciidoc-maven-plugin/src/test/java/org/wisdom/asciidoc/AsciidocMojoTest.java create mode 100644 extensions/wisdom-asciidoc-maven-plugin/src/test/resources/hello.ad diff --git a/extensions/wisdom-asciidoc-maven-plugin/pom.xml b/extensions/wisdom-asciidoc-maven-plugin/pom.xml index 5cdd44d79..4a1a6c8e4 100644 --- a/extensions/wisdom-asciidoc-maven-plugin/pom.xml +++ b/extensions/wisdom-asciidoc-maven-plugin/pom.xml @@ -108,6 +108,11 @@ maven-shared-incremental 1.1 + + + org.assertj + assertj-core + diff --git a/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/AsciidocMojo.java b/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/AsciidocMojo.java index 564f5c0c9..2125ce69c 100644 --- a/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/AsciidocMojo.java +++ b/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/AsciidocMojo.java @@ -19,17 +19,21 @@ */ package org.wisdom.asciidoc; +import com.google.common.collect.ImmutableList; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.filefilter.NameFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.*; -import org.apache.maven.shared.filtering.MavenFileFilter; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; import org.asciidoctor.*; import org.wisdom.maven.Constants; import org.wisdom.maven.WatchingException; import org.wisdom.maven.mojos.AbstractWisdomWatcherMojo; +import org.wisdom.maven.utils.WatcherUtils; import java.io.File; import java.io.IOException; @@ -37,7 +41,7 @@ /** - * Transform Asciidoc files. + * Compiles Asciidoc files to HTML documents. */ @Mojo(name = "compile-asciidoc", threadSafe = false, requiresDependencyResolution = ResolutionScope.COMPILE, @@ -67,24 +71,29 @@ public class AsciidocMojo extends AbstractWisdomWatcherMojo implements Constants @Parameter(property = "sourceHighlighter", required = false) protected String sourceHighlighter = ""; @Parameter(property = "extensions") - protected List extensions = new ArrayList(); + protected List extensions = new ArrayList<>(); @Parameter(property = "embedAssets") protected boolean embedAssets = false; @Parameter protected String stylesheet; @Parameter protected String stylesheetDir; - private File internalSources; - private File destinationForInternals; - private File externalSources; - private File destinationForExternals; - private Asciidoctor instance; - @Component - private MavenFileFilter mavenFileFilter; + + File internalSources; + File destinationForInternals; + File externalSources; + File destinationForExternals; + + Asciidoctor instance; public void execute() throws MojoExecutionException { - this.internalSources = new File(basedir, MAIN_RESOURCES_DIR); + + if (extensions == null || extensions.isEmpty()) { + extensions = ImmutableList.of("ad", "asciidoc", "adoc"); + } + + this.internalSources = new File(basedir, MAIN_RESOURCES_DIR + "/assets"); this.destinationForInternals = new File(buildDirectory, "classes/assets"); this.externalSources = new File(basedir, ASSETS_SRC_DIR); @@ -95,8 +104,7 @@ public void execute() } final OptionsBuilder optionsBuilderExternals = OptionsBuilder.options().compact(compact) - .safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter) - .inPlace(true); + .safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter).inPlace(true); final OptionsBuilder optionsBuilderInternals = OptionsBuilder.options().compact(compact) .safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter).inPlace(true); @@ -154,31 +162,46 @@ protected Asciidoctor getAsciidoctorInstance() throws MojoExecutionException { } private List scanSourceFiles(File root) { - final List files; - if (extensions == null || extensions.isEmpty()) { - final DirectoryWalker directoryWalker = new AsciiDocDirectoryWalker(root.getAbsolutePath()); - files = directoryWalker.scan(); - } else { - final DirectoryWalker directoryWalker = new CustomExtensionDirectoryWalker(root.getAbsolutePath(), extensions); - files = directoryWalker.scan(); - } - return files; + final DirectoryWalker directoryWalker = new CustomExtensionDirectoryWalker(root.getAbsolutePath(), extensions); + return directoryWalker.scan(); } protected void renderFile(Map options, File f) throws IOException { - getLog().info("Compiling Asciidoc file >> " + f.getName()); File filtered; + boolean unfiltered; + + // Check whether the source was already copied to the destination directories (by the resource copy). if (FilenameUtils.directoryContains(internalSources.getCanonicalPath(), f.getCanonicalPath())) { filtered = findFileInDirectory(f, destinationForInternals); } else { filtered = findFileInDirectory(f, destinationForExternals); } + if (filtered == null) { + // It was not copied. getLog().error("Cannot find the filtered version of " + f.getAbsolutePath() + ", " + "using unprocessed file."); filtered = f; + unfiltered = true; + } else { + // It was copied. + unfiltered = false; } + instance.renderFile(filtered, options); + + // Move the file to the expected place if not filtered + if (unfiltered) { + String name = filtered.getName().substring(0, filtered.getName().lastIndexOf(".")) + ".html"; + File output = new File(filtered.getParentFile(), name); + if (output.isFile()) { + // Move... + File finalFile = getOutputHTMLFile(filtered); + FileUtils.moveFile(output, finalFile); + } else { + getLog().error("Cannot find the output file for " + filtered.getAbsolutePath()); + } + } } /** @@ -189,6 +212,10 @@ protected void renderFile(Map options, File f) throws IOExceptio * @return the found file or {@code null} if not found */ private File findFileInDirectory(File file, File directory) { + if (!directory.isDirectory()) { + return null; + } + Collection files = FileUtils.listFiles(directory, new NameFileFilter(file.getName()), TrueFileFilter.INSTANCE); @@ -204,18 +231,7 @@ private File findFileInDirectory(File file, File directory) { @Override public boolean accept(File file) { - if (extensions == null || extensions.isEmpty()) { - return file.getName().endsWith(".ad") || file.getName().endsWith(".adoc") || - file.getName().endsWith(".asciidoc"); - } else { - for (String ext : extensions) { - if (file.getName().endsWith(ext)) { - return true; - } - } - return false; - } - + return WatcherUtils.hasExtension(file, extensions); } @Override @@ -235,7 +251,28 @@ public boolean fileUpdated(File file) throws WatchingException { @Override public boolean fileDeleted(File file) throws WatchingException { - // TODO Fix this, by deleting the previously generated files. + File output = getOutputHTMLFile(file); + if (output != null && output.isFile()) { + FileUtils.deleteQuietly(output); + } return fileCreated(file); } + + private File getOutputHTMLFile(File input) { + File source; + File destination; + if (input.getAbsolutePath().startsWith(internalSources.getAbsolutePath())) { + source = internalSources; + destination = destinationForInternals; + } else if (input.getAbsolutePath().startsWith(externalSources.getAbsolutePath())) { + source = externalSources; + destination = destinationForExternals; + } else { + return null; + } + + String fileName = input.getName().substring(0, input.getName().lastIndexOf(".")) + ".html"; + String path = input.getParentFile().getAbsolutePath().substring(source.getAbsolutePath().length()); + return new File(destination, path + "/" + fileName); + } } diff --git a/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/CustomExtensionDirectoryWalker.java b/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/CustomExtensionDirectoryWalker.java index 7904603b2..f42f5e4dd 100644 --- a/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/CustomExtensionDirectoryWalker.java +++ b/extensions/wisdom-asciidoc-maven-plugin/src/main/java/org/wisdom/asciidoc/CustomExtensionDirectoryWalker.java @@ -19,6 +19,7 @@ */ package org.wisdom.asciidoc; +import org.apache.commons.io.FilenameUtils; import org.asciidoctor.AbstractDirectoryWalker; import java.io.File; @@ -36,13 +37,8 @@ public CustomExtensionDirectoryWalker(final String root, final List exte } @Override - protected boolean isAcceptedFile(final File filename) { - final String name = filename.getName(); - for (final String extension : extensions) { - if (name.endsWith(extension)) { - return true; - } - } - return false; + protected boolean isAcceptedFile(final File file) { + String extension = FilenameUtils.getExtension(file.getName()); + return extensions.contains(extension); } } diff --git a/extensions/wisdom-asciidoc-maven-plugin/src/test/java/org/wisdom/asciidoc/AsciidocMojoTest.java b/extensions/wisdom-asciidoc-maven-plugin/src/test/java/org/wisdom/asciidoc/AsciidocMojoTest.java new file mode 100644 index 000000000..5c7257d33 --- /dev/null +++ b/extensions/wisdom-asciidoc-maven-plugin/src/test/java/org/wisdom/asciidoc/AsciidocMojoTest.java @@ -0,0 +1,137 @@ +/* + * #%L + * Wisdom-Framework + * %% + * Copyright (C) 2013 - 2014 Wisdom Framework + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package org.wisdom.asciidoc; + +import com.google.common.collect.ImmutableList; +import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.MojoExecutionException; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Checks the behavior of the Asciidoc Mojo + */ +public class AsciidocMojoTest { + + File basedir = new File("target/workbench/project"); + + @Before + public void setUp() { + FileUtils.deleteQuietly(basedir); + } + + @Test + public void testInitializationWithNoFiles() throws MojoExecutionException { + AsciidocMojo mojo = new AsciidocMojo(); + mojo.basedir = basedir; + mojo.buildDirectory = new File(mojo.basedir, "target"); + mojo.execute(); + + assertThat(mojo.instance).isNotNull(); + assertThat(mojo.getAsciidoctorInstance()).isNotNull(); + assertThat(mojo.internalSources).isEqualTo(new File(mojo.basedir, "src/main/resources/assets")); + assertThat(mojo.externalSources).isEqualTo(new File(mojo.basedir, "src/main/assets")); + assertThat(mojo.destinationForInternals).isEqualTo(new File(mojo.buildDirectory, "classes/assets")); + assertThat(mojo.destinationForExternals).isEqualTo(new File(mojo.buildDirectory, "wisdom/assets")); + } + + @Test + public void testInitializationWithUnfilteredInternalAndExternalFilesUsingRegularExtensions() throws + MojoExecutionException, IOException { + AsciidocMojo mojo = new AsciidocMojo(); + mojo.basedir = basedir; + mojo.buildDirectory = new File(mojo.basedir, "target"); + mojo.doctype = "article"; + mojo.backend = "html5"; + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "src/main/resources/assets/doc/hello.ad")); + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "src/main/assets/doc/hello.asciidoc")); + mojo.execute(); + + final File internal = new File(mojo.destinationForInternals, "doc/hello.html"); + final File external = new File(mojo.destinationForExternals, "doc/hello.html"); + assertThat(internal).isFile(); + assertThat(external).isFile(); + + assertThat(FileUtils.readFileToString(internal)).contains("

Hello, " + + "Wisdom!

").contains("href=\"http://asciidoc.org\""); + assertThat(FileUtils.readFileToString(external)).contains("

Hello, " + + "Wisdom!

").contains("href=\"http://asciidoc.org\""); + } + + @Test + public void testInitializationWithFilteredInternalAndExternalFilesUsingRegularExtensions() throws + MojoExecutionException, IOException { + AsciidocMojo mojo = new AsciidocMojo(); + mojo.basedir = basedir; + mojo.buildDirectory = new File(mojo.basedir, "target"); + mojo.doctype = "article"; + mojo.backend = "html5"; + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "src/main/resources/assets/doc/hello.ad")); + // Filtered version: + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "target/classes/assets/doc/hello.ad")); + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "src/main/assets/doc/hello.asciidoc")); + // Filtered version: + FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir, + "target/wisdom/assets/doc/hello.asciidoc")); + + mojo.execute(); + + final File internal = new File(mojo.destinationForInternals, "doc/hello.html"); + final File external = new File(mojo.destinationForExternals, "doc/hello.html"); + assertThat(internal).isFile(); + assertThat(external).isFile(); + + assertThat(FileUtils.readFileToString(internal)).contains("

Hello, " + + "Wisdom!

").contains("href=\"http://asciidoc.org\""); + assertThat(FileUtils.readFileToString(external)).contains("

Hello, " + + "Wisdom!

").contains("href=\"http://asciidoc.org\""); + } + + @Test + public void testAccept() throws MojoExecutionException { + AsciidocMojo mojo = new AsciidocMojo(); + mojo.basedir = basedir; + mojo.buildDirectory = new File(mojo.basedir, "target"); + // Regular extensions. + mojo.execute(); + + assertThat(mojo.accept(new File("hello.ad"))).isTrue(); + assertThat(mojo.accept(new File("hello.adoc"))).isTrue(); + assertThat(mojo.accept(new File("hello.asciidoc"))).isTrue(); + assertThat(mojo.accept(new File("hello.html"))).isFalse(); + + mojo.extensions = ImmutableList.of("ascii"); + // Regular extensions. + mojo.execute(); + assertThat(mojo.accept(new File("hello.ad"))).isFalse(); + assertThat(mojo.accept(new File("hello.ascii"))).isTrue(); + + } +} diff --git a/extensions/wisdom-asciidoc-maven-plugin/src/test/resources/hello.ad b/extensions/wisdom-asciidoc-maven-plugin/src/test/resources/hello.ad new file mode 100644 index 000000000..88e11d718 --- /dev/null +++ b/extensions/wisdom-asciidoc-maven-plugin/src/test/resources/hello.ad @@ -0,0 +1,12 @@ += Hello, Wisdom! +Doc Writer + +An introduction to Asciidoc is available http://asciidoc.org[here]. + +== First Section + +* item 1 +* item 2 + +[source,java] +System.out.println("Hello Wisdom"); \ No newline at end of file