Skip to content

Commit 2f328ec

Browse files
committed
feat: add tableTest format type for .table files
Add standalone .table file formatting support via a new tableTest format type in both Gradle and Maven plugins.
1 parent 6d4319c commit 2f328ec

File tree

15 files changed

+208
-2
lines changed

15 files changed

+208
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
- Add `tableTest` format type for standalone `.table` files.
1315
### Changes
1416
- Bump default `tabletest-formatter` version `1.0.1` -> `1.1.0`, now works with Java 17+.
1517

lib/src/tableTestFormatter/java/com/diffplug/spotless/glue/java/TableTestFormatterFunc.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
import org.tabletest.formatter.config.Config;
2121
import org.tabletest.formatter.config.EditorConfigProvider;
2222
import org.tabletest.formatter.core.SourceFileFormatter;
23+
import org.tabletest.formatter.core.TableTestFormatter;
2324

2425
import com.diffplug.spotless.FormatterFunc;
2526

2627
/**
27-
* Formats {@code @TableTest} annotation tables in Java and Kotlin source files.
28+
* Formats {@code @TableTest} annotation tables in Java, Kotlin, and standalone {@code .table} files.
2829
*/
2930
public class TableTestFormatterFunc implements FormatterFunc.NeedsFile {
3031

3132
private static final EditorConfigProvider CONFIG_PROVIDER = new EditorConfigProvider();
3233

3334
private final SourceFileFormatter sourceFormatter = new SourceFileFormatter();
35+
private final TableTestFormatter tableFormatter = new TableTestFormatter();
3436

3537
@Override
3638
public String applyWithFile(String unix, File file) throws Exception {
@@ -42,6 +44,11 @@ public String applyWithFile(String unix, File file) throws Exception {
4244
return formatted.equals(unix) ? unix : formatted;
4345
}
4446

47+
if (fileName.endsWith(".table")) {
48+
String formatted = tableFormatter.format(unix, "", Config.NO_INDENT);
49+
return formatted.equals(unix) ? unix : formatted;
50+
}
51+
4552
return unix;
4653
}
4754
}

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
- Add `tableTest` format type for standalone `.table` files.
68
### Changes
79
- Bump default `tabletest-formatter` version `1.0.1` -> `1.1.0`, now works with Java 17+.
810

