Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration cache, part 1 (play nice with others) #720

Merged
merged 10 commits into from
Sep 20, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,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());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,11 +20,15 @@
import javax.annotation.Nullable;

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. */
final class GitRatchetGradle extends GitRatchet<Project> {
public abstract class GitRatchetGradle extends GitRatchet<Project> implements BuildService<BuildServiceParameters.None>, OperationCompletionListener {
@Override
protected File getDir(Project project) {
return project.getProjectDir();
Expand All @@ -34,4 +38,9 @@ protected File getDir(Project project) {
protected @Nullable Project getParent(Project project) {
return project.getParent();
}

@Override
public void onFinish(FinishEvent finishEvent) {
// NOOP
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to confirm that we don't need to call close() here? GitRatchet implements AutoCloseable, so if Gradle is promising to call close() then I guess we're good, but it's odd that there's also an onFinish() which seems like it would get called at the same time as close()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OperationCompletionListener.onFinish is called on each "task finish" event.
AutoCloseable.close is called at the end of the build.

My understanding is that we are good.

}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,23 +20,23 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.inject.Inject;

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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.Closure;

/**
* NOT AN END-USER TASK, DO NOT USE FOR ANYTHING!
*
Expand All @@ -47,7 +47,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
Expand Down Expand Up @@ -87,18 +87,13 @@ public GradleProvisioner.RootProvisioner getRootProvisioner() {
return rootProvisioner;
}

@SuppressWarnings({"rawtypes", "serial"})
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());
getProject().getGradle().buildFinished(new Closure(null) {
@SuppressFBWarnings("UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS")
public Object doCall() {
gitRatchet.close();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where we used to call close()

return null;
}
});
Provider<GitRatchetGradle> gitRatchetProvider = getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {});
getBuildEventsListenerRegistry().onTaskCompletion(gitRatchetProvider);
getGitRatchet().set(gitRatchetProvider);
}

@TaskAction
Expand All @@ -107,10 +102,9 @@ public void trivialFunction() throws IOException {
Files.write(Integer.toString(getSteps().size()), unitOutput, StandardCharsets.UTF_8);
}

GitRatchetGradle gitRatchet = new GitRatchetGradle();

@Internal
GitRatchetGradle getGitRatchet() {
return gitRatchet;
}
public abstract Property<GitRatchetGradle> getGitRatchet();

@Inject
protected abstract BuildEventsListenerRegistry getBuildEventsListenerRegistry();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class SpotlessPlugin implements Plugin<Project> {
static final String SPOTLESS_MODERN = "spotlessModern";
static final String MINIMUM_GRADLE = "5.4";
static final String MINIMUM_GRADLE = "6.1";

@Override
public void apply(Project project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
* 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.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class ConfigurationCacheTest extends GradleIntegrationHarness {
protected void runTasks(String... tasks) throws IOException {
setFile("gradle.properties").toContent("org.gradle.unsafe.configuration-cache=true");
List<String> args = new ArrayList<>();
args.addAll(Arrays.asList(tasks));
gradleRunner()
.withGradleVersion(GradleVersionSupport.CONFIGURATION_CACHE.version)
.withArguments(args)
.forwardOutput()
.build();
}

@Test
public void helpConfigures() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"apply plugin: 'java'",
"spotless {",
" java {",
" googleJavaFormat('1.2')",
" }",
"}");
runTasks("help");
}

@Test
public void helpConfiguresIfTasksAreCreated() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"apply plugin: 'java'",
"spotless {",
" java {",
" googleJavaFormat('1.2')",
" }",
"}",
"tasks.named('spotlessJavaApply').get()");
runTasks("help");
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -128,11 +128,11 @@ private void assertDirty() throws Exception {
}

private BuildResultAssertion assertPass(String... tasks) throws Exception {
return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).build());
return new BuildResultAssertion(gradleRunner().withArguments(tasks).build());
}

private BuildResultAssertion assertFail(String... tasks) throws Exception {
return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).buildAndFail());
return new BuildResultAssertion(gradleRunner().withArguments(tasks).buildAndFail());
}

private static final String BASELINE_ROOT = "cf049829afeba064f27cd67911dc36e585c9d869";
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -39,7 +39,9 @@

public class GradleIntegrationHarness extends ResourceHarness {
public enum GradleVersionSupport {
JRE_11("5.0"), MINIMUM(SpotlessPlugin.MINIMUM_GRADLE), SETTINGS_PLUGINS("6.0");
JRE_11("5.0"), MINIMUM(SpotlessPlugin.MINIMUM_GRADLE),
// technically, this API exists in 6.5, but the flags for it change in 6.6, so we build to that
CONFIGURATION_CACHE("6.6");

final String version;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -52,7 +52,7 @@ public void redirectPluginOldGradle() throws IOException {
" > We have moved from 'com.diffplug.gradle.spotless'",
" to 'com.diffplug.spotless'",
" To migrate:",
" - Upgrade gradle to 5.4 or newer (you're on 5.0)",
" - Upgrade gradle to 6.1 or newer (you're on 5.0)",
" - Test your build with: id 'com.diffplug.gradle.spotless' version '4.5.1'"));
}

Expand All @@ -66,6 +66,6 @@ public void realPluginOldGradle() throws IOException {
Assertions.assertThat(gradleRunner().withGradleVersion(GradleVersionSupport.JRE_11.version)
.buildAndFail().getOutput().replace("\r", ""))
.contains(StringPrinter.buildStringFromLines(
"Spotless requires Gradle 5.4 or newer, this was 5.0"));
"Spotless requires Gradle 6.1 or newer, this was 5.0"));
}
}