From c478183b2c9f5c2188259810705a3f16466bb3af Mon Sep 17 00:00:00 2001 From: P Chen Date: Wed, 22 Sep 2021 10:14:50 +0800 Subject: [PATCH] feat: customize artifact for google java format update change log add new ctor of State instead of changing existed ones change artifact to groupArtifact check groupArtifact content add tests for custom group artifact --- CHANGES.md | 1 + .../spotless/java/GoogleJavaFormatStep.java | 23 +++++++++++-- plugin-gradle/CHANGES.md | 1 + plugin-gradle/README.md | 7 ++-- .../gradle/spotless/JavaExtension.java | 12 ++++++- plugin-maven/CHANGES.md | 1 + plugin-maven/README.md | 2 ++ .../spotless/maven/java/GoogleJavaFormat.java | 6 +++- .../java/GoogleJavaFormatStepTest.java | 34 +++++++++++++++++++ 9 files changed, 79 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 242186767f..f4445118a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java index 0690ef56f0..4430256f3d 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -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"; @@ -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 JVM_SUPPORT = Jvm. 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(); @@ -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; diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 146dbfa770..3b50028da9 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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 diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index ca29c6a94b..633fe52551 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -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 diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 92e88a234b..2965895da5 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -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()); @@ -119,7 +127,9 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) { } private FormatterStep createStep() { - return GoogleJavaFormatStep.create(version, + return GoogleJavaFormatStep.create( + groupArtifact, + version, style, provisioner(), reflowLongStrings); diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index d78afebb58..a1e738ddfd 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -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 diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 113877c23d..caa22d1d3d 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -205,6 +205,8 @@ any other maven phase (i.e. compile) then it can be configured as below; 1.8 true + + com.google.googlejavaformat:google-java-format ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java index 95c31ea6ca..3597e84e25 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java @@ -23,6 +23,9 @@ import com.diffplug.spotless.maven.FormatterStepFactory; public class GoogleJavaFormat implements FormatterStepFactory { + @Parameter + private String groupArtifact; + @Parameter private String version; @@ -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); } } diff --git a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java index 38a158b5d9..40f56b74c2 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -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() { @@ -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("");