Skip to content

Commit e3db6fc

Browse files
committed
Add Gradle bindings for Gherkin formatter
As we've created a Gherkin formatter as part of diffplug#907, we should make it possible to use it natively in Gradle, which requires we add it as a new supported type in `SpotlessExtension`.
1 parent ed842c6 commit e3db6fc

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

plugin-gradle/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
6969
- [SQL](#sql) ([dbeaver](#dbeaver), [prettier](#prettier))
7070
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
7171
- [JSON](#json)
72+
- [Gherkin](#gherkin)
7273
- Multiple languages
7374
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
7475
- javascript, jsx, angular, vue, flow, typescript, css, less, scss, html, json, graphql, markdown, ymaml
@@ -558,6 +559,34 @@ spotless {
558559
}
559560
```
560561
562+
## Gherkin
563+
564+
- `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)
565+
566+
```gradle
567+
spotless {
568+
gherkin {
569+
target 'src/**/*.feature' // you have to set the target manually
570+
simple() // has its own section below
571+
}
572+
}
573+
```
574+
575+
### simple
576+
577+
Uses a Gherkin pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
578+
579+
```gradle
580+
spotless {
581+
gherkin {
582+
target 'src/**/*.feature'
583+
simple()
584+
// optional: specify the number of spaces to use
585+
simple().indentWithSpaces(6)
586+
}
587+
}
588+
```
589+
561590
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
562591
563592
## Prettier
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2016-2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import javax.inject.Inject;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.gherkin.GherkinSimpleStep;
22+
23+
public class GherkinExtension extends FormatExtension {
24+
private static final int DEFAULT_INDENTATION = 4;
25+
static final String NAME = "gherkin";
26+
27+
@Inject
28+
public GherkinExtension(SpotlessExtension spotless) {
29+
super(spotless);
30+
}
31+
32+
@Override
33+
protected void setupTask(SpotlessTask task) {
34+
if (target == null) {
35+
throw noDefaultTargetException();
36+
}
37+
super.setupTask(task);
38+
}
39+
40+
public SimpleConfig simple() {
41+
return new SimpleConfig(DEFAULT_INDENTATION);
42+
}
43+
44+
public class SimpleConfig {
45+
private int indent;
46+
47+
public SimpleConfig(int indent) {
48+
this.indent = indent;
49+
addStep(createStep());
50+
}
51+
52+
public void indentWithSpaces(int indent) {
53+
this.indent = indent;
54+
replaceStep(createStep());
55+
}
56+
57+
private FormatterStep createStep() {
58+
return GherkinSimpleStep.create(indent, provisioner());
59+
}
60+
}
61+
62+
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ public void json(Action<JsonExtension> closure) {
175175
format(JsonExtension.NAME, JsonExtension.class, closure);
176176
}
177177

178+
/** Configures the special Gherkin-specific extension. */
179+
public void gherkin(Action<GherkinExtension> closure) {
180+
requireNonNull(closure);
181+
format(GherkinExtension.NAME, GherkinExtension.class, closure);
182+
}
183+
178184
/** Configures a custom extension. */
179185
public void format(String name, Action<FormatExtension> closure) {
180186
requireNonNull(name, "name");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Test;
21+
22+
public class GherkinExtensionTest extends GradleIntegrationHarness {
23+
@Test
24+
public void defaultFormatting() throws IOException {
25+
setFile("build.gradle").toLines(
26+
"buildscript { repositories { mavenCentral() } }",
27+
"plugins {",
28+
" id 'java'",
29+
" id 'com.diffplug.spotless'",
30+
"}",
31+
"spotless {",
32+
" gherkin {",
33+
" target 'examples/**/*.feature'",
34+
" simple()",
35+
"}",
36+
"}");
37+
setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature");
38+
setFile("examples/main/resources/example.feature").toResource("gherkin/minimalBefore.feature");
39+
gradleRunner().withArguments("spotlessApply").build();
40+
assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalBefore.feature");
41+
assertFile("examples/main/resources/example.feature").sameAsResource("gherkin/minimalAfter.feature");
42+
}
43+
44+
@Test
45+
public void formattingWithCustomNumberOfSpaces() throws IOException {
46+
setFile("build.gradle").toLines(
47+
"buildscript { repositories { mavenCentral() } }",
48+
"plugins {",
49+
" id 'java'",
50+
" id 'com.diffplug.spotless'",
51+
"}",
52+
"spotless {",
53+
" gherkin {",
54+
" target 'src/**/*.feature'",
55+
" simple().indentWithSpaces(6)",
56+
"}",
57+
"}");
58+
setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature");
59+
gradleRunner().withArguments("spotlessApply").build();
60+
assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalAfter6Spaces.feature");
61+
}
62+
}

0 commit comments

Comments
 (0)