Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin support in Maven plugin #223

Merged
merged 9 commits into from
Mar 13, 2018
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}}
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{no}} |',
extra('java.EclipseFormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('kotlin.KtLintStep') +'{{yes}} | {{no}} | {{no}} |',
lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |',
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
Expand All @@ -66,7 +66,7 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: |
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :white_large_square: |
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :+1: | :white_large_square: |
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :white_large_square: |
| [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: |
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2016 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.kotlin;

public final class KotlinConstants {

// '^' is prepended to the regex in LICENSE_HEADER_DELIMITER later in FormatExtension.licenseHeader(String, String)
public static final String LICENSE_HEADER_DELIMITER = "(package |@file)";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


private KotlinConstants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.diffplug.gradle.spotless;

import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;

import java.util.Objects;

import org.gradle.api.GradleException;
Expand All @@ -25,8 +27,6 @@
import com.diffplug.spotless.kotlin.KtLintStep;

public class KotlinExtension extends FormatExtension {
// '^' is prepended to the regex in LICENSE_HEADER_DELIMITER later in FormatExtension.licenseHeader(String, String)
private static final String LICENSE_HEADER_DELIMITER = "(package |@file)";
static final String NAME = "kotlin";

public KotlinExtension(SpotlessExtension rootExtension) {
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Version 1.10.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))

* Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222))
* Added support for Kotlin and Ktlint in Maven plugin ([#223](https://github.com/diffplug/spotless/pull/223)).

### Version 1.0.0.BETA4 - February 27th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.0.0.BETA4/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.0.0.BETA4))

Expand Down
49 changes: 49 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,55 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/*
</configuration>
```

<a name="scala"></a>

## Applying to Scala source

By default, all files matching `src/main/scala/**/*.scala`, `src/test/scala/**/*.scala`, `src/main/scala/**/*.sc` and `src/test/scala/**/*.sc` Ant style pattern will be formatted. Each element under `<scala>` is a step, and they will be applied in the order specified. Every step is optional.

```xml
<configuration>
<scala>
<licenseHeader>
<!-- Specify either content or file, but not both -->
<content>/* Licensed under Apache-2.0 */</content>
<file>${basedir}/license-header</file>
</licenseHeader>
<endWithNewLine/>
<trimTrailingWhitespace/>
<scalafmt>
<file>${basedir}/scalafmt.conf</file>
<!-- Optional, available versions: https://github.com/scalameta/scalafmt/releases -->
<version>1.1.0</version>
</scalafmt>
</scala>
</configuration>
```

<a name="kotlin"></a>

## Applying to Kotlin source

By default, all files matching `src/main/kotlin/**/*.kt` and `src/test/kotlin/**/*.kt` Ant style pattern will be formatted. Each element under `<kotlin>` is a step, and they will be applied in the order specified. Every step is optional.

```xml
<configuration>
<kotlin>
<licenseHeader>
<!-- Specify either content or file, but not both -->
<content>/* Licensed under Apache-2.0 */</content>
<file>${basedir}/license-header</file>
</licenseHeader>
<endWithNewLine/>
<trimTrailingWhitespace/>
<ktlint>
<!-- Optional, available versions: https://github.com/shyiko/ktlint/releases -->
<version>0.14.0</version>
</ktlint>
</kotlin>
</configuration>
```

<a name="format"></a>

## Applying to custom sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.diffplug.spotless.maven.generic.Format;
import com.diffplug.spotless.maven.generic.LicenseHeader;
import com.diffplug.spotless.maven.java.Java;
import com.diffplug.spotless.maven.kotlin.Kotlin;
import com.diffplug.spotless.maven.scala.Scala;

public abstract class AbstractSpotlessMojo extends AbstractMojo {
Expand Down Expand Up @@ -85,6 +86,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Scala scala;

@Parameter
private Kotlin kotlin;

protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;

@Override
Expand Down Expand Up @@ -142,7 +146,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
return Stream.of(format, java, scala)
return Stream.of(format, java, scala, kotlin)
.filter(Objects::nonNull)
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.maven.generic.LicenseHeader;
import com.diffplug.spotless.maven.generic.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit that I was surprised that this change from a full import to a wildcard import was apparently accepted by Travis CI.

@nedtwigg Do you think it would be a good idea if I open an issue to cover catching wildcard imports in the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eclipse formatter that we use doesn't have any rules against wildcard imports. It might be worth a bullet-point in this issue, maybe a config option on importSorter if it ever gets updated: #167

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nedtwigg I'm not sure that I understood your last message correctly.

Did you mean that you'd like me to write a new message on #167 - a reminder of sorts - to add a convert-wildcard-imports-to-full-imports option to the new importOrder() formatting option that would be implemented? Or did you mean something else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! You can make a new issue too if you'd like, but it seems like implementing the wildcard imports issue is so closely tied to the ImportSorter that it makes sense to bundle them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I'll go act on that now, then. :)


public abstract class FormatterFactory {
@Parameter
Expand Down Expand Up @@ -87,7 +87,27 @@ public final void addLicenseHeader(LicenseHeader licenseHeader) {
addStepFactory(licenseHeader);
}

protected void addStepFactory(FormatterStepFactory stepFactory) {
public final void addEndWithNewline(EndWithNewline endWithNewline) {
addStepFactory(endWithNewline);
}

public final void addIndent(Indent indent) {
addStepFactory(indent);
}

public final void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
addStepFactory(trimTrailingWhitespace);
}

public final void addReplace(Replace replace) {
addStepFactory(replace);
}

public final void addReplaceRegex(ReplaceRegex replaceRegex) {
addStepFactory(replaceRegex);
}

protected final void addStepFactory(FormatterStepFactory stepFactory) {
Objects.requireNonNull(stepFactory);
stepFactories.add(stepFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

import com.diffplug.spotless.maven.FormatterFactory;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <format>...</format>} configuration element.
* <p>
* It defines a formatter for custom includes/excludes that executes list of generic, language agnostic steps,
* like {@link LicenseHeader}.
*/
public class Format extends FormatterFactory {

@Override
Expand All @@ -32,24 +38,4 @@ public String licenseHeaderDelimiter() {
// do not specify a default delimiter
return null;
}

public void addEndWithNewline(EndWithNewline endWithNewline) {
addStepFactory(endWithNewline);
}

public void addIndent(Indent indent) {
addStepFactory(indent);
}

public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
addStepFactory(trimTrailingWhitespace);
}

public void addReplace(Replace replace) {
addStepFactory(replace);
}

public void addReplaceRegex(ReplaceRegex replaceRegex) {
addStepFactory(replaceRegex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
*/
package com.diffplug.spotless.maven.java;

import static com.diffplug.common.collect.Sets.newHashSet;
import static java.util.Collections.unmodifiableSet;

import java.util.Set;

import com.diffplug.spotless.maven.generic.Format;
import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <java>...</java>} configuration element.
* <p>
* It defines a formatter for java source files that can execute both language agnostic (e.g. {@link LicenseHeader})
* and java-specific (e.g. {@link Eclipse}) steps.
*/
public class Java extends FormatterFactory {

public class Java extends Format {
private static final Set<String> DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java",
"src/test/java/**/*.java"));
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java");
private static final String LICENSE_HEADER_DELIMITER = "package ";

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016 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.kotlin;

import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;

import java.util.Set;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;

public class Kotlin extends FormatterFactory {

private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/kotlin/**/*.kt", "src/test/kotlin/**/*.kt");

@Override
public Set<String> defaultIncludes() {
return DEFAULT_INCLUDES;
}

@Override
public String licenseHeaderDelimiter() {
return LICENSE_HEADER_DELIMITER;
}

public void addKtlint(Ktlint ktlint) {
addStepFactory(ktlint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016 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.kotlin;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.kotlin.KtLintStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Ktlint implements FormatterStepFactory {

@Parameter
private String version;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String ktlintVersion = version != null ? version : KtLintStep.defaultVersion();
return KtLintStep.create(ktlintVersion, config.getProvisioner());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
*/
package com.diffplug.spotless.maven.scala;

import static com.diffplug.common.collect.Sets.newHashSet;
import static java.util.Collections.unmodifiableSet;

import java.util.Set;

import com.diffplug.spotless.maven.generic.Format;
import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <scala>...</scala>} configuration element.
* <p>
* It defines a formatter for scala source files that can execute both language agnostic (e.g. {@link LicenseHeader})
* and scala-specific (e.g. {@link Scalafmt}) steps.
*/
public class Scala extends FormatterFactory {

public class Scala extends Format {
private static final Set<String> DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala",
"src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc"));
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/scala/**/*.scala",
"src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc");
private static final String LICENSE_HEADER_DELIMITER = "package ";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ protected void writePomWithScalaSteps(String... steps) throws IOException {
writePom(groupWithSteps("scala", steps));
}

protected void writePomWithKotlinSteps(String... steps) throws IOException {
writePom(groupWithSteps("kotlin", steps));
}

protected void writePom(String... configuration) throws IOException {
writePom(null, configuration);
}
Expand Down
Loading