From 595cecdb97437e3b7440ac1ec9cab2c7567c1c12 Mon Sep 17 00:00:00 2001 From: Tyler Bertrand Date: Tue, 1 Aug 2023 19:54:44 -0500 Subject: [PATCH 01/11] Utilize providers for line endings This way the external git processes are launched at execution time and not during configuration --- .../com/diffplug/gradle/spotless/FormatExtension.java | 4 +++- .../java/com/diffplug/gradle/spotless/SpotlessTask.java | 9 +++++---- .../gradle/spotless/DiffMessageFormatterTest.java | 4 ++-- .../com/diffplug/gradle/spotless/FormatTaskTest.java | 2 +- .../com/diffplug/gradle/spotless/PaddedCellTaskTest.java | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index e28ec13dcd..7a62f8cabf 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -40,6 +40,7 @@ import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.file.ConfigurableFileTree; +import org.gradle.api.file.Directory; import org.gradle.api.file.FileCollection; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.tasks.TaskProvider; @@ -925,7 +926,8 @@ protected void setupTask(SpotlessTask task) { steps.replaceAll(formatterStep -> formatterStep.filterByContent(OnMatch.EXCLUDE, targetExcludeContentPattern)); } task.setSteps(steps); - task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> totalTarget)); + Directory projectDir = getProject().getLayout().getProjectDirectory(); + task.setLineEndingsPolicy(getProject().provider(() -> getLineEndings().createPolicy(projectDir.getAsFile(), () -> totalTarget))); spotless.getRegisterDependenciesTask().hookSubprojectTask(task); task.setupRatchet(getRatchetFrom() != null ? getRatchetFrom() : ""); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java index c23cea0845..2af9a80b7a 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java @@ -28,6 +28,7 @@ import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileCollection; import org.gradle.api.provider.Property; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.Internal; @@ -64,14 +65,14 @@ public void setEncoding(String encoding) { this.encoding = Objects.requireNonNull(encoding); } - protected final LiveCache lineEndingsPolicy = createLive("lineEndingsPolicy"); + protected final LiveCache> lineEndingsPolicy = createLive("lineEndingsPolicy"); @Input - public LineEnding.Policy getLineEndingsPolicy() { + public Provider getLineEndingsPolicy() { return lineEndingsPolicy.get(); } - public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) { + public void setLineEndingsPolicy(Provider lineEndingsPolicy) { this.lineEndingsPolicy.set(lineEndingsPolicy); } @@ -185,7 +186,7 @@ String formatName() { Formatter buildFormatter() { return Formatter.builder() .name(formatName()) - .lineEndingsPolicy(lineEndingsPolicy.get()) + .lineEndingsPolicy(lineEndingsPolicy.get().get()) .encoding(Charset.forName(encoding)) .rootDir(getProjectDir().get().getAsFile().toPath()) .steps(steps.get()) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java index ef72043a47..c564664f10 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java @@ -62,7 +62,7 @@ public BuildServiceParameters.None getParameters() { private SpotlessTaskImpl createFormatTask(String name) { SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class); task.init(taskService); - task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy()); + task.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy)); task.setTarget(Collections.singletonList(file)); return task; } @@ -100,7 +100,7 @@ private Bundle create(File... files) throws IOException { private Bundle create(List files) throws IOException { Bundle bundle = new Bundle("underTest"); - bundle.task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy()); + bundle.task.setLineEndingsPolicy(bundle.project.provider(LineEnding.UNIX::createPolicy)); bundle.task.setTarget(files); return bundle; } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java index 8985cd7a14..c4c055c799 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java @@ -35,7 +35,7 @@ class FormatTaskTest extends ResourceHarness { void createTask() { Project project = TestProvisioner.gradleProject(rootFolder()); spotlessTask = project.getTasks().create("spotlessTaskUnderTest", SpotlessTaskImpl.class); - spotlessTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy()); + spotlessTask.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy)); spotlessTask.init(GradleIntegrationHarness.providerOf(new SpotlessTaskService() { @Override public BuildServiceParameters.None getParameters() { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java index bc7a2fdc96..db55077d00 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java @@ -66,7 +66,7 @@ private SpotlessTaskImpl createFormatTask(String name, FormatterStep step) { SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class); task.init(taskService); task.addStep(step); - task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy()); + task.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy)); task.setTarget(Collections.singletonList(file)); return task; } From 2c33cd319b73f1aa5b86cef2414ebd6656f8f6f1 Mon Sep 17 00:00:00 2001 From: Tyler Bertrand Date: Wed, 2 Aug 2023 15:29:22 -0500 Subject: [PATCH 02/11] Update plugin-gradle changelog --- plugin-gradle/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index b21debb36f..5ffc7912ba 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -8,6 +8,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) +* Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644)) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760)) From 35be272b300d9a3a4a939b706728050cbf1b9c69 Mon Sep 17 00:00:00 2001 From: Tyler Bertrand Date: Thu, 3 Aug 2023 20:17:56 -0500 Subject: [PATCH 03/11] spotlessApply --- .../com/diffplug/gradle/spotless/DiffMessageFormatterTest.java | 2 +- .../test/java/com/diffplug/gradle/spotless/FormatTaskTest.java | 2 +- .../java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java index c564664f10..bc556ac06e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java index c4c055c799..ddd813468d 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java index db55077d00..4fe78d884d 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 696a3c628117aca1608ebbf111768ba0411a4b0f Mon Sep 17 00:00:00 2001 From: Simon Le Bras Date: Tue, 8 Aug 2023 12:09:53 +0200 Subject: [PATCH 04/11] Fix configuration cache issue with BufStep --- .../diffplug/spotless/protobuf/BufStep.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java index c9b9efa543..acddff85cf 100644 --- a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java +++ b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java @@ -21,6 +21,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; import javax.annotation.Nullable; @@ -61,13 +62,12 @@ public FormatterStep create() { return FormatterStep.createLazy(name(), this::createState, State::toFunc); } - private State createState() throws IOException, InterruptedException { + private State createState() { String instructions = "https://docs.buf.build/installation"; - String exeAbsPath = ForeignExe.nameAndVersion("buf", version) - .pathToExe(pathToExe) - .versionRegex(Pattern.compile("(\\S*)")) - .fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}") - .confirmVersionAndGetAbsolutePath(); + ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version) + .pathToExe(pathToExe) + .versionRegex(Pattern.compile("(\\S*)")) + .fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}"); return new State(this, exeAbsPath); } @@ -76,19 +76,23 @@ static class State implements Serializable { private static final long serialVersionUID = -1825662356883926318L; // used for up-to-date checks and caching final String version; + final transient ForeignExe exe; // used for executing - final transient List args; + private transient @Nullable List args; - State(BufStep step, String exeAbsPath) { + State(BufStep step, ForeignExe exeAbsPath) { this.version = step.version; - this.args = Arrays.asList(exeAbsPath, "format"); + this.exe = Objects.requireNonNull(exeAbsPath); } String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException { - String[] processArgs = args.toArray(new String[args.size() + 1]); - // add an argument to the end - processArgs[args.size()] = file.getAbsolutePath(); - return runner.exec(input.getBytes(StandardCharsets.UTF_8), processArgs).assertExitZero(StandardCharsets.UTF_8); + if (args == null) { + args = Arrays.asList( + exe.confirmVersionAndGetAbsolutePath(), + "format", + file.getAbsolutePath()); + } + return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8); } FormatterFunc.Closeable toFunc() { From 7878351838617e58959df3b47528a77f17efd4fa Mon Sep 17 00:00:00 2001 From: Simon Le Bras Date: Tue, 8 Aug 2023 12:20:17 +0200 Subject: [PATCH 05/11] Update CHANGES.md --- plugin-gradle/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 5ffc7912ba..907f1a1cb0 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -9,6 +9,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) * Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644)) +* Fix configuration cache failure when formatting proto files with Buf. ([#1779]https://github.com/diffplug/spotless/pull/1779)) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760)) From 19f68d6763969e289591a82c353b3638f1c40210 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 8 Aug 2023 09:11:17 -0700 Subject: [PATCH 06/11] spotlessApply --- .../java/com/diffplug/spotless/protobuf/BufStep.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java index acddff85cf..a5a6a6cdef 100644 --- a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java +++ b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java @@ -65,9 +65,9 @@ public FormatterStep create() { private State createState() { String instructions = "https://docs.buf.build/installation"; ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version) - .pathToExe(pathToExe) - .versionRegex(Pattern.compile("(\\S*)")) - .fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}"); + .pathToExe(pathToExe) + .versionRegex(Pattern.compile("(\\S*)")) + .fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}"); return new State(this, exeAbsPath); } @@ -88,9 +88,9 @@ static class State implements Serializable { String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException { if (args == null) { args = Arrays.asList( - exe.confirmVersionAndGetAbsolutePath(), - "format", - file.getAbsolutePath()); + exe.confirmVersionAndGetAbsolutePath(), + "format", + file.getAbsolutePath()); } return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8); } From 012c13569c101086137cee96ac2ec4e5c5398749 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 9 Aug 2023 09:22:34 +0800 Subject: [PATCH 07/11] Support GJF own import order --- CHANGES.md | 1 + .../java/GoogleJavaFormatFormatterFunc.java | 12 ++++++---- .../spotless/java/GoogleJavaFormatStep.java | 23 ++++++++++++++----- plugin-gradle/CHANGES.md | 1 + plugin-gradle/README.md | 2 +- .../gradle/spotless/JavaExtension.java | 9 +++++++- plugin-maven/CHANGES.md | 1 + .../spotless/maven/java/GoogleJavaFormat.java | 8 +++++-- 8 files changed, 42 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2b60913d67..81af46a526 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] ### Added * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) +* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed * Use latest versions of popular style guides for `eslint` tests to fix failing `useEslintXoStandardRules` test. ([#1761](https://github.com/diffplug/spotless/pull/1761), [#1756](https://github.com/diffplug/spotless/issues/1756)) * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) diff --git a/lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java b/lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java index 87e086b440..8c6aa42575 100644 --- a/lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java +++ b/lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java @@ -29,6 +29,8 @@ import com.diffplug.spotless.FormatterFunc; +// Used via reflection by the Gradle plugin. +@SuppressWarnings("unused") public class GoogleJavaFormatFormatterFunc implements FormatterFunc { @Nonnull @@ -42,10 +44,13 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc { private final boolean reflowStrings; - public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) { + private final boolean reorderImports; + + public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) { this.version = Objects.requireNonNull(version); this.formatterStyle = Style.valueOf(Objects.requireNonNull(style)); this.reflowStrings = reflowStrings; + this.reorderImports = reorderImports; this.formatter = new Formatter(JavaFormatterOptions.builder() .style(formatterStyle) @@ -57,10 +62,7 @@ public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String st public String apply(@Nonnull String input) throws Exception { String formatted = formatter.formatSource(input); String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted); - // Issue #1679: we used to call ImportOrderer.reorderImports(String) here, but that is deprecated. - // Replacing the call with (the correct) reorderImports(String, Style) causes issues for Style.AOSP, - // so we force the style to GOOGLE for now (which is what the deprecated method did) - String sortedImports = ImportOrderer.reorderImports(removedUnused, Style.GOOGLE); + String sortedImports = ImportOrderer.reorderImports(removedUnused, reorderImports ? formatterStyle : Style.GOOGLE); return reflowLongStrings(sortedImports); } 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 321d05420f..a0f796af4d 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -32,6 +32,7 @@ private GoogleJavaFormatStep() {} private static final String DEFAULT_STYLE = "GOOGLE"; private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false; + private static final boolean DEFAULT_REORDER_IMPORTS = false; static final String NAME = "google-java-format"; static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format"; @@ -55,8 +56,12 @@ public static FormatterStep create(String version, String style, Provisioner pro 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) { + return create(groupArtifact, version, style, provisioner, reflowLongStrings, false); + } + + /** 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, boolean reorderImports) { Objects.requireNonNull(groupArtifact, "groupArtifact"); if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) { throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'"); @@ -65,7 +70,7 @@ public static FormatterStep create(String groupArtifact, String version, String Objects.requireNonNull(style, "style"); Objects.requireNonNull(provisioner, "provisioner"); return FormatterStep.createLazy(NAME, - () -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings), + () -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports), State::createFormat); } @@ -92,6 +97,10 @@ public static boolean defaultReflowLongStrings() { return DEFAULT_REFLOW_LONG_STRINGS; } + public static boolean defaultReorderImports() { + return DEFAULT_REORDER_IMPORTS; + } + static final class State implements Serializable { private static final long serialVersionUID = 1L; @@ -101,6 +110,7 @@ static final class State implements Serializable { final String version; final String style; final boolean reflowLongStrings; + final boolean reorderImports; State(String stepName, String version, Provisioner provisioner) throws Exception { this(stepName, version, DEFAULT_STYLE, provisioner); @@ -111,10 +121,10 @@ 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); + this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS); } - State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception { + State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception { JVM_SUPPORT.assertFormatterSupported(version); ModuleHelper.doOpenInternalPackagesIfRequired(); this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); @@ -122,13 +132,14 @@ static final class State implements Serializable { this.version = version; this.style = style; this.reflowLongStrings = reflowLongStrings; + this.reorderImports = reorderImports; } FormatterFunc createFormat() throws Exception { final ClassLoader classLoader = jarState.getClassLoader(); Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc"); - Constructor constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class); - FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings); + Constructor constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class); + FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports); return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc); } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 907f1a1cb0..1ce2f06a42 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] ### Added * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) +* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 325093796e..dbc61207e5 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -206,7 +206,7 @@ spotless { // optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style // and/or reflow long strings // and/or use custom group artifact (you probably don't need this) - googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format') + googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format') ``` ### palantir-java-format 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 13fe674168..fcd1a0d105 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 @@ -173,6 +173,7 @@ public class GoogleJavaFormatConfig { String groupArtifact; String style; boolean reflowLongStrings; + boolean reorderImports; GoogleJavaFormatConfig(String version) { this.version = Objects.requireNonNull(version); @@ -207,13 +208,19 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) { return this; } + public GoogleJavaFormatConfig reorderImports(boolean reorderImports) { + this.reorderImports = reorderImports; + return this; + } + private FormatterStep createStep() { return GoogleJavaFormatStep.create( groupArtifact, version, style, provisioner(), - reflowLongStrings); + reflowLongStrings, + reorderImports); } } diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index bab8fca7b3..65a557b14f 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] ### Added * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) +* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) 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 3597e84e25..98296beb5f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,12 +35,16 @@ public class GoogleJavaFormat implements FormatterStepFactory { @Parameter private Boolean reflowLongStrings; + @Parameter + private Boolean reorderImports; + @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(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings); + boolean reorderImports = this.reorderImports != null ? this.reorderImports : GoogleJavaFormatStep.defaultReorderImports(); + return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports); } } From 959fd854c36080caa13123de2089ebcb7396adb4 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 24 Aug 2023 09:42:44 +0800 Subject: [PATCH 08/11] Check if EditorConfig file exist for Ktlint --- plugin-gradle/CHANGES.md | 1 + .../gradle/spotless/KotlinExtension.java | 10 +++++--- .../gradle/spotless/KotlinExtensionTest.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 1ce2f06a42..27d60e7270 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) +* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Fixed * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index f2c3a29d75..27d93717f4 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -85,11 +85,15 @@ public class KotlinFormatExtension { addStep(createStep()); } - public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException { - if (editorConfigFile == null) { + public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException { + if (editorConfigPath == null) { this.editorConfigPath = null; } else { - this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile)); + File editorConfigFile = getProject().file(editorConfigPath); + if (!editorConfigFile.exists()) { + throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile); + } + this.editorConfigPath = FileSignature.signAsList(editorConfigFile); } replaceStep(createStep()); return this; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 10fbe5bd90..98e2259be4 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -15,6 +15,9 @@ */ package com.diffplug.gradle.spotless; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; import java.io.IOException; import org.junit.jupiter.api.Test; @@ -81,6 +84,27 @@ void withExperimentalEditorConfigOverride() throws IOException { assertFile("src/main/kotlin/Main.kt").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean"); } + @Test + void testWithInvalidEditorConfigFile() throws IOException { + String invalidPath = "invalid/path/to/.editorconfig".replace('/', File.separatorChar); + + setFile("build.gradle").toLines( + "plugins {", + " id 'org.jetbrains.kotlin.jvm' version '1.5.31'", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " ktlint().setEditorConfigPath('" + invalidPath + "')", + " }", + "}"); + setFile("src/main/kotlin/Main.kt").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); + String buildOutput = gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput(); + assertTrue(buildOutput.contains("EditorConfig file does not exist: ")); + assertTrue(buildOutput.contains(invalidPath)); + } + @Test void testWithHeader() throws IOException { setFile("build.gradle").toLines( From b3c59f7d4b9e214c3f4c14d9333b816e3a90a34b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 24 Aug 2023 12:31:36 -0700 Subject: [PATCH 09/11] Fix on windows. --- .../com/diffplug/gradle/spotless/KotlinExtensionTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 98e2259be4..1c23728244 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -15,7 +15,7 @@ */ package com.diffplug.gradle.spotless; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; @@ -96,13 +96,13 @@ void testWithInvalidEditorConfigFile() throws IOException { "repositories { mavenCentral() }", "spotless {", " kotlin {", - " ktlint().setEditorConfigPath('" + invalidPath + "')", + " ktlint().setEditorConfigPath('" + invalidPath.replace("\\", "\\\\") + "')", " }", "}"); setFile("src/main/kotlin/Main.kt").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); String buildOutput = gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput(); - assertTrue(buildOutput.contains("EditorConfig file does not exist: ")); - assertTrue(buildOutput.contains(invalidPath)); + assertThat(buildOutput).contains("EditorConfig file does not exist: "); + assertThat(buildOutput).contains(invalidPath); } @Test From 62a957e51b723b3572bbe4beef9255e910d9c036 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 24 Aug 2023 12:31:48 -0700 Subject: [PATCH 10/11] Categorize as fix rather than new feature. --- plugin-gradle/CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 27d60e7270..2a5bd5db46 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -6,12 +6,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) -* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Fixed * Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) * Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) * Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644)) * Fix configuration cache failure when formatting proto files with Buf. ([#1779]https://github.com/diffplug/spotless/pull/1779)) +* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760)) From 83aace9d39b7037a430945e152b72580797c2a8a Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 25 Aug 2023 09:35:35 +0800 Subject: [PATCH 11/11] Fix changelog link formats --- CHANGES.md | 4 ++-- plugin-gradle/CHANGES.md | 8 ++++---- plugin-maven/CHANGES.md | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 81af46a526..905a78b8e6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,8 +15,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed * Use latest versions of popular style guides for `eslint` tests to fix failing `useEslintXoStandardRules` test. ([#1761](https://github.com/diffplug/spotless/pull/1761), [#1756](https://github.com/diffplug/spotless/issues/1756)) -* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) -* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) +* Add support for `prettier` version `3.0.0` and newer. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) +* Fix npm install calls when npm cache is not up-to-date. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760)) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2a5bd5db46..4e7f0d21be 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -7,11 +7,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed -* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) -* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) +* Add support for `prettier` version `3.0.0` and newer. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) +* Fix npm install calls when npm cache is not up-to-date. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) * Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644)) -* Fix configuration cache failure when formatting proto files with Buf. ([#1779]https://github.com/diffplug/spotless/pull/1779)) -* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) +* Fix configuration cache failure when formatting proto files with Buf. ([#1779](https://github.com/diffplug/spotless/pull/1779)) +* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788)) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 65a557b14f..d5526dd3c6 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,8 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) ### Fixed -* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) -* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) +* Add support for `prettier` version `3.0.0` and newer. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751)) +* Fix npm install calls when npm cache is not up-to-date. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750)) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))