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

Add support for gofmt #2001

Merged
merged 13 commits into from
Jan 22, 2024
Prev Previous commit
Next Next commit
Implement gofmt support in plugin-maven
  • Loading branch information
petertrr committed Jan 16, 2024

Verified

This commit was signed with the committer’s verified signature.
gsmet Guillaume Smet
commit e868f630283b19a7166f7c91d227349600b90541
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
* Support for `gofmt` ([#2001](https://github.com/diffplug/spotless/issues/2001))

## [2.42.0] - 2024-01-15
### Added
28 changes: 28 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ user@machine repo % mvn spotless:check
- [JSON](#json) ([simple](#simple), [gson](#gson), [jackson](#jackson), [Biome](#biome), [jsonPatch](#jsonPatch))
- [YAML](#yaml)
- [Gherkin](#gherkin)
- [Go](#go)
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -1047,6 +1048,33 @@ Uses a Gherkin pretty-printer that optionally allows configuring the number of s
</gherkinUtils>
```

## Go

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

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

<gofmt /> <!-- has its own section below -->
</go>
</configuration>
```

### gofmt

Standard Go formatter, part of Go distribution.

```xml
<gofmt>
<version>go1.25.1</version>
<goExecutablePath>/opt/sdks/go1.25.1/bin/go</goExecutablePath>
</gofmt>
```

## Prettier

[homepage](https://prettier.io/). [changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md). [official plugins](https://prettier.io/docs/en/plugins.html#official-plugins). [community plugins](https://prettier.io/docs/en/plugins.html#community-plugins). Prettier is a formatter that can format almost every anything - JavaScript, JSX, Angular, Vue, Flow, TypeScript, CSS, Less, SCSS, HTML, JSON, GraphQL, Markdown (including GFM and MDX), and YAML. It can format even more [using plugins](https://prettier.io/docs/en/plugins.html) (PHP, Ruby, Swift, XML, Apex, Elm, Java (!!), Kotlin, pgSQL, .properties, solidity, svelte, toml, shellscript, ...).
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.diffplug.spotless.maven.go.Go;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
@@ -184,6 +185,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Gherkin gherkin;

@Parameter
private Go go;

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

@@ -358,7 +362,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, javascript, antlr4, pom, sql, python, markdown, json, yaml, gherkin))
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, javascript, antlr4, pom, sql, python, markdown, json, yaml, gherkin, go))
.filter(Objects::nonNull)
.collect(toList());
}
23 changes: 23 additions & 0 deletions plugin-maven/src/main/java/com/diffplug/spotless/maven/go/Go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.diffplug.spotless.maven.go;

import com.diffplug.spotless.maven.FormatterFactory;
import org.apache.maven.project.MavenProject;

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

public class Go extends FormatterFactory {
@Override
public Set<String> defaultIncludes(MavenProject project) {
return Collections.emptySet();
}

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

public void addGofmt(Gofmt gofmt) {
addStepFactory(gofmt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.diffplug.spotless.maven.go;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.go.GofmtFormatStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;
import org.apache.maven.plugins.annotations.Parameter;

public class Gofmt implements FormatterStepFactory {

@Parameter
private String version;

@Parameter
private String goExecutablePath;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
GofmtFormatStep step = GofmtFormatStep.withVersion(version == null ? GofmtFormatStep.defaultVersion() : version);
if (goExecutablePath != null) {
step = step.withGoExecutable(goExecutablePath);
}
return step.create();
}
}
Original file line number Diff line number Diff line change
@@ -187,6 +187,10 @@ protected void writePomWithGherkinSteps(String... steps) throws IOException {
writePom(groupWithSteps("gherkin", including("**/*.feature"), steps));
}

protected void writePomWithGoSteps(String... steps) throws IOException {
writePom(groupWithSteps("go", including("**/*.go"), steps));
}

protected void writePom(String... configuration) throws IOException {
writePom(null, configuration, null, null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.diffplug.spotless.maven.go;

import com.diffplug.spotless.maven.MavenIntegrationHarness;
import org.junit.jupiter.api.Test;

@com.diffplug.spotless.tag.GofmtTest
public class GofmtTest extends MavenIntegrationHarness {
@Test
void testGofmt() throws Exception {
writePomWithGoSteps("<gofmt><version>go1.21.5</version></gofmt>");

setFile("src/main/go/example.go").toResource("go/gofmt/go.dirty");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("src/main/go/example.go").sameAsResource("go/gofmt/go.clean");
}
}