From 64bd15b00fdd8953aa54f89762b1295a25ca98fb Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 3 May 2020 22:03:16 -0700 Subject: [PATCH 01/12] Proposal for `-PspotlessApplyOnlyTo` (faster alternative to `-PspotlessFiles`) --- plugin-gradle/README.md | 16 +++++++-- .../gradle/spotless/SpotlessExtension.java | 35 +++++++++++++++++++ .../gradle/spotless/SpotlessTask.java | 11 ++++-- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index ebe9e958d8..8ef8b4f826 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -675,13 +675,25 @@ spotless { ## Can I apply Spotless to specific files? -You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns: +### By absolute paths using `-PspotlessApplyOnlyTo` (fast!) + +You can target specific files by setting the `spotlessApplyOnlyTo` project property to a comma-separated list of file paths (absolute paths recommended). + +``` +cmd> gradlew spotlessApply "-PspotlessApplyOnlyTo=C:\Path\To\File.java,README.md" +``` + +The paths are resolved using [`Project.file`](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#file-java.lang.Object-), so for multi-project builds it is important to use absolute paths. + +### By regex using `-PspotlessFiles` (slow and deprecated) + +You can also target specific files by passing a comma-delimited list of regexes: ``` cmd> gradlew spotlessApply -PspotlessFiles=my/file/pattern.java,more/generic/.*-pattern.java ``` -The patterns are matched using `String#matches(String)` against the absolute file path. +The patterns are matched using `String#matches(String)` against the absolute file path. It is tricky to get this right, and executes far slower than `-PspotlessApplyOnlyTo`. ## Example configurations (from real-world projects) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 7ebb62bab4..64d8be46f5 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -17,11 +17,16 @@ import static java.util.Objects.requireNonNull; +import java.io.File; +import java.io.IOException; import java.lang.reflect.Constructor; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Collectors; import org.gradle.api.Action; import org.gradle.api.GradleException; @@ -31,7 +36,9 @@ import org.gradle.api.plugins.BasePlugin; import com.diffplug.common.base.Errors; +import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; +import com.diffplug.spotless.PaddedCell; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import groovy.lang.Closure; @@ -51,6 +58,7 @@ public class SpotlessExtension { private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place."; private static final String FILES_PROPERTY = "spotlessFiles"; + private static final String APPLY_ONLY_TO_PROPERTY = "spotlessApplyOnlyTo"; public SpotlessExtension(Project project) { this.project = requireNonNull(project); @@ -302,5 +310,32 @@ public Object doCall(TaskExecutionGraph graph) { diagnoseTask.source = spotlessTask; rootDiagnoseTask.dependsOn(diagnoseTask); diagnoseTask.mustRunAfter(clean); + + if (project.hasProperty(APPLY_ONLY_TO_PROPERTY)) { + // disable the normal tasks, to disable their up-to-date checking + spotlessTask.setEnabled(false); + checkTask.setEnabled(false); + applyTask.setEnabled(false); + // the rootApplyTask is no longer just a marker task, now it does a bit of work itself + rootApplyTask.doLast(APPLY_ONLY_TO_PROPERTY + " " + name, unused -> { + String[] paths = ((String) project.property(APPLY_ONLY_TO_PROPERTY)).split(","); + Collection filtered = Arrays.stream(paths) + .map(project::file) + .filter(spotlessTask.getTarget()::contains) + .collect(Collectors.toList()); + if (!filtered.isEmpty()) { + try (Formatter formatter = spotlessTask.buildFormatter()) { + for (File file : filtered) { + PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file); + if (!dirty.isClean() && !dirty.didNotConverge()) { + dirty.writeCanonicalTo(file); + } + } + } catch (IOException e) { + throw Errors.asRuntime(e); + } + } + }); + } } } 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 0a40d8b3ac..8cd04ed980 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 @@ -30,6 +30,7 @@ import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; +import org.gradle.api.file.FileCollection; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.Internal; @@ -104,15 +105,19 @@ public FormatExceptionPolicy getExceptionPolicy() { return exceptionPolicy; } - protected Iterable target; + protected FileCollection target; @Internal - public Iterable getTarget() { + public FileCollection getTarget() { return target; } public void setTarget(Iterable target) { - this.target = Objects.requireNonNull(target); + if (target instanceof FileCollection) { + this.target = (FileCollection) target; + } else { + this.target = getProject().files(target); + } } /** Internal use only. */ From f59d0ba69a682435d645d653cf9ea080f1ca9a31 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 5 May 2020 14:06:00 -0700 Subject: [PATCH 02/12] Refactor into `spotlessIdeHook` --- .../gradle/spotless/SpotlessExtension.java | 32 +++--- .../diffplug/gradle/spotless/IdeHookTest.java | 106 ++++++++++++++++++ 2 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 64d8be46f5..ffb1c4876e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -22,11 +22,8 @@ import java.lang.reflect.Constructor; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; -import java.util.stream.Collectors; import org.gradle.api.Action; import org.gradle.api.GradleException; @@ -58,7 +55,7 @@ public class SpotlessExtension { private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place."; private static final String FILES_PROPERTY = "spotlessFiles"; - private static final String APPLY_ONLY_TO_PROPERTY = "spotlessApplyOnlyTo"; + private static final String IDE_HOOK_PROPERTY = "spotlessIdeHook"; public SpotlessExtension(Project project) { this.project = requireNonNull(project); @@ -311,27 +308,28 @@ public Object doCall(TaskExecutionGraph graph) { rootDiagnoseTask.dependsOn(diagnoseTask); diagnoseTask.mustRunAfter(clean); - if (project.hasProperty(APPLY_ONLY_TO_PROPERTY)) { + if (project.hasProperty(IDE_HOOK_PROPERTY)) { // disable the normal tasks, to disable their up-to-date checking spotlessTask.setEnabled(false); checkTask.setEnabled(false); applyTask.setEnabled(false); // the rootApplyTask is no longer just a marker task, now it does a bit of work itself - rootApplyTask.doLast(APPLY_ONLY_TO_PROPERTY + " " + name, unused -> { - String[] paths = ((String) project.property(APPLY_ONLY_TO_PROPERTY)).split(","); - Collection filtered = Arrays.stream(paths) - .map(project::file) - .filter(spotlessTask.getTarget()::contains) - .collect(Collectors.toList()); - if (!filtered.isEmpty()) { + rootApplyTask.doLast(unused -> { + String path = (String) project.property(IDE_HOOK_PROPERTY); + File file = new File(path); + if (spotlessTask.getTarget().contains(file)) { try (Formatter formatter = spotlessTask.buildFormatter()) { - for (File file : filtered) { - PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file); - if (!dirty.isClean() && !dirty.didNotConverge()) { - dirty.writeCanonicalTo(file); - } + PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file); + if (dirty.isClean()) { + System.err.println("IS CLEAN"); + } else if (dirty.didNotConverge()) { + System.err.println("DID NOT CONVERGE"); + System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md"); + } else { + dirty.writeCanonicalTo(System.out); } } catch (IOException e) { + e.printStackTrace(System.err); throw Errors.asRuntime(e); } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java new file mode 100644 index 0000000000..a10d8f3bfe --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2016 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.gradle.spotless; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.StandardCharsets; + +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Test; + +import com.diffplug.common.base.StringPrinter; +import com.diffplug.common.io.Files; + +public class IdeHookTest extends GradleIntegrationTest { + private String output, error; + private File dirty, clean, diverge, outofbounds; + + @Before + public void before() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "spotless {", + " format 'misc', {", + " target 'DIRTY.md', 'CLEAN.md'", + " customLazyGroovy('lowercase') {", + " return { str -> str.toLowerCase(Locale.ROOT) }", + " }", + " }", + " format 'diverge', {", + " target 'DIVERGE.md'", + " customLazyGroovy('diverge') {", + " return { str -> str + ' ' }", + " }", + " }", + "}"); + dirty = new File(rootFolder(), "DIRTY.md"); + Files.write("ABC".getBytes(StandardCharsets.UTF_8), dirty); + clean = new File(rootFolder(), "CLEAN.md"); + Files.write("abc".getBytes(StandardCharsets.UTF_8), clean); + diverge = new File(rootFolder(), "DIVERGE.md"); + Files.write("ABC".getBytes(StandardCharsets.UTF_8), diverge); + outofbounds = new File(rootFolder(), "OUTOFBOUNDS.md"); + Files.write("ABC".getBytes(StandardCharsets.UTF_8), outofbounds); + } + + private void runWith(String... arguments) throws IOException { + StringBuilder output = new StringBuilder(); + StringBuilder error = new StringBuilder(); + try (Writer outputWriter = new StringPrinter(output::append).toWriter(); + Writer errorWriter = new StringPrinter(error::append).toWriter();) { + gradleRunner() + .withArguments(arguments) + .forwardStdOutput(outputWriter) + .forwardStdError(errorWriter) + .build(); + } + this.output = output.toString(); + this.error = error.toString(); + } + + @Test + public void dirty() throws IOException { + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + dirty.getAbsolutePath(), "--stacktrace"); + Assertions.assertThat(output).isEqualTo("abc"); + Assertions.assertThat(error).isEmpty(); + } + + @Test + public void clean() throws IOException { + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + clean.getAbsolutePath()); + Assertions.assertThat(output).isEmpty(); + Assertions.assertThat(error).startsWith("IS CLEAN"); + } + + @Test + public void diverge() throws IOException { + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + diverge.getAbsolutePath()); + Assertions.assertThat(output).isEmpty(); + Assertions.assertThat(error).startsWith("DID NOT CONVERGE"); + } + + @Test + public void outofbounds() throws IOException { + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + outofbounds.getAbsolutePath()); + Assertions.assertThat(output).isEmpty(); + Assertions.assertThat(error).isEmpty(); + } +} From 842bacccb568b23dcad4e176ac0a01c2b2372705 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 5 May 2020 14:27:10 -0700 Subject: [PATCH 03/12] Resolve a degenerate case involving empty files. --- .../com/diffplug/gradle/spotless/SpotlessExtension.java | 5 +++++ .../java/com/diffplug/gradle/spotless/IdeHookTest.java | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index ffb1c4876e..a929765468 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -317,6 +317,10 @@ public Object doCall(TaskExecutionGraph graph) { rootApplyTask.doLast(unused -> { String path = (String) project.property(IDE_HOOK_PROPERTY); File file = new File(path); + if (!file.isAbsolute()) { + System.err.println("Argument passed to " + IDE_HOOK_PROPERTY + " must be an absolute path"); + return; + } if (spotlessTask.getTarget().contains(file)) { try (Formatter formatter = spotlessTask.buildFormatter()) { PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file); @@ -326,6 +330,7 @@ public Object doCall(TaskExecutionGraph graph) { System.err.println("DID NOT CONVERGE"); System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md"); } else { + System.err.println("IS DIRTY"); dirty.writeCanonicalTo(System.out); } } catch (IOException e) { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java index a10d8f3bfe..c6d1cb0a7e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java @@ -80,7 +80,7 @@ private void runWith(String... arguments) throws IOException { public void dirty() throws IOException { runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + dirty.getAbsolutePath(), "--stacktrace"); Assertions.assertThat(output).isEqualTo("abc"); - Assertions.assertThat(error).isEmpty(); + Assertions.assertThat(error).startsWith("IS DIRTY"); } @Test @@ -103,4 +103,11 @@ public void outofbounds() throws IOException { Assertions.assertThat(output).isEmpty(); Assertions.assertThat(error).isEmpty(); } + + @Test + public void notAbsolute() throws IOException { + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=build.gradle"); + Assertions.assertThat(output).isEmpty(); + Assertions.assertThat(error).contains("Argument passed to spotlessIdeHook must be an absolute path"); + } } From c53e3455fcfdf8ca79b9b34461469cc67591ecb5 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 6 May 2020 09:01:40 -0700 Subject: [PATCH 04/12] Fix CircleCI memory exhaustion failures. --- .circleci/config.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3c6d67bfd0..c9e2f911d3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,8 +1,11 @@ anchors: env_gradle: &env_gradle environment: - _JAVA_OPTIONS: "-Xmx3g" - GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2" + # java doesn't play nice with containers, it tries to hog the entire machine + # https://circleci.com/blog/how-to-handle-java-oom-errors/ + # try the experimental JVM option + _JAVA_OPTIONS: "-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap" + GRADLE_OPTS: "-Dorg.gradle.workers.max=2" # and we're only allowed to use 2 vCPUs docker: - image: cimg/openjdk:8.0 @@ -67,6 +70,8 @@ jobs: <<: *env_gradle docker: - image: cimg/openjdk:11.0 + environment: # java 11 does play nice with containers, doesn't need special settings + _JAVA_OPTIONS: "" <<: *test_nomaven test_justmaven_8: << : *env_gradle From 855306c42cd14e9dcbdfd45ab9b7d4693030b608 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 6 May 2020 09:01:57 -0700 Subject: [PATCH 05/12] Fix IdeHookTest on JDK 11 (because it defaulted to testing with gradle 5, which has a stderr bug in the tooling API) --- .../java/com/diffplug/gradle/spotless/IdeHookTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java index c6d1cb0a7e..73abd0169e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java @@ -21,6 +21,7 @@ import java.nio.charset.StandardCharsets; import org.assertj.core.api.Assertions; +import org.gradle.testkit.runner.GradleRunner; import org.junit.Before; import org.junit.Test; @@ -66,7 +67,12 @@ private void runWith(String... arguments) throws IOException { StringBuilder error = new StringBuilder(); try (Writer outputWriter = new StringPrinter(output::append).toWriter(); Writer errorWriter = new StringPrinter(error::append).toWriter();) { - gradleRunner() + // gradle 2.14 -> 4.6 confirmed to work + // gradle 4.7 -> 5.1 don't work in tooling API because of https://github.com/gradle/gradle/issues/7617 + // gradle 5.1 -> current confirmed to work + GradleRunner.create() + .withProjectDir(rootFolder()) + .withPluginClasspath() .withArguments(arguments) .forwardStdOutput(outputWriter) .forwardStdError(errorWriter) From 1ccd0800bdd0d7ca1591d8d19e2a6073643c2a89 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 7 May 2020 10:22:29 -0700 Subject: [PATCH 06/12] IDE hook now reads file content from stdin - our test is now broken. --- lib/src/main/java/com/diffplug/spotless/PaddedCell.java | 4 ++++ .../java/com/diffplug/gradle/spotless/SpotlessExtension.java | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/PaddedCell.java b/lib/src/main/java/com/diffplug/spotless/PaddedCell.java index 0888e1646a..d801db038f 100644 --- a/lib/src/main/java/com/diffplug/spotless/PaddedCell.java +++ b/lib/src/main/java/com/diffplug/spotless/PaddedCell.java @@ -186,6 +186,10 @@ public static DirtyState calculateDirtyState(Formatter formatter, File file) thr Objects.requireNonNull(file, "file"); byte[] rawBytes = Files.readAllBytes(file.toPath()); + return calculateDirtyState(formatter, file, rawBytes); + } + + public static DirtyState calculateDirtyState(Formatter formatter, File file, byte[] rawBytes) throws IOException { String raw = new String(rawBytes, formatter.getEncoding()); String rawUnix = LineEnding.toUnix(raw); diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index a929765468..111939185f 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -33,6 +33,7 @@ import org.gradle.api.plugins.BasePlugin; import com.diffplug.common.base.Errors; +import com.diffplug.common.io.ByteStreams; import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.PaddedCell; @@ -323,7 +324,8 @@ public Object doCall(TaskExecutionGraph graph) { } if (spotlessTask.getTarget().contains(file)) { try (Formatter formatter = spotlessTask.buildFormatter()) { - PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file); + byte[] bytes = ByteStreams.toByteArray(System.in); + PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); if (dirty.isClean()) { System.err.println("IS CLEAN"); } else if (dirty.didNotConverge()) { From dce819fcc8c8621c59c587a55ab32aa4dc646024 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 7 May 2020 14:16:26 -0700 Subject: [PATCH 07/12] Refactore the IdeHook into its own class. --- .../com/diffplug/gradle/spotless/IdeHook.java | 55 +++++++++++++++++++ .../gradle/spotless/SpotlessExtension.java | 35 +----------- 2 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java new file mode 100644 index 0000000000..c8f08c0dc1 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 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.gradle.spotless; + +import java.io.File; +import java.io.IOException; + +import com.diffplug.common.base.Errors; +import com.diffplug.common.io.ByteStreams; +import com.diffplug.spotless.Formatter; +import com.diffplug.spotless.PaddedCell; + +class IdeHook { + final static String PROPERTY = "spotlessIdeHook"; + + static void performHook(SpotlessTask spotlessTask) { + String path = (String) spotlessTask.getProject().property(PROPERTY); + File file = new File(path); + if (!file.isAbsolute()) { + System.err.println("Argument passed to " + PROPERTY + " must be an absolute path"); + return; + } + if (spotlessTask.getTarget().contains(file)) { + try (Formatter formatter = spotlessTask.buildFormatter()) { + byte[] bytes = ByteStreams.toByteArray(System.in); + PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); + if (dirty.isClean()) { + System.err.println("IS CLEAN"); + } else if (dirty.didNotConverge()) { + System.err.println("DID NOT CONVERGE"); + System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md"); + } else { + System.err.println("IS DIRTY"); + dirty.writeCanonicalTo(System.out); + } + } catch (IOException e) { + e.printStackTrace(System.err); + throw Errors.asRuntime(e); + } + } + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 111939185f..ffb62ad039 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -17,8 +17,6 @@ import static java.util.Objects.requireNonNull; -import java.io.File; -import java.io.IOException; import java.lang.reflect.Constructor; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -33,10 +31,7 @@ import org.gradle.api.plugins.BasePlugin; import com.diffplug.common.base.Errors; -import com.diffplug.common.io.ByteStreams; -import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; -import com.diffplug.spotless.PaddedCell; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import groovy.lang.Closure; @@ -56,7 +51,6 @@ public class SpotlessExtension { private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place."; private static final String FILES_PROPERTY = "spotlessFiles"; - private static final String IDE_HOOK_PROPERTY = "spotlessIdeHook"; public SpotlessExtension(Project project) { this.project = requireNonNull(project); @@ -309,38 +303,13 @@ public Object doCall(TaskExecutionGraph graph) { rootDiagnoseTask.dependsOn(diagnoseTask); diagnoseTask.mustRunAfter(clean); - if (project.hasProperty(IDE_HOOK_PROPERTY)) { + if (project.hasProperty(IdeHook.PROPERTY)) { // disable the normal tasks, to disable their up-to-date checking spotlessTask.setEnabled(false); checkTask.setEnabled(false); applyTask.setEnabled(false); // the rootApplyTask is no longer just a marker task, now it does a bit of work itself - rootApplyTask.doLast(unused -> { - String path = (String) project.property(IDE_HOOK_PROPERTY); - File file = new File(path); - if (!file.isAbsolute()) { - System.err.println("Argument passed to " + IDE_HOOK_PROPERTY + " must be an absolute path"); - return; - } - if (spotlessTask.getTarget().contains(file)) { - try (Formatter formatter = spotlessTask.buildFormatter()) { - byte[] bytes = ByteStreams.toByteArray(System.in); - PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); - if (dirty.isClean()) { - System.err.println("IS CLEAN"); - } else if (dirty.didNotConverge()) { - System.err.println("DID NOT CONVERGE"); - System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md"); - } else { - System.err.println("IS DIRTY"); - dirty.writeCanonicalTo(System.out); - } - } catch (IOException e) { - e.printStackTrace(System.err); - throw Errors.asRuntime(e); - } - } - }); + rootApplyTask.doLast(unused -> IdeHook.performHook(spotlessTask)); } } } From e6781491082f81b7904cc35836590b3440d9783b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 7 May 2020 14:24:44 -0700 Subject: [PATCH 08/12] IdeHook now has `spotlessIdeHookUseStdIn` and `spotlessIdeHookUseStdOut` properties to configure where the I/O comes from. This makes it easier to test and more flexible. --- .../com/diffplug/gradle/spotless/IdeHook.java | 16 ++++++++++++++-- .../diffplug/gradle/spotless/IdeHookTest.java | 10 +++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java index c8f08c0dc1..baa9e63490 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import com.diffplug.common.base.Errors; import com.diffplug.common.io.ByteStreams; @@ -25,6 +26,8 @@ class IdeHook { final static String PROPERTY = "spotlessIdeHook"; + final static String USE_STD_IN = "spotlessIdeHookUseStdIn"; + final static String USE_STD_OUT = "spotlessIdeHookUseStdOut"; static void performHook(SpotlessTask spotlessTask) { String path = (String) spotlessTask.getProject().property(PROPERTY); @@ -35,7 +38,12 @@ static void performHook(SpotlessTask spotlessTask) { } if (spotlessTask.getTarget().contains(file)) { try (Formatter formatter = spotlessTask.buildFormatter()) { - byte[] bytes = ByteStreams.toByteArray(System.in); + byte[] bytes; + if (spotlessTask.getProject().hasProperty(USE_STD_IN)) { + bytes = ByteStreams.toByteArray(System.in); + } else { + bytes = Files.readAllBytes(file.toPath()); + } PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); if (dirty.isClean()) { System.err.println("IS CLEAN"); @@ -44,7 +52,11 @@ static void performHook(SpotlessTask spotlessTask) { System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md"); } else { System.err.println("IS DIRTY"); - dirty.writeCanonicalTo(System.out); + if (spotlessTask.getProject().hasProperty(USE_STD_OUT)) { + dirty.writeCanonicalTo(System.out); + } else { + dirty.writeCanonicalTo(file); + } } } catch (IOException e) { e.printStackTrace(System.err); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java index 73abd0169e..c13d3d98bb 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java @@ -84,35 +84,35 @@ private void runWith(String... arguments) throws IOException { @Test public void dirty() throws IOException { - runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + dirty.getAbsolutePath(), "--stacktrace"); + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + dirty.getAbsolutePath(), "-PspotlessIdeHookUseStdOut"); Assertions.assertThat(output).isEqualTo("abc"); Assertions.assertThat(error).startsWith("IS DIRTY"); } @Test public void clean() throws IOException { - runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + clean.getAbsolutePath()); + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + clean.getAbsolutePath(), "-PspotlessIdeHookUseStdOut"); Assertions.assertThat(output).isEmpty(); Assertions.assertThat(error).startsWith("IS CLEAN"); } @Test public void diverge() throws IOException { - runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + diverge.getAbsolutePath()); + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + diverge.getAbsolutePath(), "-PspotlessIdeHookUseStdOut"); Assertions.assertThat(output).isEmpty(); Assertions.assertThat(error).startsWith("DID NOT CONVERGE"); } @Test public void outofbounds() throws IOException { - runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + outofbounds.getAbsolutePath()); + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=" + outofbounds.getAbsolutePath(), "-PspotlessIdeHookUseStdOut"); Assertions.assertThat(output).isEmpty(); Assertions.assertThat(error).isEmpty(); } @Test public void notAbsolute() throws IOException { - runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=build.gradle"); + runWith("spotlessApply", "--quiet", "-PspotlessIdeHook=build.gradle", "-PspotlessIdeHookUseStdOut"); Assertions.assertThat(output).isEmpty(); Assertions.assertThat(error).contains("Argument passed to spotlessIdeHook must be an absolute path"); } From 1413cd1a954646de31ce1fa571ff43195255ab83 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 7 May 2020 15:12:30 -0700 Subject: [PATCH 09/12] Update changelog, add docs for `IDE_HOOK.md` --- plugin-gradle/CHANGES.md | 2 ++ plugin-gradle/IDE_HOOK.md | 26 ++++++++++++++++++++++++++ plugin-gradle/README.md | 31 ++++++++++++++++++------------- 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 plugin-gradle/IDE_HOOK.md diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 74d6a578b5..b4ec663675 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* `-PspotlessIdeHook` which makes the VS Code extension faster and more reliable. See [`IDE_INTEGRATION.md`](IDE_INTEGRATION.md) for more details. ([#568](https://github.com/diffplug/spotless/pull/568)) ## [3.29.0] - 2020-05-05 ### Added diff --git a/plugin-gradle/IDE_HOOK.md b/plugin-gradle/IDE_HOOK.md new file mode 100644 index 0000000000..8734245cad --- /dev/null +++ b/plugin-gradle/IDE_HOOK.md @@ -0,0 +1,26 @@ +# Spotless Gradle IDE integrations + +Thanks to `spotlessApply`, it is not necessary for Spotless and your IDE to agree on formatting - you can always run spotless at the end to fix things up. But if you want them to agree, there are two approaches: + +- 👎setup your IDE to match Spotless: tricky to get right, hard to keep up-to-date + - [eclipse](https://github.com/diffplug/spotless/blob/master/ECLIPSE_SCREENSHOTS.md) +- 👍setup your IDE to use Spotless as the source of truth: easy to setup, guaranteed to stay up-to-date + - [VS Code](https://marketplace.visualstudio.com/items?itemName=richardwillis.vscode-spotless-gradle) + - (add your IDE here!) + +## How to add an IDE + +The Spotless plugin for Gradle accepts a command-line argument `-PspotlessIdeHook=${ABSOLUTE_PATH_TO_FILE}`. In this mode, `spotlessCheck` is disabled, and `spotlessApply` will apply only to that one file. Because it already knows the absolute path of the only file you are asking about, it is able to run much faster than a normal invocation of `spotlessApply`. + +For extra flexibility, you can add `-PspotlessIdeHookUseStdIn`, and Spotless will read the file content from `stdin`. This allows you to send the content of a dirty editor buffer without writing to a file. You can also add `-PspotlessIdeHookUseStdOut`, and Spotless will return the formatted content on `stdout` rather than writing it to a file (you should also add `--quiet` to make sure Gradle doesn't dump logging info into `stdout`). + +In all of these cases, Spotless will send useful status information on `stderr`: + +- if `stderr` starts with `IS DIRTY`, then the file was dirty, and `stdout` contains its full formatted contents + - in every other case, `stdout` will be empty / the file will be unchanged because there is nothing to change +- if `stderr` starts with `IS CLEAN`, then the file is already clean +- if `stderr` starts with `DID NOT CONVERGE`, then the formatter is misbehaving, and the rest of `stderr` has useful diagnostic info (e.g. `spotlessDiagnose` for [padded cell](../PADDEDCELL.md)) +- if `stderr` is empty, then the file is not being formatted by spotless (not included in any target) +- if `stderr` is anything else, then it is the stacktrace of whatever went wrong + +See the VS Code extension above for a working example, or [the PR](https://github.com/diffplug/spotless/pull/568) where this feature was added for more context. diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index ec18fb6172..6ab0385b5b 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -1,34 +1,39 @@ -# Spotless: Keep your code spotless with Gradle +# Spotless plugin for Gradle +*Keep your code Spotless with Gradle* [![Gradle plugin](https://img.shields.io/badge/plugins.gradle.org-com.diffplug.gradle.spotless-blue.svg)](https://plugins.gradle.org/plugin/com.diffplug.gradle.spotless) -[![Maven central](https://img.shields.io/badge/mavencentral-com.diffplug.gradle.spotless%3Aspotless-blue.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.diffplug.spotless%22%20AND%20a%3A%22spotless-plugin-gradle%22) -[![Javadoc](https://img.shields.io/badge/javadoc-3.29.0-blue.svg)](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/3.29.0/index.html) +[![Maven central](https://img.shields.io/badge/mavencentral-yes-blue.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.diffplug.spotless%22%20AND%20a%3A%22spotless-plugin-gradle%22) +[![Javadoc](https://img.shields.io/badge/javadoc-yes-blue.svg)](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/3.29.0/index.html) +[![License Apache](https://img.shields.io/badge/license-apache-blue.svg)](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)) +[![Changelog](https://img.shields.io/badge/changelog-3.29.0-blue.svg)](CHANGES.md) -[![Changelog](https://img.shields.io/badge/changelog-3.29.0-brightgreen.svg)](CHANGES.md) -[![Travis CI](https://travis-ci.org/diffplug/spotless.svg?branch=master)](https://travis-ci.org/diffplug/spotless) +[![Circle CI](https://circleci.com/gh/diffplug/spotless/tree/master.svg?style=shield)](https://circleci.com/gh/diffplug/spotless/tree/master) [![Live chat](https://img.shields.io/badge/gitter-chat-brightgreen.svg)](https://gitter.im/diffplug/spotless) -[![License Apache](https://img.shields.io/badge/license-apache-brightgreen.svg)](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)) +[![VS Code plugin Apache](https://img.shields.io/badge/IDE-VS_Code-blueviolet.svg)](https://marketplace.visualstudio.com/items?itemName=richardwillis.vscode-spotless-gradle) +[![VS Code plugin Apache](https://img.shields.io/badge/IDE-add_yours-blueviolet.svg)](IDE_HOOK.md) -Spotless is a general-purpose formatting plugin used by [2,700 projects on GitHub (Jan 2020)](https://github.com/search?l=gradle&q=spotless&type=Code). It is completely à la carte, but also includes powerful "batteries-included" if you opt-in. +Spotless is a general-purpose formatting plugin used by [3,500 projects on GitHub (May 2020)](https://github.com/search?l=gradle&q=spotless&type=Code). It is completely à la carte, but also includes powerful "batteries-included" if you opt-in. -To people who use your build, it looks like this: +To people who use your build, it looks like this ([IDE support also available]()): ``` cmd> gradlew build From 13700cbe45344f45cbef79cbba94aec6724757f8 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 7 May 2020 15:14:35 -0700 Subject: [PATCH 10/12] This should allow a speedup, but may cause errors... --- .../src/main/java/com/diffplug/gradle/spotless/IdeHook.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java index baa9e63490..eb21fb5547 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java @@ -58,6 +58,8 @@ static void performHook(SpotlessTask spotlessTask) { dirty.writeCanonicalTo(file); } } + System.err.close(); + System.out.close(); } catch (IOException e) { e.printStackTrace(System.err); throw Errors.asRuntime(e); From d30a1ee0d5b9e36ecf7f877ba873d6c798c97b24 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 11 May 2020 09:54:03 -0700 Subject: [PATCH 11/12] Update lib changelog. --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 0ddf566b74..f9a9af5394 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* `PaddedCell.calculateDirtyState(Formatter, File, byte[])` to allow IDE integrations to send dirty editor buffers. ## [1.29.0] - 2020-05-05 ### Added From f53f5536a4533b6c5e82d12dde2ecc86f144c475 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 11 May 2020 09:54:23 -0700 Subject: [PATCH 12/12] Fix plugin-gradle readme since we didn't add `PspotlessApplyOnlyTo` --- plugin-gradle/README.md | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 6ab0385b5b..f6ca7af118 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -698,25 +698,13 @@ spotless { ## Can I apply Spotless to specific files? -### By absolute paths using `-PspotlessApplyOnlyTo` (fast!) - -You can target specific files by setting the `spotlessApplyOnlyTo` project property to a comma-separated list of file paths (absolute paths recommended). - -``` -cmd> gradlew spotlessApply "-PspotlessApplyOnlyTo=C:\Path\To\File.java,README.md" -``` - -The paths are resolved using [`Project.file`](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#file-java.lang.Object-), so for multi-project builds it is important to use absolute paths. - -### By regex using `-PspotlessFiles` (slow and deprecated) - -You can also target specific files by passing a comma-delimited list of regexes: +You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns: ``` cmd> gradlew spotlessApply -PspotlessFiles=my/file/pattern.java,more/generic/.*-pattern.java ``` -The patterns are matched using `String#matches(String)` against the absolute file path. It is tricky to get this right, and executes far slower than `-PspotlessApplyOnlyTo`. +The patterns are matched using `String#matches(String)` against the absolute file path. ## Example configurations (from real-world projects)