Skip to content

Commit

Permalink
Add python support to maven plugin (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed May 3, 2021
2 parents c09f3cb + 76fcd54 commit b0893c7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [python](README.md#python), specifically [black](README.md#black).
### Changed
* Update ktfmt from 0.21 to 0.24
### Fixed
Expand Down
37 changes: 37 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ user@machine repo % mvn spotless:check
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
- [Scala](#scala) ([scalafmt](#scalafmt))
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
- [Python](#python) ([black](#black))
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
- [Sql](#sql) ([dbeaver](#dbeaver))
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
Expand Down Expand Up @@ -397,6 +398,42 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
</eclipseCdt>
```

## Python

[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Python.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Black.java).

```xml
<configuration>
<python>
<!-- You have to set the target manually -->
<includes>
<include>src/main/**/*.py</include>
</includes>

<black /> <!-- has its own section below -->
</python>
</configuration>
```

### black

[homepage](https://github.com/psf/black). [changelog](https://github.com/psf/black/blob/master/CHANGES.md).

```xml
<black>
<version>19.10b0</version> <!-- optional -->
<!-- if black is not on your path, you must specify its location manually -->
<pathToExe>C:/myuser/.pyenv/versions/3.8.0/scripts/black.exe</pathToExe>
<!--
Spotless always checks the version of the black it is using
and will fail with an error if it does not match the expected version
(whether manually specified or default). If there is a problem, Spotless
will suggest commands to help install the correct version.
TODO: handle installation & packaging automatically - https://github.com/diffplug/spotless/issues/674
-->
</black>
```

## Antlr4

[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/antlr4/Antlr4.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/antlr4).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,7 @@
import com.diffplug.spotless.maven.groovy.Groovy;
import com.diffplug.spotless.maven.java.Java;
import com.diffplug.spotless.maven.kotlin.Kotlin;
import com.diffplug.spotless.maven.python.Python;
import com.diffplug.spotless.maven.scala.Scala;
import com.diffplug.spotless.maven.sql.Sql;
import com.diffplug.spotless.maven.typescript.Typescript;
Expand Down Expand Up @@ -122,6 +123,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Sql sql;

@Parameter
private Python python;

@Parameter(property = "spotlessFiles")
private String filePatterns;

Expand Down Expand Up @@ -254,7 +258,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, sql))
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, sql, python))
.filter(Objects::nonNull)
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 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.python;

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.python.BlackStep;

public class Black implements FormatterStepFactory {

@Parameter
private String version;

@Parameter
private String pathToExe;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
BlackStep black = BlackStep.withVersion(version == null ? BlackStep.defaultVersion() : version);
if (pathToExe != null) {
black = black.withPathToExe(pathToExe);
}
return black.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2021 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.python;

import java.util.Collections;
import java.util.Set;

import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

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

@Override
public Set<String> defaultIncludes() {
return Collections.emptySet();
}

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

public void addBlack(Black black) {
addStepFactory(black);
}

}

0 comments on commit b0893c7

Please sign in to comment.