From a396804f312eb7b3f8b68c3f0a8a226d2a96194d Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Wed, 16 Sep 2020 14:38:37 +0200 Subject: [PATCH] Add sql formatter to maven --- README.md | 4 +- plugin-maven/CHANGES.md | 2 + plugin-maven/README.md | 48 +++++++++++++++++++ .../spotless/maven/AbstractSpotlessMojo.java | 6 ++- .../diffplug/spotless/maven/sql/DBeaver.java | 37 ++++++++++++++ .../com/diffplug/spotless/maven/sql/Sql.java | 44 +++++++++++++++++ .../maven/MavenIntegrationHarness.java | 4 ++ .../diffplug/spotless/maven/sql/SqlTest.java | 41 ++++++++++++++++ 8 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/DBeaver.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/sql/SqlTest.java diff --git a/README.md b/README.md index 42d5bdcf32..01ca33da62 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ lib('npm.PrettierFormatterStep') +'{{yes}} | {{yes}} lib('npm.TsFmtFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('python.BlackStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |', lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', -lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{yes}} | {{no}} |', +lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', '| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | {{no}} | {{no}} | {{no}} | {{no}} |', ].join('\n'); @@ -100,7 +100,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`python.BlackStep`](lib/src/main/java/com/diffplug/spotless/python/BlackStep.java) | :+1: | :white_large_square: | :white_large_square: | :white_large_square: | | [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :+1: | :white_large_square: | -| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :+1: | :white_large_square: | +| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`wtp.EclipseWtpFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | :white_large_square: | :white_large_square: | :white_large_square: | :white_large_square: | diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 898fe77c96..c80d56cf5c 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for sql formatting ([#698](https://github.com/diffplug/spotless/pull/698)) ## [2.3.1] - 2020-09-12 ### Fixed diff --git a/plugin-maven/README.md b/plugin-maven/README.md index eaca22e72b..ca116e2ac3 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -51,6 +51,7 @@ user@machine repo % mvn spotless:check - [Scala](#scala) ([scalafmt](#scalafmt)) - [C/C++](#cc) ([eclipse cdt](#eclipse-cdt)) - [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter)) + - [Sql](#sql) ([dbeaver](#dbeaver)) - [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier)) - Multiple languages - [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection)) @@ -341,6 +342,53 @@ Spotless requires Maven to be running on JRE 8+. ``` + + + +## SQL + +[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). + +```xml + + + + + src/main/resources/**/*.sql + + + + + + -- (C)$YEAR + + + +``` + +### dbeaver + +[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. + +```xml + + dbeaver.props + +``` + +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). + +```properties +# case of the keywords (UPPER, LOWER or ORIGINAL) +sql.formatter.keyword.case=UPPER +# Statement delimiter +sql.formatter.statement.delimiter=; +# Indentation style (space or tab) +sql.formatter.indent.type=space +# Number of identation characters +sql.formatter.indent.size=4 +``` + ## Typescript diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index caacf51e47..dcfca6fb98 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -54,6 +54,7 @@ import com.diffplug.spotless.maven.java.Java; import com.diffplug.spotless.maven.kotlin.Kotlin; import com.diffplug.spotless.maven.scala.Scala; +import com.diffplug.spotless.maven.sql.Sql; import com.diffplug.spotless.maven.typescript.Typescript; public abstract class AbstractSpotlessMojo extends AbstractMojo { @@ -112,6 +113,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Antlr4 antlr4; + @Parameter + private Sql sql; + @Parameter(property = "spotlessFiles") private String filePatterns; @@ -201,7 +205,7 @@ private FileLocator getFileLocator() { } private List getFormatterFactories() { - return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, antlr4)) + return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, antlr4, sql)) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/DBeaver.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/DBeaver.java new file mode 100644 index 0000000000..d3426dea78 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/DBeaver.java @@ -0,0 +1,37 @@ +/* + * Copyright 2020 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.spotless.maven.sql; + +import java.util.Collections; +import java.util.Optional; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; +import com.diffplug.spotless.sql.DBeaverSQLFormatterStep; + +public class DBeaver implements FormatterStepFactory { + + @Parameter + private String configFile; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + return DBeaverSQLFormatterStep.create(Optional.ofNullable(configFile).map(c -> stepConfig.getFileLocator().locateFile(c)).map(Collections::singleton).orElseGet(Collections::emptySet)); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java new file mode 100644 index 0000000000..b1c579f79b --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/sql/Sql.java @@ -0,0 +1,44 @@ +/* + * Copyright 2020 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.spotless.maven.sql; + +import java.util.Collections; +import java.util.Set; + +import com.diffplug.spotless.maven.FormatterFactory; + +/** + * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element. + *

+ * It defines a formatter for sql source files. + */ +public class Sql extends FormatterFactory { + private static final String LICENSE_HEADER_DELIMITER = null; + + @Override + public Set defaultIncludes() { + return Collections.emptySet(); + } + + @Override + public String licenseHeaderDelimiter() { + return LICENSE_HEADER_DELIMITER; + } + + public void addDbeaver(DBeaver dbeaver) { + addStepFactory(dbeaver); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index 7934cb3d69..50bd2cd08f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -122,6 +122,10 @@ protected void writePomWithTypescriptSteps(String... steps) throws IOException { writePom(groupWithSteps("typescript", including("**/*.ts"), steps)); } + protected void writePomWithSqlSteps(String... steps) throws IOException { + writePom(groupWithSteps("sql", including("**/*.sql"), steps)); + } + protected void writePomWithPrettierSteps(String includes, String... steps) throws IOException { writePom(formats(groupWithSteps("format", including(includes), steps))); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/sql/SqlTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/sql/SqlTest.java new file mode 100644 index 0000000000..a6fd8719c3 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/sql/SqlTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016-2020 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.spotless.maven.sql; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationHarness; + +public class SqlTest extends MavenIntegrationHarness { + @Test + public void testDbeaverWithDefaultConfig() throws Exception { + writePomWithSqlSteps(""); + + setFile("src/main/resources/aFolder/create.sql").toResource("sql/dbeaver/create.dirty"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("src/main/resources/aFolder/create.sql").sameAsResource("sql/dbeaver/create.clean"); + } + + @Test + public void testDbeaverWithAlternativeConfig() throws Exception { + writePomWithSqlSteps("myConfig.properties"); + setFile("myConfig.properties").toResource("sql/dbeaver/sqlConfig2.properties"); + + setFile("src/main/resources/aFolder/create.sql").toResource("sql/dbeaver/create.dirty"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("src/main/resources/aFolder/create.sql").sameAsResource("sql/dbeaver/create.clean.alternative"); + } +}