From 7ba9184aa96e29cc69cfea6014847cf64483712f Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Fri, 17 Sep 2021 10:30:42 +0200 Subject: [PATCH 1/3] Let RegisterDependenciesTask properly declare its usage of the GitRatchedGradle shared build service --- .../com/diffplug/gradle/spotless/FormatExtension.java | 2 +- .../gradle/spotless/RegisterDependenciesTask.java | 11 ++++------- 2 files changed, 5 insertions(+), 8 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 1f4f90352d..515f6230fc 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 @@ -725,7 +725,7 @@ protected void setupTask(SpotlessTask task) { spotless.getRegisterDependenciesTask().hookSubprojectTask(task); } if (getRatchetFrom() != null) { - task.setupRatchet(spotless.getRegisterDependenciesTask().gitRatchet, getRatchetFrom()); + task.setupRatchet(spotless.getRegisterDependenciesTask().getGitRatchet().get(), getRatchetFrom()); } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java index ff23eb6ab4..6e3a9192b4 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java @@ -23,6 +23,7 @@ import org.gradle.api.DefaultTask; import org.gradle.api.execution.TaskExecutionGraph; +import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.OutputFile; @@ -42,7 +43,7 @@ * - When this "registerDependencies" task does its up-to-date check, it queries the task execution graph to see which * SpotlessTasks are at risk of being executed, and causes them all to be evaluated safely in the root buildscript. */ -public class RegisterDependenciesTask extends DefaultTask { +public abstract class RegisterDependenciesTask extends DefaultTask { static final String TASK_NAME = "spotlessInternalRegisterDependencies"; @Input @@ -87,7 +88,7 @@ void setup() { Preconditions.checkArgument(getProject().getRootProject() == getProject(), "Can only be used on the root project"); unitOutput = new File(getProject().getBuildDir(), "tmp/spotless-register-dependencies"); rootProvisioner = new GradleProvisioner.RootProvisioner(getProject()); - gitRatchet = getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {}).get(); + getGitRatchet().set(getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {})); } @TaskAction @@ -96,10 +97,6 @@ public void trivialFunction() throws IOException { Files.write(Integer.toString(getSteps().size()), unitOutput, StandardCharsets.UTF_8); } - GitRatchetGradle gitRatchet; - @Internal - GitRatchetGradle getGitRatchet() { - return gitRatchet; - } + public abstract Property getGitRatchet(); } From 2cf2705ee5616258665b765d2c240574ea6fd8a5 Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Fri, 17 Sep 2021 10:38:17 +0200 Subject: [PATCH 2/3] Register GitRatchetGradle as an OperationCompletionListener in order to have Gradle call it's close() function when the build is finished --- .../diffplug/gradle/spotless/GitRatchetGradle.java | 9 ++++++++- .../gradle/spotless/RegisterDependenciesTask.java | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java index 7aee242a8f..0e35708c4b 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java @@ -22,11 +22,13 @@ import org.gradle.api.Project; import org.gradle.api.services.BuildService; import org.gradle.api.services.BuildServiceParameters; +import org.gradle.tooling.events.FinishEvent; +import org.gradle.tooling.events.OperationCompletionListener; import com.diffplug.spotless.extra.GitRatchet; /** Gradle implementation of GitRatchet. */ -public abstract class GitRatchetGradle extends GitRatchet implements BuildService { +public abstract class GitRatchetGradle extends GitRatchet implements BuildService, OperationCompletionListener { @Override protected File getDir(Project project) { return project.getProjectDir(); @@ -36,4 +38,9 @@ protected File getDir(Project project) { protected @Nullable Project getParent(Project project) { return project.getParent(); } + + @Override + public void onFinish(FinishEvent finishEvent) { + // NOOP + } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java index 6e3a9192b4..8c2e4d8734 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java @@ -24,15 +24,19 @@ import org.gradle.api.DefaultTask; import org.gradle.api.execution.TaskExecutionGraph; import org.gradle.api.provider.Property; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; +import org.gradle.build.event.BuildEventsListenerRegistry; import com.diffplug.common.base.Preconditions; import com.diffplug.common.io.Files; import com.diffplug.spotless.FormatterStep; +import javax.inject.Inject; + /** * NOT AN END-USER TASK, DO NOT USE FOR ANYTHING! * @@ -88,7 +92,9 @@ void setup() { Preconditions.checkArgument(getProject().getRootProject() == getProject(), "Can only be used on the root project"); unitOutput = new File(getProject().getBuildDir(), "tmp/spotless-register-dependencies"); rootProvisioner = new GradleProvisioner.RootProvisioner(getProject()); - getGitRatchet().set(getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {})); + Provider gitRatchetProvider = getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {}); + getBuildEventsListenerRegistry().onTaskCompletion(gitRatchetProvider); + getGitRatchet().set(gitRatchetProvider); } @TaskAction @@ -99,4 +105,7 @@ public void trivialFunction() throws IOException { @Internal public abstract Property getGitRatchet(); + + @Inject + protected abstract BuildEventsListenerRegistry getBuildEventsListenerRegistry(); } From e13f97a8ee532a58ad89b493ca7c911a0d6020a9 Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Fri, 17 Sep 2021 10:42:46 +0200 Subject: [PATCH 3/3] ./gradlew :plugin-gradle:spotlessApply --- .../java/com/diffplug/gradle/spotless/FormatExtension.java | 2 +- .../java/com/diffplug/gradle/spotless/GitRatchetGradle.java | 2 +- .../diffplug/gradle/spotless/RegisterDependenciesTask.java | 6 +++--- .../java/com/diffplug/gradle/spotless/SpotlessPlugin.java | 2 +- .../diffplug/gradle/spotless/ConfigurationCacheTest.java | 2 +- .../com/diffplug/gradle/spotless/GitRatchetGradleTest.java | 2 +- .../diffplug/gradle/spotless/GradleIntegrationHarness.java | 2 +- .../gradle/spotless/SpotlessPluginRedirectTest.java | 2 +- 8 files changed, 10 insertions(+), 10 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 515f6230fc..0e4c705115 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 @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 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/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java index 0e35708c4b..5a7f402b89 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 DiffPlug + * Copyright 2020-2021 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/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java index 8c2e4d8734..63a2a37b7c 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + import org.gradle.api.DefaultTask; import org.gradle.api.execution.TaskExecutionGraph; import org.gradle.api.provider.Property; @@ -35,8 +37,6 @@ import com.diffplug.common.io.Files; import com.diffplug.spotless.FormatterStep; -import javax.inject.Inject; - /** * NOT AN END-USER TASK, DO NOT USE FOR ANYTHING! * diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java index e66586d10d..786bf8e5fb 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 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/ConfigurationCacheTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java index 2907f16dba..bb0c99919e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 DiffPlug + * Copyright 2020-2021 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/GitRatchetGradleTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java index 417fe31cc7..9957d62d06 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 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/GradleIntegrationHarness.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java index c5112a0813..d2d053edb0 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 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/SpotlessPluginRedirectTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessPluginRedirectTest.java index 02bfa0bdba..f83f7219b3 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessPluginRedirectTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessPluginRedirectTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 DiffPlug + * Copyright 2020-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.