From e3db6fc0974ac27dacbccd194e0c6aba30bfa7ad Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Thu, 2 Sep 2021 17:16:37 +0100 Subject: [PATCH] Add Gradle bindings for Gherkin formatter As we've created a Gherkin formatter as part of #907, we should make it possible to use it natively in Gradle, which requires we add it as a new supported type in `SpotlessExtension`. --- plugin-gradle/README.md | 29 +++++++++ .../gradle/spotless/GherkinExtension.java | 62 +++++++++++++++++++ .../gradle/spotless/SpotlessExtension.java | 6 ++ .../gradle/spotless/GherkinExtensionTest.java | 62 +++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.java diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 28f8f85b54..92d8b1553f 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -69,6 +69,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [SQL](#sql) ([dbeaver](#dbeaver), [prettier](#prettier)) - [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier)) - [JSON](#json) + - [Gherkin](#gherkin) - Multiple languages - [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection)) - javascript, jsx, angular, vue, flow, typescript, css, less, scss, html, json, graphql, markdown, ymaml @@ -558,6 +559,34 @@ spotless { } ``` +## Gherkin + +- `com.diffplug.gradle.spotless.GherkinExtension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/5.15.0/com/diffplug/gradle/spotless/GherkinExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java) + +```gradle +spotless { + gherkin { + target 'src/**/*.feature' // you have to set the target manually + simple() // has its own section below + } +} +``` + +### simple + +Uses a Gherkin pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects: + +```gradle +spotless { + gherkin { + target 'src/**/*.feature' + simple() + // optional: specify the number of spaces to use + simple().indentWithSpaces(6) + } +} +``` + ## Prettier diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java new file mode 100644 index 0000000000..59ff16805f --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2021 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import javax.inject.Inject; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.gherkin.GherkinSimpleStep; + +public class GherkinExtension extends FormatExtension { + private static final int DEFAULT_INDENTATION = 4; + static final String NAME = "gherkin"; + + @Inject + public GherkinExtension(SpotlessExtension spotless) { + super(spotless); + } + + @Override + protected void setupTask(SpotlessTask task) { + if (target == null) { + throw noDefaultTargetException(); + } + super.setupTask(task); + } + + public SimpleConfig simple() { + return new SimpleConfig(DEFAULT_INDENTATION); + } + + public class SimpleConfig { + private int indent; + + public SimpleConfig(int indent) { + this.indent = indent; + addStep(createStep()); + } + + public void indentWithSpaces(int indent) { + this.indent = indent; + replaceStep(createStep()); + } + + private FormatterStep createStep() { + return GherkinSimpleStep.create(indent, provisioner()); + } + } + +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 7959bdc71d..ba42d75958 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -175,6 +175,12 @@ public void json(Action closure) { format(JsonExtension.NAME, JsonExtension.class, closure); } + /** Configures the special Gherkin-specific extension. */ + public void gherkin(Action closure) { + requireNonNull(closure); + format(GherkinExtension.NAME, GherkinExtension.class, closure); + } + /** Configures a custom extension. */ public void format(String name, Action closure) { requireNonNull(name, "name"); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.java new file mode 100644 index 0000000000..4b1fdc7efe --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2021 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import java.io.IOException; + +import org.junit.Test; + +public class GherkinExtensionTest extends GradleIntegrationHarness { + @Test + public void defaultFormatting() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'java'", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " gherkin {", + " target 'examples/**/*.feature'", + " simple()", + "}", + "}"); + setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); + setFile("examples/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalBefore.feature"); + assertFile("examples/main/resources/example.feature").sameAsResource("gherkin/minimalAfter.feature"); + } + + @Test + public void formattingWithCustomNumberOfSpaces() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'java'", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " gherkin {", + " target 'src/**/*.feature'", + " simple().indentWithSpaces(6)", + "}", + "}"); + setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalAfter6Spaces.feature"); + } +}