Skip to content

Commit

Permalink
Customize artifact of google java format (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Sep 27, 2021
2 parents 1a9d710 + c478183 commit 4e30602
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changed
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))

## [2.16.1] - 2021-09-20
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private GoogleJavaFormatStep() {}
private static final String DEFAULT_STYLE = "GOOGLE";
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter";
static final String FORMATTER_METHOD = "formatSource";

Expand Down Expand Up @@ -76,16 +76,29 @@ public static FormatterStep create(String version, String style, Provisioner pro

/** Creates a step which formats everything - code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
return create(MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
}
Objects.requireNonNull(version, "version");
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(NAME, version, style, provisioner, reflowLongStrings),
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
State::createFormat);
}

static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.7").add(11, "1.11.0");

public static String defaultGroupArtifact() {
return MAVEN_COORDINATE;
}

/** Get default formatter version */
public static String defaultVersion() {
return JVM_SUPPORT.getRecommendedFormatterVersion();
Expand Down Expand Up @@ -118,8 +131,12 @@ static final class State implements Serializable {
}

State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
}

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
this.stepName = stepName;
this.version = version;
this.style = style;
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changed
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))

## [5.15.1] - 2021-09-20
### Changed
Expand Down
7 changes: 4 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ spotless {
spotless {
java {
googleJavaFormat()
// optional: you can specify a specific version and/or switch to AOSP style and/or reflow long strings (requires at least 1.8)
//
googleJavaFormat('1.8').aosp().reflowLongStrings()
// optional: you can specify a specific version and/or switch to AOSP style
// and/or reflow long strings (requires at least 1.8)
// and/or use custom group artifact (you probably don't need this)
googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
```

### eclipse jdt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,23 @@ public GoogleJavaFormatConfig googleJavaFormat(String version) {

public class GoogleJavaFormatConfig {
final String version;
String groupArtifact;
String style;
boolean reflowLongStrings;

GoogleJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
this.groupArtifact = GoogleJavaFormatStep.defaultGroupArtifact();
this.style = GoogleJavaFormatStep.defaultStyle();
addStep(createStep());
}

public GoogleJavaFormatConfig groupArtifact(String groupArtifact) {
this.groupArtifact = Objects.requireNonNull(groupArtifact);
replaceStep(createStep());
return this;
}

public GoogleJavaFormatConfig style(String style) {
this.style = Objects.requireNonNull(style);
replaceStep(createStep());
Expand All @@ -119,7 +127,9 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
}

private FormatterStep createStep() {
return GoogleJavaFormatStep.create(version,
return GoogleJavaFormatStep.create(
groupArtifact,
version,
style,
provisioner(),
reflowLongStrings);
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changed
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))

## [2.13.1] - 2021-09-20
### Changed
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ any other maven phase (i.e. compile) then it can be configured as below;
<version>1.8</version> <!-- optional -->
<style>GOOGLE</style> <!-- or AOSP (optional) -->
<reflowLongStrings>true</reflowLongStrings> <!-- optional (requires at least 1.8) -->
<!-- optional: custom group artifact (you probably don't need this) -->
<groupArtifact>com.google.googlejavaformat:google-java-format</groupArtifact>
</googleJavaFormat>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.diffplug.spotless.maven.FormatterStepFactory;

public class GoogleJavaFormat implements FormatterStepFactory {
@Parameter
private String groupArtifact;

@Parameter
private String version;

Expand All @@ -34,9 +37,10 @@ public class GoogleJavaFormat implements FormatterStepFactory {

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
return GoogleJavaFormatStep.create(version, style, config.getProvisioner(), reflowLongStrings);
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ void behaviorWithReflowLongStrings() throws Exception {
}
}

@Test
void behaviorWithCustomGroupArtifact() throws Exception {
FormatterStep step = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), "1.2", GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), false);
StepHarness.forStep(step)
.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test", "java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test");
}

@Test
void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down Expand Up @@ -113,6 +123,30 @@ protected FormatterStep create() {
}.testEquals();
}

@Test
void equalityGroupArtifact() throws Exception {
new SerializableEqualityTester() {
String groupArtifact = GoogleJavaFormatStep.defaultGroupArtifact();
String version = "1.11.0";
String style = "";
boolean reflowLongStrings = false;

@Override
protected void setupTest(API api) {
// same version == same
api.areDifferentThan();
// change the groupArtifact, and it's different
groupArtifact = "io.opil:google-java-format";
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return GoogleJavaFormatStep.create(groupArtifact, version, style, TestProvisioner.mavenCentral(), reflowLongStrings);
}
}.testEquals();
}

@Test
void fixWindowsBugForGfj1Point1() {
fixWindowsBugTestcase("");
Expand Down

0 comments on commit 4e30602

Please sign in to comment.