From ff18f8034df6084b2a1d2bdfc79ff6e5d5011c1b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 18 Jun 2019 12:03:42 +0100 Subject: [PATCH] Add AsciidoctorJ 1.6 and 2.0 compatibility AsciidoctorJ 1.6 is not backwards compatible with AsciidoctorJ 1.5 and AsciidoctorJ 2.0 is not backwards compatible with AsciidoctorJ 1.6 or 1.5. The backwards incompatibilities are such that they cannot be worked around using reflection so code needs to be compiled against each of the three versions. To this end, this commit splits spring-restdocs-asciidoctor into several separate modules: 1. spring-restdocs-asciidoctor-support 2. spring-restdocs-asciidoctor-1.5 3. spring-restdocs-asciidoctor-1.6 4. spring-restdocs-asciidoctor-2.0 spring-restdocs-asciidoctor-support contains support code that is not tied to a specific version of AsciidoctorJ and can be used with 1.5, 1.6 and 2.0. The other three modules contain code that is specific to a particular version of AsciidoctorJ. Each version-specific module uses class names that are unique across all three modules and is written in such a way that they will back off when used in an environment with a different version of AsciidoctorJ. The existing spring-restdocs-asciidoctor module has been modified to merge the version specific jars and the support jar together into a single jar that supports AsciidoctorJ 1.5, 1.6, and 2.0. The above-described changes should allow users to depend upon spring-restdocs-asciidoctor as before and to now be able to use AsciidoctorJ 1.5, 1.6, or 2.0. Closes gh-581 --- build.gradle | 26 ++----- gradle.properties | 3 + settings.gradle | 4 ++ spring-restdocs-asciidoctor-1.5/build.gradle | 10 +++ ...tAttributesAsciidoctorJ15Preprocessor.java | 4 +- ...stDocsAsciidoctorJ15ExtensionRegistry.java | 52 ++++++++++++++ ...ibutesAsciidoctorJ15PreprocessorTests.java | 35 ++++++---- spring-restdocs-asciidoctor-1.6/build.gradle | 10 +++ ...tAttributesAsciidoctorJ16Preprocessor.java | 39 +++++++++++ ...stDocsAsciidoctorJ16ExtensionRegistry.java | 52 ++++++++++++++ ...ibutesAsciidoctorJ16PreprocessorTests.java | 70 +++++++++++++++++++ spring-restdocs-asciidoctor-2.0/build.gradle | 10 +++ ...tAttributesAsciidoctorJ20Preprocessor.java | 39 +++++++++++ ...stDocsAsciidoctorJ20ExtensionRegistry.java | 13 ++-- ...ibutesAsciidoctorJ20PreprocessorTests.java | 70 +++++++++++++++++++ .../build.gradle | 6 ++ .../SnippetsDirectoryResolver.java | 2 +- .../extensions/operation_block_macro.rb | 0 .../SnippetsDirectoryResolverTests.java | 2 +- spring-restdocs-asciidoctor/build.gradle | 25 +++++-- .../restdocs/asciidoctor/package-info.java | 20 ------ ...sciidoctor.extension.spi.ExtensionRegistry | 3 +- ...ctor.jruby.extension.spi.ExtensionRegistry | 1 + 23 files changed, 427 insertions(+), 69 deletions(-) create mode 100644 spring-restdocs-asciidoctor-1.5/build.gradle rename spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessor.java => spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15Preprocessor.java (90%) create mode 100644 spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ15ExtensionRegistry.java rename spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessorTests.java => spring-restdocs-asciidoctor-1.5/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15PreprocessorTests.java (58%) create mode 100644 spring-restdocs-asciidoctor-1.6/build.gradle create mode 100644 spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16Preprocessor.java create mode 100644 spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ16ExtensionRegistry.java create mode 100644 spring-restdocs-asciidoctor-1.6/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16PreprocessorTests.java create mode 100644 spring-restdocs-asciidoctor-2.0/build.gradle create mode 100644 spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20Preprocessor.java rename spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsExtensionRegistry.java => spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ20ExtensionRegistry.java (70%) create mode 100644 spring-restdocs-asciidoctor-2.0/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20PreprocessorTests.java create mode 100644 spring-restdocs-asciidoctor-support/build.gradle rename {spring-restdocs-asciidoctor => spring-restdocs-asciidoctor-support}/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java (97%) rename {spring-restdocs-asciidoctor => spring-restdocs-asciidoctor-support}/src/main/resources/extensions/operation_block_macro.rb (100%) rename {spring-restdocs-asciidoctor => spring-restdocs-asciidoctor-support}/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java (98%) delete mode 100644 spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/package-info.java create mode 100644 spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry diff --git a/build.gradle b/build.gradle index 6d69b698..d794fd80 100644 --- a/build.gradle +++ b/build.gradle @@ -83,7 +83,6 @@ subprojects { dependency 'io.rest-assured:rest-assured:3.0.7' dependency 'org.apache.pdfbox:pdfbox:2.0.7' dependency 'org.assertj:assertj-core:2.9.1' - dependency 'org.asciidoctor:asciidoctorj:1.5.8.1' dependency 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16' dependency 'org.hamcrest:hamcrest-core:1.3' dependency 'org.hamcrest:hamcrest-library:1.3' @@ -123,27 +122,12 @@ subprojects { } -configure(subprojects - project(":docs")) { subproject -> +def codeProjects = subprojects - project(":docs") +configure(codeProjects) { apply plugin: 'io.spring.javaformat' apply plugin: 'checkstyle' apply from: "${rootProject.projectDir}/gradle/publish-maven.gradle" - if (project.hasProperty('platformVersion') && subproject.path != ':spring-restdocs-asciidoctor') { - apply plugin: 'spring-io' - - repositories { - maven { url "https://repo.spring.io/libs-snapshot" } - } - - dependencyManagement { - springIoTestRuntime { - imports { - mavenBom "io.spring.platform:platform-bom:${platformVersion}" - } - } - } - } - checkstyle { configFile = rootProject.file('config/checkstyle/checkstyle.xml') configProperties = [ 'checkstyle.config.dir' : rootProject.file('config/checkstyle') ] @@ -158,7 +142,10 @@ configure(subprojects - project(":docs")) { subproject -> checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:$javaFormatVersion" jacoco 'org.jacoco:org.jacoco.agent::runtime' } +} +def publishedCodeProjects = codeProjects.findAll { codeProject -> !codeProject.name.contains('spring-restdocs-asciidoctor')} +configure(publishedCodeProjects) { subproject -> javadoc { description = "Generates project-level javadoc for use in -javadoc jar" options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED @@ -243,8 +230,7 @@ task api (type: Javadoc) { options.links = javadocLinks options.addStringOption '-quiet' - source subprojects.findAll { project -> project.path != ":spring-restdocs-asciidoctor" } - .collect { project -> project.sourceSets.main.allJava } + source publishedCodeProjects.collect { project -> project.sourceSets.main.allJava } destinationDir = new File(buildDir, "api") diff --git a/gradle.properties b/gradle.properties index ce03bf97..165156e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,6 @@ version=2.0.4.BUILD-SNAPSHOT javaFormatVersion=0.0.7-SNAPSHOT org.gradle.daemon=false +asciidoctorj15Version=1.5.8.1 +asciidoctorj16Version=1.6.2 +asciidoctorj20Version=2.0.0-RC.3 diff --git a/settings.gradle b/settings.gradle index 1af633bf..eabf9a19 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,6 +2,10 @@ rootProject.name = 'spring-restdocs' include 'docs' include 'spring-restdocs-asciidoctor' +include 'spring-restdocs-asciidoctor-1.5' +include 'spring-restdocs-asciidoctor-1.6' +include 'spring-restdocs-asciidoctor-2.0' +include 'spring-restdocs-asciidoctor-support' include 'spring-restdocs-core' include 'spring-restdocs-mockmvc' include 'spring-restdocs-restassured' diff --git a/spring-restdocs-asciidoctor-1.5/build.gradle b/spring-restdocs-asciidoctor-1.5/build.gradle new file mode 100644 index 00000000..653cb107 --- /dev/null +++ b/spring-restdocs-asciidoctor-1.5/build.gradle @@ -0,0 +1,10 @@ +description = 'AsciidoctorJ 1.5 extensions for Spring REST Docs' + +dependencies { + compileOnly "org.asciidoctor:asciidoctorj:$asciidoctorj15Version" + implementation project(':spring-restdocs-asciidoctor-support') + testCompile 'junit:junit' + testCompile "org.asciidoctor:asciidoctorj:$asciidoctorj15Version" + testCompile 'org.assertj:assertj-core' + testRuntime project(':spring-restdocs-asciidoctor-support') +} \ No newline at end of file diff --git a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessor.java b/spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15Preprocessor.java similarity index 90% rename from spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessor.java rename to spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15Preprocessor.java index 5483cfcd..36d86fb0 100644 --- a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessor.java +++ b/spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15Preprocessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ import org.asciidoctor.extension.PreprocessorReader; * * @author Andy Wilkinson */ -final class DefaultAttributesPreprocessor extends Preprocessor { +final class DefaultAttributesAsciidoctorJ15Preprocessor extends Preprocessor { private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver(); diff --git a/spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ15ExtensionRegistry.java b/spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ15ExtensionRegistry.java new file mode 100644 index 00000000..529a141a --- /dev/null +++ b/spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ15ExtensionRegistry.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.extension.spi.ExtensionRegistry; + +/** + * AsciidoctorJ 1.5 {@link ExtensionRegistry} for Spring REST Docs. + * + * @author Andy Wilkinson + */ +public final class RestDocsAsciidoctorJ15ExtensionRegistry implements ExtensionRegistry { + + @Override + public void register(Asciidoctor asciidoctor) { + if (!asciidoctorJ15()) { + return; + } + asciidoctor.javaExtensionRegistry() + .preprocessor(new DefaultAttributesAsciidoctorJ15Preprocessor()); + asciidoctor.rubyExtensionRegistry() + .loadClass(RestDocsAsciidoctorJ15ExtensionRegistry.class + .getResourceAsStream("/extensions/operation_block_macro.rb")) + .blockMacro("operation", "OperationBlockMacro"); + } + + private boolean asciidoctorJ15() { + try { + return !Class.forName("org.asciidoctor.extension.JavaExtensionRegistry") + .isInterface(); + } + catch (Throwable ex) { + return false; + } + } + +} diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessorTests.java b/spring-restdocs-asciidoctor-1.5/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15PreprocessorTests.java similarity index 58% rename from spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessorTests.java rename to spring-restdocs-asciidoctor-1.5/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15PreprocessorTests.java index 12bf34fe..76129483 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessorTests.java +++ b/spring-restdocs-asciidoctor-1.5/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15PreprocessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,36 +26,45 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link DefaultAttributesPreprocessor}. + * Tests for {@link DefaultAttributesAsciidoctorJ15Preprocessor}. * * @author Andy Wilkinson */ -public class DefaultAttributesPreprocessorTests { +public class DefaultAttributesAsciidoctorJ15PreprocessorTests { @Test public void snippetsAttributeIsSet() { - Options options = new Options(); - options.setAttributes(new Attributes("projectdir=../../..")); - String converted = Asciidoctor.Factory.create().convert("{snippets}", options); + String converted = createAsciidoctor().convert("{snippets}", + createOptions("projectdir=../../..")); assertThat(converted) .contains("build" + File.separatorChar + "generated-snippets"); } @Test public void snippetsAttributeFromConvertArgumentIsNotOverridden() { - Options options = new Options(); - options.setAttributes(new Attributes("snippets=custom projectdir=../../..")); - String converted = Asciidoctor.Factory.create().convert("{snippets}", options); + String converted = createAsciidoctor().convert("{snippets}", + createOptions("snippets=custom projectdir=../../..")); assertThat(converted).contains("custom"); } @Test public void snippetsAttributeFromDocumentPreambleIsNotOverridden() { - Options options = new Options(); - options.setAttributes(new Attributes("projectdir=../../..")); - String converted = Asciidoctor.Factory.create() - .convert(":snippets: custom\n{snippets}", options); + String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}", + createOptions("projectdir=../../..")); assertThat(converted).contains("custom"); } + private Options createOptions(String attributes) { + Options options = new Options(); + options.setAttributes(new Attributes(attributes)); + return options; + } + + private Asciidoctor createAsciidoctor() { + Asciidoctor asciidoctor = Asciidoctor.Factory.create(); + asciidoctor.javaExtensionRegistry() + .preprocessor(new DefaultAttributesAsciidoctorJ15Preprocessor()); + return asciidoctor; + } + } diff --git a/spring-restdocs-asciidoctor-1.6/build.gradle b/spring-restdocs-asciidoctor-1.6/build.gradle new file mode 100644 index 00000000..a09724e6 --- /dev/null +++ b/spring-restdocs-asciidoctor-1.6/build.gradle @@ -0,0 +1,10 @@ +description = 'AsciidoctorJ 1.6 extensions for Spring REST Docs' + +dependencies { + compileOnly "org.asciidoctor:asciidoctorj:$asciidoctorj16Version" + implementation project(':spring-restdocs-asciidoctor-support') + testCompile 'junit:junit' + testCompile "org.asciidoctor:asciidoctorj:$asciidoctorj16Version" + testCompile 'org.assertj:assertj-core' + testRuntime project(':spring-restdocs-asciidoctor-support') +} \ No newline at end of file diff --git a/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16Preprocessor.java b/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16Preprocessor.java new file mode 100644 index 00000000..017126aa --- /dev/null +++ b/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16Preprocessor.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import org.asciidoctor.ast.Document; +import org.asciidoctor.extension.Preprocessor; +import org.asciidoctor.extension.PreprocessorReader; + +/** + * {@link Preprocessor} that sets defaults for REST Docs-related {@link Document} + * attributes. + * + * @author Andy Wilkinson + */ +final class DefaultAttributesAsciidoctorJ16Preprocessor extends Preprocessor { + + private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver(); + + @Override + public void process(Document document, PreprocessorReader reader) { + document.setAttribute("snippets", this.snippetsDirectoryResolver + .getSnippetsDirectory(document.getAttributes()), false); + } + +} diff --git a/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ16ExtensionRegistry.java b/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ16ExtensionRegistry.java new file mode 100644 index 00000000..91c07aa6 --- /dev/null +++ b/spring-restdocs-asciidoctor-1.6/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ16ExtensionRegistry.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.extension.spi.ExtensionRegistry; + +/** + * AsciidoctorJ 1.6 {@link ExtensionRegistry} for Spring REST Docs. + * + * @author Andy Wilkinson + */ +public final class RestDocsAsciidoctorJ16ExtensionRegistry implements ExtensionRegistry { + + @Override + public void register(Asciidoctor asciidoctor) { + if (!asciidoctorJ16()) { + return; + } + asciidoctor.javaExtensionRegistry() + .preprocessor(new DefaultAttributesAsciidoctorJ16Preprocessor()); + asciidoctor.rubyExtensionRegistry() + .loadClass(RestDocsAsciidoctorJ16ExtensionRegistry.class + .getResourceAsStream("/extensions/operation_block_macro.rb")) + .blockMacro("operation", "OperationBlockMacro"); + } + + private boolean asciidoctorJ16() { + try { + return Class.forName("org.asciidoctor.extension.JavaExtensionRegistry") + .isInterface(); + } + catch (Throwable ex) { + return false; + } + } + +} diff --git a/spring-restdocs-asciidoctor-1.6/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16PreprocessorTests.java b/spring-restdocs-asciidoctor-1.6/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16PreprocessorTests.java new file mode 100644 index 00000000..6055607e --- /dev/null +++ b/spring-restdocs-asciidoctor-1.6/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ16PreprocessorTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import java.io.File; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.Attributes; +import org.asciidoctor.Options; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DefaultAttributesAsciidoctorJ16Preprocessor}. + * + * @author Andy Wilkinson + */ +public class DefaultAttributesAsciidoctorJ16PreprocessorTests { + + @Test + public void snippetsAttributeIsSet() { + String converted = createAsciidoctor().convert("{snippets}", + createOptions("projectdir=../../..")); + assertThat(converted) + .contains("build" + File.separatorChar + "generated-snippets"); + } + + @Test + public void snippetsAttributeFromConvertArgumentIsNotOverridden() { + String converted = createAsciidoctor().convert("{snippets}", + createOptions("snippets=custom projectdir=../../..")); + assertThat(converted).contains("custom"); + } + + @Test + public void snippetsAttributeFromDocumentPreambleIsNotOverridden() { + String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}", + createOptions("projectdir=../../..")); + assertThat(converted).contains("custom"); + } + + private Options createOptions(String attributes) { + Options options = new Options(); + options.setAttributes(new Attributes(attributes)); + return options; + } + + private Asciidoctor createAsciidoctor() { + Asciidoctor asciidoctor = Asciidoctor.Factory.create(); + asciidoctor.javaExtensionRegistry() + .preprocessor(new DefaultAttributesAsciidoctorJ16Preprocessor()); + return asciidoctor; + } + +} diff --git a/spring-restdocs-asciidoctor-2.0/build.gradle b/spring-restdocs-asciidoctor-2.0/build.gradle new file mode 100644 index 00000000..48e8d5cf --- /dev/null +++ b/spring-restdocs-asciidoctor-2.0/build.gradle @@ -0,0 +1,10 @@ +description = 'AsciidoctorJ 2.0 extensions for Spring REST Docs' + +dependencies { + compileOnly "org.asciidoctor:asciidoctorj:$asciidoctorj20Version" + implementation project(':spring-restdocs-asciidoctor-support') + testCompile 'junit:junit' + testCompile "org.asciidoctor:asciidoctorj:$asciidoctorj15Version" + testCompile 'org.assertj:assertj-core' + testRuntime project(':spring-restdocs-asciidoctor-support') +} \ No newline at end of file diff --git a/spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20Preprocessor.java b/spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20Preprocessor.java new file mode 100644 index 00000000..054fd9d8 --- /dev/null +++ b/spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20Preprocessor.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import org.asciidoctor.ast.Document; +import org.asciidoctor.extension.Preprocessor; +import org.asciidoctor.extension.PreprocessorReader; + +/** + * {@link Preprocessor} that sets defaults for REST Docs-related {@link Document} + * attributes. + * + * @author Andy Wilkinson + */ +final class DefaultAttributesAsciidoctorJ20Preprocessor extends Preprocessor { + + private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver(); + + @Override + public void process(Document document, PreprocessorReader reader) { + document.setAttribute("snippets", this.snippetsDirectoryResolver + .getSnippetsDirectory(document.getAttributes()), false); + } + +} diff --git a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsExtensionRegistry.java b/spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ20ExtensionRegistry.java similarity index 70% rename from spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsExtensionRegistry.java rename to spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ20ExtensionRegistry.java index 4872c27b..5fd651f3 100644 --- a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsExtensionRegistry.java +++ b/spring-restdocs-asciidoctor-2.0/src/main/java/org/springframework/restdocs/asciidoctor/RestDocsAsciidoctorJ20ExtensionRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,24 +17,23 @@ package org.springframework.restdocs.asciidoctor; import org.asciidoctor.Asciidoctor; -import org.asciidoctor.extension.spi.ExtensionRegistry; +import org.asciidoctor.jruby.extension.spi.ExtensionRegistry; /** - * Asciidoctor {@link ExtensionRegistry} for Spring REST Docs. + * AsciidoctorJ 2.0 {@link ExtensionRegistry} for Spring REST Docs. * * @author Andy Wilkinson */ -public final class RestDocsExtensionRegistry implements ExtensionRegistry { +public final class RestDocsAsciidoctorJ20ExtensionRegistry implements ExtensionRegistry { @Override public void register(Asciidoctor asciidoctor) { asciidoctor.javaExtensionRegistry() - .preprocessor(new DefaultAttributesPreprocessor()); + .preprocessor(new DefaultAttributesAsciidoctorJ20Preprocessor()); asciidoctor.rubyExtensionRegistry() - .loadClass(RestDocsExtensionRegistry.class + .loadClass(RestDocsAsciidoctorJ20ExtensionRegistry.class .getResourceAsStream("/extensions/operation_block_macro.rb")) .blockMacro("operation", "OperationBlockMacro"); - } } diff --git a/spring-restdocs-asciidoctor-2.0/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20PreprocessorTests.java b/spring-restdocs-asciidoctor-2.0/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20PreprocessorTests.java new file mode 100644 index 00000000..7c4cbfd7 --- /dev/null +++ b/spring-restdocs-asciidoctor-2.0/src/test/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ20PreprocessorTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * 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 + * + * https://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. + */ + +package org.springframework.restdocs.asciidoctor; + +import java.io.File; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.Attributes; +import org.asciidoctor.Options; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DefaultAttributesAsciidoctorJ20Preprocessor}. + * + * @author Andy Wilkinson + */ +public class DefaultAttributesAsciidoctorJ20PreprocessorTests { + + @Test + public void snippetsAttributeIsSet() { + String converted = createAsciidoctor().convert("{snippets}", + createOptions("projectdir=../../..")); + assertThat(converted) + .contains("build" + File.separatorChar + "generated-snippets"); + } + + @Test + public void snippetsAttributeFromConvertArgumentIsNotOverridden() { + String converted = createAsciidoctor().convert("{snippets}", + createOptions("snippets=custom projectdir=../../..")); + assertThat(converted).contains("custom"); + } + + @Test + public void snippetsAttributeFromDocumentPreambleIsNotOverridden() { + String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}", + createOptions("projectdir=../../..")); + assertThat(converted).contains("custom"); + } + + private Options createOptions(String attributes) { + Options options = new Options(); + options.setAttributes(new Attributes(attributes)); + return options; + } + + private Asciidoctor createAsciidoctor() { + Asciidoctor asciidoctor = Asciidoctor.Factory.create(); + asciidoctor.javaExtensionRegistry() + .preprocessor(new DefaultAttributesAsciidoctorJ20Preprocessor()); + return asciidoctor; + } + +} diff --git a/spring-restdocs-asciidoctor-support/build.gradle b/spring-restdocs-asciidoctor-support/build.gradle new file mode 100644 index 00000000..5274e875 --- /dev/null +++ b/spring-restdocs-asciidoctor-support/build.gradle @@ -0,0 +1,6 @@ +description = 'Support code for AsciidoctorJ extensions for Spring REST Docs' + +dependencies { + testCompile 'junit:junit' + testCompile 'org.assertj:assertj-core' +} \ No newline at end of file diff --git a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java b/spring-restdocs-asciidoctor-support/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java similarity index 97% rename from spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java rename to spring-restdocs-asciidoctor-support/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java index 4889c51b..622853d1 100644 --- a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java +++ b/spring-restdocs-asciidoctor-support/src/main/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-restdocs-asciidoctor/src/main/resources/extensions/operation_block_macro.rb b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb similarity index 100% rename from spring-restdocs-asciidoctor/src/main/resources/extensions/operation_block_macro.rb rename to spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java b/spring-restdocs-asciidoctor-support/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java similarity index 98% rename from spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java rename to spring-restdocs-asciidoctor-support/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java index c17f6e8e..04d0aaab 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java +++ b/spring-restdocs-asciidoctor-support/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-restdocs-asciidoctor/build.gradle b/spring-restdocs-asciidoctor/build.gradle index dcb58c2f..ac464080 100644 --- a/spring-restdocs-asciidoctor/build.gradle +++ b/spring-restdocs-asciidoctor/build.gradle @@ -1,11 +1,28 @@ -description = 'Asciidoctor extensions for Spring REST Docs' +configurations { + merge + testRuntime { extendsFrom merge } +} dependencies { - provided 'org.asciidoctor:asciidoctorj' - testCompile 'junit:junit' + merge project(':spring-restdocs-asciidoctor-1.5') + merge project(':spring-restdocs-asciidoctor-1.6') + merge project(':spring-restdocs-asciidoctor-2.0') testCompile 'org.apache.pdfbox:pdfbox' - testCompile 'org.asciidoctor:asciidoctorj' + testCompile 'org.asciidoctor:asciidoctorj:1.5.8.1' testCompile 'org.assertj:assertj-core' + testCompile 'junit:junit' testCompile 'org.springframework:spring-core' testRuntime 'org.asciidoctor:asciidoctorj-pdf' +} + +jar { + from configurations.merge.collect { file -> zipTree(file) } +} + +matrixTest { + asciidoctorj { + group = 'org.asciidoctor' + artifact = 'asciidoctorj' + versions = [ asciidoctorj16Version, asciidoctorj20Version ] + } } \ No newline at end of file diff --git a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/package-info.java b/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/package-info.java deleted file mode 100644 index 8b586e73..00000000 --- a/spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * 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 - * - * https://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. - */ - -/** - * Spring REST Docs Asciidoctor extensions. - */ -package org.springframework.restdocs.asciidoctor; diff --git a/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.extension.spi.ExtensionRegistry b/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.extension.spi.ExtensionRegistry index 7c790a30..ccdd8d45 100644 --- a/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.extension.spi.ExtensionRegistry +++ b/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.extension.spi.ExtensionRegistry @@ -1 +1,2 @@ -org.springframework.restdocs.asciidoctor.RestDocsExtensionRegistry \ No newline at end of file +org.springframework.restdocs.asciidoctor.RestDocsAsciidoctorJ15ExtensionRegistry +org.springframework.restdocs.asciidoctor.RestDocsAsciidoctorJ16ExtensionRegistry diff --git a/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry b/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry new file mode 100644 index 00000000..7ae7da38 --- /dev/null +++ b/spring-restdocs-asciidoctor/src/main/resources/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry @@ -0,0 +1 @@ +org.springframework.restdocs.asciidoctor.RestDocsAsciidoctorJ20ExtensionRegistry