Skip to content

Commit a17fa25

Browse files
authored
Add sql formatter to maven (#698)
2 parents 6a1f09d + e9a5448 commit a17fa25

File tree

9 files changed

+177
-6
lines changed

9 files changed

+177
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ lib('npm.PrettierFormatterStep') +'{{yes}} | {{yes}}
6565
lib('npm.TsFmtFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6666
lib('python.BlackStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
6767
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
68-
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{yes}} | {{no}} |',
68+
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
6969
extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
7070
'| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | {{no}} | {{no}} | {{no}} | {{no}} |',
7171
].join('\n');
@@ -100,7 +100,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
100100
| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
101101
| [`python.BlackStep`](lib/src/main/java/com/diffplug/spotless/python/BlackStep.java) | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
102102
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
103-
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :+1: | :white_large_square: |
103+
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
104104
| [`wtp.EclipseWtpFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
105105
| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | :white_large_square: | :white_large_square: | :white_large_square: | :white_large_square: |
106106
<!---freshmark /matrix -->

plugin-maven/CHANGES.md

+2
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+
* Added support for sql formatting ([#698](https://github.com/diffplug/spotless/pull/698))
68

79
## [2.3.1] - 2020-09-12
810
### Fixed

plugin-maven/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ user@machine repo % mvn spotless:check
5252
- [Scala](#scala) ([scalafmt](#scalafmt))
5353
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
5454
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
55+
- [Sql](#sql) ([dbeaver](#dbeaver))
5556
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
5657
- Multiple languages
5758
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection))
@@ -386,6 +387,48 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
386387
</antlr4formatter>
387388
```
388389

390+
## SQL
391+
392+
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql).
393+
394+
```xml
395+
<configuration>
396+
<sql>
397+
<!-- You have to set the target manually -->
398+
<includes>
399+
<include>src/main/resources/**/*.sql</include>
400+
</includes>
401+
402+
<dbeaver /> <!-- has its own section below -->
403+
404+
<prettier /> <!-- has its own section below -->
405+
</sql>
406+
</configuration>
407+
```
408+
409+
### dbeaver
410+
411+
[homepage](https://dbeaver.io/). DBeaver is only distributed as a monolithic jar, so the formatter used here was copy-pasted into Spotless, and thus there is no version to change.
412+
413+
```xml
414+
<dbveaer>
415+
<configFile>dbeaver.props</configFile> <!-- configFile is optional -->
416+
</dbveaer>
417+
```
418+
419+
Default configuration file, other options [available here](https://github.com/diffplug/spotless/blob/main/lib/src/main/java/com/diffplug/spotless/sql/dbeaver/DBeaverSQLFormatterConfiguration.java).
420+
421+
```properties
422+
# case of the keywords (UPPER, LOWER or ORIGINAL)
423+
sql.formatter.keyword.case=UPPER
424+
# Statement delimiter
425+
sql.formatter.statement.delimiter=;
426+
# Indentation style (space or tab)
427+
sql.formatter.indent.type=space
428+
# Number of identation characters
429+
sql.formatter.indent.size=4
430+
```
431+
389432
<a name="applying-to-typescript-source"></a>
390433

391434
## Typescript

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.diffplug.spotless.maven.java.Java;
5656
import com.diffplug.spotless.maven.kotlin.Kotlin;
5757
import com.diffplug.spotless.maven.scala.Scala;
58+
import com.diffplug.spotless.maven.sql.Sql;
5859
import com.diffplug.spotless.maven.typescript.Typescript;
5960

6061
public abstract class AbstractSpotlessMojo extends AbstractMojo {
@@ -116,6 +117,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
116117
@Parameter
117118
private Antlr4 antlr4;
118119

120+
@Parameter
121+
private Sql sql;
122+
119123
@Parameter(property = "spotlessFiles")
120124
private String filePatterns;
121125

@@ -205,7 +209,7 @@ private FileLocator getFileLocator() {
205209
}
206210

207211
private List<FormatterFactory> getFormatterFactories() {
208-
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4))
212+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, sql))
209213
.filter(Objects::nonNull)
210214
.collect(toList());
211215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 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.spotless.maven.sql;
17+
18+
import java.util.Collections;
19+
import java.util.Optional;
20+
21+
import org.apache.maven.plugins.annotations.Parameter;
22+
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.maven.FormatterStepConfig;
25+
import com.diffplug.spotless.maven.FormatterStepFactory;
26+
import com.diffplug.spotless.sql.DBeaverSQLFormatterStep;
27+
28+
public class DBeaver implements FormatterStepFactory {
29+
30+
@Parameter
31+
private String configFile;
32+
33+
@Override
34+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
35+
return DBeaverSQLFormatterStep.create(Optional.ofNullable(configFile).map(c -> stepConfig.getFileLocator().locateFile(c)).map(Collections::singleton).orElseGet(Collections::emptySet));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 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.spotless.maven.sql;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <dbeaver>...</dbeaver>} configuration element.
25+
* <p>
26+
* It defines a formatter for sql source files.
27+
*/
28+
public class Sql extends FormatterFactory {
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addDbeaver(DBeaver dbeaver) {
40+
addStepFactory(dbeaver);
41+
}
42+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@
2626
* It defines a formatter for typescript source files.
2727
*/
2828
public class Typescript extends FormatterFactory {
29-
private static final String LICENSE_HEADER_DELIMITER = null;
30-
3129
@Override
3230
public Set<String> defaultIncludes() {
3331
return Collections.emptySet();
3432
}
3533

3634
@Override
3735
public String licenseHeaderDelimiter() {
38-
return LICENSE_HEADER_DELIMITER;
36+
return null;
3937
}
4038

4139
public void addTsfmt(Tsfmt tsfmt) {

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ protected void writePomWithTypescriptSteps(String... steps) throws IOException {
126126
writePom(groupWithSteps("typescript", including("**/*.ts"), steps));
127127
}
128128

129+
protected void writePomWithSqlSteps(String... steps) throws IOException {
130+
writePom(groupWithSteps("sql", including("**/*.sql"), steps));
131+
}
132+
129133
protected void writePomWithPrettierSteps(String includes, String... steps) throws IOException {
130134
writePom(formats(groupWithSteps("format", including(includes), steps)));
131135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016-2020 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.spotless.maven.sql;
17+
18+
import org.junit.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
public class SqlTest extends MavenIntegrationHarness {
23+
@Test
24+
public void testDbeaverWithDefaultConfig() throws Exception {
25+
writePomWithSqlSteps("<dbeaver/>");
26+
27+
setFile("src/main/resources/aFolder/create.sql").toResource("sql/dbeaver/create.dirty");
28+
mavenRunner().withArguments("spotless:apply").runNoError();
29+
assertFile("src/main/resources/aFolder/create.sql").sameAsResource("sql/dbeaver/create.clean");
30+
}
31+
32+
@Test
33+
public void testDbeaverWithAlternativeConfig() throws Exception {
34+
writePomWithSqlSteps("<dbeaver><configFile>myConfig.properties</configFile></dbeaver>");
35+
setFile("myConfig.properties").toResource("sql/dbeaver/sqlConfig2.properties");
36+
37+
setFile("src/main/resources/aFolder/create.sql").toResource("sql/dbeaver/create.dirty");
38+
mavenRunner().withArguments("spotless:apply").runNoError();
39+
assertFile("src/main/resources/aFolder/create.sql").sameAsResource("sql/dbeaver/create.clean.alternative");
40+
}
41+
}

0 commit comments

Comments
 (0)