plugin-gradle/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,34 @@ shfmt().pathToExe('/opt/homebrew/bin/shfmt')
12651265
12661266
<a name="applying-freshmark-to-markdown-files"></a>
12671267
1268+
## TableTest
1269+
1270+
- `com.diffplug.gradle.spotless.TableTestExtension` [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TableTestExtension.java)
1271+
1272+
```gradle
1273+
spotless {
1274+
tableTest {
1275+
target 'src/**/*.table' // you have to set the target manually
1276+
tableTestFormatter() // has its own section below
1277+
}
1278+
}
1279+
```
1280+
1281+
### tabletest-formatter
1282+
1283+
[homepage](https://github.com/nchaugen/tabletest-formatter). [changelog](https://github.com/nchaugen/tabletest-formatter/releases). Formats standalone [TableTest](https://github.com/nchaugen/tabletest) `.table` files.
1284+
1285+
```gradle
1286+
spotless {
1287+
tableTest {
1288+
target 'src/**/*.table'
1289+
tableTestFormatter()
1290+
// optional: you can specify a specific version
1291+
tableTestFormatter('1.1.0')
1292+
}
1293+
}
1294+
```
1295+
12681296
## Gherkin
12691297
12701298
- `com.diffplug.gradle.spotless.GherkinExtension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/8.3.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)

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
@@ -183,6 +183,12 @@ public void javascript(Action<JavascriptExtension> closure) {
183183
format(JavascriptExtension.NAME, JavascriptExtension.class, closure);
184184
}
185185

186+
/** Configures the special tableTest-specific extension for .table files. */
187+
public void tableTest(Action<TableTestExtension> closure) {
188+
requireNonNull(closure);
189+
format(TableTestExtension.NAME, TableTestExtension.class, closure);
190+
}
191+
186192
/** Configures the special typescript-specific extension for typescript files. */
187193
public void typescript(Action<TypescriptExtension> closure) {
188194
format(TypescriptExtension.NAME, TypescriptExtension.class, closure);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.diffplug.gradle.spotless;
2+
3+
import javax.inject.Inject;
4+
5+
import com.diffplug.spotless.FormatterStep;
6+
import com.diffplug.spotless.java.TableTestFormatterStep;
7+
8+
public class TableTestExtension extends FormatExtension {
9+
static final String NAME = "tableTest";
10+
11+
@Inject
12+
public TableTestExtension(SpotlessExtension spotless) {
13+
super(spotless);
14+
}
15+
16+
@Override
17+
protected void setupTask(SpotlessTask task) {
18+
if (target == null) {
19+
throw noDefaultTargetException();
20+
}
21+
super.setupTask(task);
22+
}
23+
24+
public TableTestFormatterConfig tableTestFormatter() {
25+
return tableTestFormatter(TableTestFormatterStep.defaultVersion());
26+
}
27+
28+
public TableTestFormatterConfig tableTestFormatter(String version) {
29+
return new TableTestFormatterConfig(version);
30+
}
31+
32+
public class TableTestFormatterConfig {
33+
private final String version;
34+
35+
TableTestFormatterConfig(String version) {
36+
this.version = version;
37+
addStep(createStep());
38+
}
39+
40+
private FormatterStep createStep() {
41+
return TableTestFormatterStep.create(version, provisioner());
42+
}
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.diffplug.gradle.spotless;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class TableTestExtensionTest extends GradleIntegrationHarness {
8+
@Test
9+
public void defaultFormatting() throws IOException {
10+
setFile("build.gradle").toLines(
11+
"plugins {",
12+
" id 'java'",
13+
" id 'com.diffplug.spotless'",
14+
"}",
15+
"repositories { mavenCentral() }",
16+
"spotless {",
17+
" tableTest {",
18+
" target 'src/**/*.table'",
19+
" tableTestFormatter()",
20+
" }",
21+
"}");
22+
setFile("src/main/resources/example.table").toResource("tableTest/tableFileUnformatted.test");
23+
setFile("other/example.table").toResource("tableTest/tableFileUnformatted.test");
24+
gradleRunner().withArguments("spotlessApply").build();
25+
assertFile("src/main/resources/example.table").sameAsResource("tableTest/tableFileFormatted.test");
26+
assertFile("other/example.table").sameAsResource("tableTest/tableFileUnformatted.test");
27+
}
28+
}

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
- Add `tableTest` format type for standalone `.table` files.
68
### Changes
79
- Bump default `tabletest-formatter` version `1.0.1` -> `1.1.0`, now works with Java 17+.
810

plugin-maven/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,25 @@ When formatting shell scripts via `shfmt`, configure `shfmt` settings via `.edit
11901190
</shfmt>
11911191
```
11921192

1193+
## TableTest
1194+
1195+
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/tabletest/TableTest.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/tabletest).
1196+
1197+
```xml
1198+
<configuration>
1199+
<tableTest>
1200+
<includes>
1201+
<include>src/**/*.table</include>
1202+
</includes>
1203+
<tableTestFormatter>
1204+
<version>1.1.0</version> <!-- optional -->
1205+
</tableTestFormatter>
1206+
</tableTest>
1207+
</configuration>
1208+
```
1209+
1210+
[homepage](https://github.com/nchaugen/tabletest-formatter). [changelog](https://github.com/nchaugen/tabletest-formatter/releases). Formats standalone [TableTest](https://github.com/nchaugen/tabletest) `.table` files.
1211+
11931212
## Gherkin
11941213

11951214
- `com.diffplug.spotless.maven.FormatterFactory.addStepFactory(FormatterStepFactory)` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/gherkin/Gherkin.java)

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.diffplug.spotless.maven.rdf.Rdf;
8282
import com.diffplug.spotless.maven.scala.Scala;
8383
import com.diffplug.spotless.maven.shell.Shell;
84+
import com.diffplug.spotless.maven.tabletest.TableTest;
8485
import com.diffplug.spotless.maven.sql.Sql;
8586
import com.diffplug.spotless.maven.typescript.Typescript;
8687
import com.diffplug.spotless.maven.yaml.Yaml;
@@ -208,6 +209,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
208209
@Parameter
209210
private Protobuf protobuf;
210211

212+
@Parameter
213+
private TableTest tableTest;
214+
211215
@Parameter(property = "spotlessFiles")
212216
private String filePatterns;
213217

@@ -410,7 +414,7 @@ private FileLocator getFileLocator() {
410414
}
411415

412416
private List<FormatterFactory> getFormatterFactories() {
413-
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, css, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go, rdf, protobuf))
417+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, css, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go, rdf, protobuf, tableTest))
414418
.filter(Objects::nonNull)
415419
.map(factory -> factory.init(repositorySystemSession))
416420
.collect(toList());

0 commit comments

Comments
 (0)