Skip to content

Commit

Permalink
Fix RESTdocs build when using Gradle with the Kotlin DSL
Browse files Browse the repository at this point in the history
Closes gh-581
  • Loading branch information
mhalbritter committed Feb 19, 2024
1 parent fd6e0d4 commit f9aa3d4
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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<GradleBuild> {

@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);

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<GradleBuild> {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
});
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* Tests for {@link SpringRestDocsProjectGenerationConfiguration}.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class SpringRestDocsProjectGenerationConfigurationTests {

Expand All @@ -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"));
}

Expand Down

0 comments on commit f9aa3d4

Please sign in to comment.