Make snippet directory resolution more robust

The previous approach had (at least) two problems:

 - A Gradle build run from a directory that also contains a pom.xml
   would result in the resolver incorrectly identifing that Maven was
   being used
 - A Maven build run from a directory that did not could a pom and
   that used -f to provide the path to a pom would result in the
   resolver incorretly indentifying that Gradle was being used

With this commit, the resolver now uses the presence of the maven.home
system property to identify that Maven is being used. When Maven
is being used, rather than looking for a pom.xml in the working
directory, the resolver now locates the pom.xml by searching up the
directory hierarchy from the docdir.

Closes gh-297
This commit is contained in:
Andy Wilkinson
2016-10-22 21:58:11 +01:00
parent c13b7d03f5
commit 49596df0f3
4 changed files with 80 additions and 25 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.restdocs.asciidoctor;
import java.io.File;
import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Preprocessor;
import org.asciidoctor.extension.PreprocessorReader;
@@ -30,8 +28,7 @@ import org.asciidoctor.extension.PreprocessorReader;
*/
final class DefaultAttributesPreprocessor extends Preprocessor {
private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver(
new File("."));
private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver();
@Override
public PreprocessorReader process(Document document, PreprocessorReader reader) {

View File

@@ -17,6 +17,7 @@
package org.springframework.restdocs.asciidoctor;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
@@ -30,29 +31,42 @@ import java.util.Map;
*/
class SnippetsDirectoryResolver {
private final File root;
SnippetsDirectoryResolver(File root) {
this.root = root;
}
File getSnippetsDirectory(Map<String, Object> attributes) {
if (new File(this.root, "pom.xml").exists()) {
if (System.getProperty("maven.home") != null) {
return getMavenSnippetsDirectory(attributes);
}
return getGradleSnippetsDirectory(attributes);
}
private File getMavenSnippetsDirectory(Map<String, Object> attributes) {
Path rootPath = Paths.get(this.root.getAbsolutePath());
Path docDirPath = Paths.get((String) attributes.get("docdir"));
Path relativePath = docDirPath.relativize(rootPath);
return new File(relativePath.toFile(), "target/generated-snippets");
Path docdir = Paths.get(getRequiredAttribute(attributes, "docdir"));
return new File(docdir.relativize(findPom(docdir).getParent()).toFile(),
"target/generated-snippets");
}
private Path findPom(Path docdir) {
Path path = docdir;
while (path != null) {
Path pom = path.resolve("pom.xml");
if (Files.isRegularFile(pom)) {
return pom;
}
path = path.getParent();
}
throw new IllegalStateException("pom.xml not found in '" + docdir + "' or above");
}
private File getGradleSnippetsDirectory(Map<String, Object> attributes) {
return new File((String) attributes.get("projectdir"),
return new File(getRequiredAttribute(attributes, "projectdir"),
"build/generated-snippets");
}
private String getRequiredAttribute(Map<String, Object> attributes, String name) {
String attribute = (String) attributes.get(name);
if (attribute == null || attribute.length() == 0) {
throw new IllegalStateException(name + " attribute not found");
}
return attribute;
}
}