Prepare for Asciidoctor Gradle Plugin 2.0 by looking for gradle-projectdir

Closes gh-562
This commit is contained in:
Andy Wilkinson
2018-11-21 15:17:51 +00:00
parent 1966640520
commit b694298720
2 changed files with 37 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Supplier;
/**
* Resolves the directory from which snippets can be read for inclusion in an Asciidoctor
@@ -57,13 +58,23 @@ class SnippetsDirectoryResolver {
}
private File getGradleSnippetsDirectory(Map<String, Object> attributes) {
return new File(getRequiredAttribute(attributes, "projectdir"),
return new File(
getRequiredAttribute(attributes, "gradle-projectdir",
() -> getRequiredAttribute(attributes, "projectdir")),
"build/generated-snippets");
}
private String getRequiredAttribute(Map<String, Object> attributes, String name) {
return getRequiredAttribute(attributes, name, null);
}
private String getRequiredAttribute(Map<String, Object> attributes, String name,
Supplier<String> fallback) {
String attribute = (String) attributes.get(name);
if (attribute == null || attribute.length() == 0) {
if (fallback != null) {
return fallback.get();
}
throw new IllegalStateException(name + " attribute not found");
}
return attribute;

View File

@@ -78,7 +78,30 @@ public class SnippetsDirectoryResolverTests {
}
@Test
public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectDir()
public void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdir()
throws IOException {
Map<String, Object> attributes = new HashMap<>();
attributes.put("gradle-projectdir", "project/dir");
File snippetsDirectory = new SnippetsDirectoryResolver()
.getSnippetsDirectory(attributes);
assertThat(snippetsDirectory)
.isEqualTo(new File("project/dir/build/generated-snippets"));
}
@Test
public void gradleProjectsUseBuildGeneratedSnippetsBeneathGradleProjectdirWhenBothItAndProjectdirAreSet()
throws IOException {
Map<String, Object> attributes = new HashMap<>();
attributes.put("gradle-projectdir", "project/dir");
attributes.put("projectdir", "fallback/dir");
File snippetsDirectory = new SnippetsDirectoryResolver()
.getSnippetsDirectory(attributes);
assertThat(snippetsDirectory)
.isEqualTo(new File("project/dir/build/generated-snippets"));
}
@Test
public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectdirWhenGradleProjectdirIsNotSet()
throws IOException {
Map<String, Object> attributes = new HashMap<>();
attributes.put("projectdir", "project/dir");
@@ -89,7 +112,7 @@ public class SnippetsDirectoryResolverTests {
}
@Test
public void illegalStateWhenProjectdirAttributeIsNotSetInGradleProject()
public void illegalStateWhenGradleProjectdirAndProjectdirAttributesAreNotSetInGradleProject()
throws IOException {
Map<String, Object> attributes = new HashMap<>();
this.thrown.expect(IllegalStateException.class);