diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/AbstractSpringRestDocsGradleBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/AbstractSpringRestDocsGradleBuildCustomizer.java new file mode 100644 index 00000000000..17980026c69 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/AbstractSpringRestDocsGradleBuildCustomizer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2024 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 io.spring.start.site.extension.dependency.springrestdocs; + +import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; +import io.spring.initializr.generator.spring.build.BuildCustomizer; + +/** + * Abstract {@link BuildCustomizer Customizer} for a {@link GradleBuild} when the + * generated project * depends on Spring REST Docs. + * + * @author Moritz Halbritter + */ +abstract class AbstractSpringRestDocsGradleBuildCustomizer implements BuildCustomizer { + + @Override + public void customize(GradleBuild build) { + build.plugins().add("org.asciidoctor.jvm.convert", (plugin) -> plugin.setVersion("3.3.2")); + build.properties().property("snippetsDir", "file(\"build/generated-snippets\")"); + customizeForDialect(build); + } + + abstract void customizeForDialect(GradleBuild build); + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizer.java similarity index 68% rename from start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizer.java rename to start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizer.java index 903b69993eb..5bc9ed03066 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizer.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -20,17 +20,15 @@ import io.spring.initializr.generator.spring.build.BuildCustomizer; /** - * {@link BuildCustomizer Customizer} for a {@link GradleBuild} when the generated project - * depends on Spring REST Docs. + * {@link BuildCustomizer Customizer} for a Groovy {@link GradleBuild} when the generated + * project depends on Spring REST Docs. * * @author Andy Wilkinson */ -class SpringRestDocsGradleBuildCustomizer implements BuildCustomizer { +class SpringRestDocsGradleGroovyBuildCustomizer extends AbstractSpringRestDocsGradleBuildCustomizer { @Override - public void customize(GradleBuild build) { - build.plugins().add("org.asciidoctor.jvm.convert", (plugin) -> plugin.setVersion("3.3.2")); - build.properties().property("snippetsDir", "file(\"build/generated-snippets\")"); + void customizeForDialect(GradleBuild build) { build.tasks().customize("test", (task) -> task.invoke("outputs.dir", "snippetsDir")); build.tasks().customize("asciidoctor", (task) -> { task.invoke("inputs.dir", "snippetsDir"); diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizer.java new file mode 100644 index 00000000000..4e04126c447 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2024 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 io.spring.start.site.extension.dependency.springrestdocs; + +import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; +import io.spring.initializr.generator.spring.build.BuildCustomizer; + +/** + * {@link BuildCustomizer Customizer} for a Kotlin {@link GradleBuild} when the generated + * project depends on Spring REST Docs. + * + * @author Moritz Halbritter + */ +class SpringRestDocsGradleKotlinBuildCustomizer extends AbstractSpringRestDocsGradleBuildCustomizer { + + @Override + void customizeForDialect(GradleBuild build) { + build.tasks().customize("test", (task) -> task.invoke("outputs.dir", "project.extra[\"snippetsDir\"]!!")); + build.tasks().customize("asciidoctor", (task) -> { + task.invoke("inputs.dir", "project.extra[\"snippetsDir\"]!!"); + task.invoke("dependsOn", "tasks.test"); + }); + } + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfiguration.java index e33d30d1f7b..3f38b2b1f0f 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2024 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. @@ -29,25 +29,32 @@ * REST Docs. * * @author Andy Wilkinson + * @author Moritz Halbritter */ @ProjectGenerationConfiguration @ConditionalOnRequestedDependency("restdocs") public class SpringRestDocsProjectGenerationConfiguration { @Bean - public SpringRestDocsBuildCustomizer springRestDocsBuildCustomizer() { + SpringRestDocsBuildCustomizer springRestDocsBuildCustomizer() { return new SpringRestDocsBuildCustomizer(); } @Bean - @ConditionalOnBuildSystem(GradleBuildSystem.ID) - public SpringRestDocsGradleBuildCustomizer restDocsGradleBuildCustomizer() { - return new SpringRestDocsGradleBuildCustomizer(); + @ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY) + SpringRestDocsGradleGroovyBuildCustomizer restDocsGradleGroovyBuildCustomizer() { + return new SpringRestDocsGradleGroovyBuildCustomizer(); + } + + @Bean + @ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN) + SpringRestDocsGradleKotlinBuildCustomizer restDocsGradleKotlinBuildCustomizer() { + return new SpringRestDocsGradleKotlinBuildCustomizer(); } @Bean @ConditionalOnBuildSystem(MavenBuildSystem.ID) - public SpringRestDocsMavenBuildCustomizer restDocsMavenBuildCustomizer() { + SpringRestDocsMavenBuildCustomizer restDocsMavenBuildCustomizer() { return new SpringRestDocsMavenBuildCustomizer(); } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizerTests.java similarity index 89% rename from start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizerTests.java rename to start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizerTests.java index 725daf3cf2e..56263f84d2a 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleBuildCustomizerTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleGroovyBuildCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2024 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,13 +26,13 @@ import static org.assertj.core.api.Assertions.entry; /** - * Tests for {@link SpringRestDocsGradleBuildCustomizer}. + * Tests for {@link SpringRestDocsGradleGroovyBuildCustomizer}. * * @author Andy Wilkinson */ -class SpringRestDocsGradleBuildCustomizerTests { +class SpringRestDocsGradleGroovyBuildCustomizerTests { - private final SpringRestDocsGradleBuildCustomizer customizer = new SpringRestDocsGradleBuildCustomizer(); + private final SpringRestDocsGradleGroovyBuildCustomizer customizer = new SpringRestDocsGradleGroovyBuildCustomizer(); @Test void customizesGradleBuild() { diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizerTests.java new file mode 100644 index 00000000000..743c07652b0 --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsGradleKotlinBuildCustomizerTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2012-2024 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 io.spring.start.site.extension.dependency.springrestdocs; + +import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; +import io.spring.initializr.generator.buildsystem.gradle.GradleTask; +import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Invocation; +import io.spring.initializr.generator.buildsystem.gradle.StandardGradlePlugin; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +/** + * Tests for {@link SpringRestDocsGradleKotlinBuildCustomizer}. + * + * @author Moritz Halbritter + */ +class SpringRestDocsGradleKotlinBuildCustomizerTests { + + private final SpringRestDocsGradleKotlinBuildCustomizer customizer = new SpringRestDocsGradleKotlinBuildCustomizer(); + + @Test + void customizesGradleBuild() { + GradleBuild build = new GradleBuild(); + this.customizer.customize(build); + assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> { + assertThat(plugin.getId()).isEqualTo("org.asciidoctor.jvm.convert"); + assertThat(((StandardGradlePlugin) plugin).getVersion()).isEqualTo("3.3.2"); + }); + assertThat(build.properties().values()).contains(entry("snippetsDir", "file(\"build/generated-snippets\")")); + GradleTask testTask = build.tasks().get("test"); + assertThat(testTask).isNotNull(); + assertThat(testTask.getInvocations()).hasSize(1); + Invocation invocation = testTask.getInvocations().get(0); + assertThat(invocation.getTarget()).isEqualTo("outputs.dir"); + assertThat(invocation.getArguments()).containsExactly("project.extra[\"snippetsDir\"]!!"); + GradleTask asciidoctorTask = build.tasks().get("asciidoctor"); + assertThat(asciidoctorTask).isNotNull(); + assertThat(asciidoctorTask.getInvocations()).hasSize(2); + Invocation inputsDir = asciidoctorTask.getInvocations().get(0); + assertThat(inputsDir.getTarget()).isEqualTo("inputs.dir"); + assertThat(inputsDir.getArguments()).containsExactly("project.extra[\"snippetsDir\"]!!"); + Invocation dependsOn = asciidoctorTask.getInvocations().get(1); + assertThat(dependsOn.getTarget()).isEqualTo("dependsOn"); + assertThat(dependsOn.getArguments()).containsExactly("tasks.test"); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfigurationTests.java index c74518a5159..c7fd51bd193 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springrestdocs/SpringRestDocsProjectGenerationConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -31,6 +31,7 @@ * Tests for {@link SpringRestDocsProjectGenerationConfiguration}. * * @author Stephane Nicoll + * @author Moritz Halbritter */ class SpringRestDocsProjectGenerationConfigurationTests { @@ -49,13 +50,26 @@ void springRestDocsCustomizerMaven() { } @Test - void springRestDocsCustomizerGradle() { + void springRestDocsCustomizerGradleGroovy() { MutableProjectDescription description = new MutableProjectDescription(); - description.setBuildSystem(new GradleBuildSystem()); + description.setBuildSystem(new GradleBuildSystem(GradleBuildSystem.DIALECT_GROOVY)); + description.addDependency("restdocs", mock(Dependency.class)); + this.projectTester.configure(description, + (context) -> assertThat(context).getBeans(BuildCustomizer.class) + .containsKeys("restDocsGradleGroovyBuildCustomizer") + .doesNotContainKeys("restDocsGradleKotlinBuildCustomizer") + .doesNotContainKeys("restDocsMavenBuildCustomizer")); + } + + @Test + void springRestDocsCustomizerGradleKotlin() { + MutableProjectDescription description = new MutableProjectDescription(); + description.setBuildSystem(new GradleBuildSystem(GradleBuildSystem.DIALECT_KOTLIN)); description.addDependency("restdocs", mock(Dependency.class)); this.projectTester.configure(description, (context) -> assertThat(context).getBeans(BuildCustomizer.class) - .containsKeys("restDocsGradleBuildCustomizer") + .containsKeys("restDocsGradleKotlinBuildCustomizer") + .doesNotContainKeys("restDocsGradleGroovyBuildCustomizer") .doesNotContainKeys("restDocsMavenBuildCustomizer")); }