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 (JVM-local support only, just an idea) #721

Closed
wants to merge 6 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,14 @@ protected Project getProject() {
*/
public SpotlessApply createIndependentApplyTask(String taskName) {
// create and setup the task
SpotlessTask spotlessTask = spotless.project.getTasks().create(taskName + "Helper", SpotlessTaskImpl.class);
SpotlessTaskImpl spotlessTask = spotless.project.getTasks().create(taskName + "Helper", SpotlessTaskImpl.class);
setupTask(spotlessTask);
// enforce the clean ordering
Task clean = spotless.project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);
spotlessTask.mustRunAfter(clean);
// create the apply task
SpotlessApply applyTask = spotless.project.getTasks().create(taskName, SpotlessApply.class);
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
applyTask.linkSource(spotlessTask);
applyTask.link(spotlessTask);
applyTask.dependsOn(spotlessTask);

return applyTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@

import javax.annotation.Nullable;

import org.gradle.api.Project;
import org.gradle.api.services.BuildService;
import org.gradle.api.services.BuildServiceParameters;

import com.diffplug.spotless.extra.GitRatchet;

/** Gradle implementation of GitRatchet. */
public abstract class GitRatchetGradle extends GitRatchet<Project> implements BuildService<BuildServiceParameters.None> {
public abstract class GitRatchetGradle extends GitRatchet<File> implements BuildService<BuildServiceParameters.None> {
@Override
protected File getDir(Project project) {
return project.getProjectDir();
protected File getDir(File project) {
return project;
}

@Override
protected @Nullable Project getParent(Project project) {
return project.getParent();
protected @Nullable File getParent(File project) {
return project.getParentFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static void dumpIsClean() {
System.err.println("IS CLEAN");
}

static void performHook(SpotlessTask spotlessTask) {
static void performHook(SpotlessTaskImpl spotlessTask) {
String path = (String) spotlessTask.getProject().property(PROPERTY);
File file = new File(path);
if (!file.isAbsolute()) {
Expand All @@ -43,7 +43,7 @@ static void performHook(SpotlessTask spotlessTask) {
if (spotlessTask.getTarget().contains(file)) {
try (Formatter formatter = spotlessTask.buildFormatter()) {
if (spotlessTask.ratchet != null) {
if (spotlessTask.ratchet.isClean(spotlessTask.getProject(), spotlessTask.rootTreeSha, file)) {
if (spotlessTask.ratchet.isClean(spotlessTask.getProjectDir(), spotlessTask.rootTreeSha, file)) {
dumpIsClean();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,18 @@
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.FileVisitor;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

public class SpotlessApply extends DefaultTask {
private SpotlessTask source;

/** Bidirectional link between Apply and Spotless allows check to know if Apply ran or not. */
void linkSource(SpotlessTask source) {
this.source = source;
source.applyTask = this;
}

private File spotlessOutDirectory;

@PathSensitive(PathSensitivity.RELATIVE)
@InputDirectory
public File getSpotlessOutDirectory() {
return spotlessOutDirectory;
}

public void setSpotlessOutDirectory(File spotlessOutDirectory) {
this.spotlessOutDirectory = spotlessOutDirectory;
}

public abstract class SpotlessApply extends SpotlessTaskService.DependentTask {
@TaskAction
public void performAction() {
ConfigurableFileTree files = getProject().fileTree(spotlessOutDirectory);
SpotlessTaskImpl task = source();
ConfigurableFileTree files = task.outputFiles();
if (files.isEmpty()) {
getState().setDidWork(source.getDidWork());
getState().setDidWork(task.getDidWork());
} else {
files.visit(new FileVisitor() {
@Override
Expand All @@ -65,7 +42,7 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
@Override
public void visitFile(FileVisitDetails fileVisitDetails) {
String path = fileVisitDetails.getPath();
File originalSource = new File(getProject().getProjectDir(), path);
File originalSource = new File(task.getProjectDir(), path);
try {
getLogger().debug("Copying " + fileVisitDetails.getFile() + " to " + originalSource);
Files.copy(fileVisitDetails.getFile().toPath(), originalSource.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,18 @@
import java.util.Collections;
import java.util.List;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.FileVisitor;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class SpotlessCheck extends DefaultTask {
SpotlessTask source;
private File spotlessOutDirectory;

@PathSensitive(PathSensitivity.RELATIVE)
@InputDirectory
public File getSpotlessOutDirectory() {
return spotlessOutDirectory;
}

public void setSpotlessOutDirectory(File spotlessOutDirectory) {
this.spotlessOutDirectory = spotlessOutDirectory;
}
public abstract class SpotlessCheck extends SpotlessTaskService.DependentTask {

public void performActionTest() throws Exception {
performAction(true);
Expand All @@ -63,12 +47,10 @@ public void performAction() throws Exception {
}

private void performAction(boolean isTest) {
ConfigurableFileTree files = getProject().fileTree(spotlessOutDirectory);
SpotlessTaskImpl task = source();
ConfigurableFileTree files = task.outputFiles();
if (files.isEmpty()) {
getState().setDidWork(source.getDidWork());
} else if (!isTest && getProject().getGradle().getTaskGraph().hasTask(source.applyTask)) {
// if our matching apply has already run, then we don't need to do anything
getState().setDidWork(false);
getState().setDidWork(task.getDidWork());
} else {
List<File> problemFiles = new ArrayList<>();
files.visit(new FileVisitor() {
Expand All @@ -80,7 +62,7 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
@Override
public void visitFile(FileVisitDetails fileVisitDetails) {
String path = fileVisitDetails.getPath();
File originalSource = new File(getProject().getProjectDir(), path);
File originalSource = new File(task.getProjectDir(), path);
try {
// read the file on disk
byte[] userFile = Files.readAllBytes(originalSource.toPath());
Expand Down Expand Up @@ -109,7 +91,7 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
}
});
if (!problemFiles.isEmpty()) {
try (Formatter formatter = source.buildFormatter()) {
try (Formatter formatter = task.buildFormatter()) {
Collections.sort(problemFiles);
throw formatViolationsFor(formatter, problemFiles);
}
Expand All @@ -120,18 +102,12 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
/** Returns an exception which indicates problem files nicely. */
private GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) {
return new GradleException(DiffMessageFormatter.builder()
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
.runToFix("Run '" + calculateGradleCommand() + " " + SpotlessTaskImpl.getProjectPath(this) + "spotlessApply' to fix these violations.")
.formatter(formatter)
.problemFiles(problemFiles)
.getMessage());
}

private String getTaskPathPrefix() {
return getProject().getPath().equals(":")
? ":"
: getProject().getPath() + ":";
}

private static String calculateGradleCommand() {
return FileSignature.machineIsWin() ? "gradlew.bat" : "./gradlew";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -22,31 +22,23 @@
import java.nio.file.Paths;
import java.util.Locale;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.PaddedCell;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class SpotlessDiagnoseTask extends DefaultTask {
SpotlessTask source;

@Internal
public SpotlessTask getSource() {
return source;
}
public abstract class SpotlessDiagnoseTask extends SpotlessTaskService.DependentTask {

@TaskAction
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public void performAction() throws IOException {
Path srcRoot = getProject().getProjectDir().toPath();
Path diagnoseRoot = getProject().getBuildDir().toPath().resolve("spotless-diagnose-" + source.formatName());
Path diagnoseRoot = getProject().getBuildDir().toPath().resolve("spotless-diagnose-" + source().formatName());
getProject().delete(diagnoseRoot.toFile());
try (Formatter formatter = source.buildFormatter()) {
for (File file : source.target) {
try (Formatter formatter = source().buildFormatter()) {
for (File file : source().target) {
getLogger().debug("Running padded cell check on " + file);
PaddedCell padded = PaddedCell.check(formatter, file);
if (!padded.misbehaved()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
TaskProvider<SpotlessApply> applyTask = tasks.register(taskName + APPLY, SpotlessApply.class, task -> {
task.setEnabled(!isIdeHook);
task.dependsOn(spotlessTask);
task.setSpotlessOutDirectory(spotlessTask.get().getOutputDirectory());
task.linkSource(spotlessTask.get());
task.link(spotlessTask.get());
});
rootApplyTask.configure(task -> {
task.dependsOn(applyTask);
Expand All @@ -104,17 +103,23 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
TaskProvider<SpotlessCheck> checkTask = tasks.register(taskName + CHECK, SpotlessCheck.class, task -> {
task.setEnabled(!isIdeHook);
task.dependsOn(spotlessTask);
task.setSpotlessOutDirectory(spotlessTask.get().getOutputDirectory());
task.source = spotlessTask.get();
task.link(spotlessTask.get());

// if the user runs both, make sure that apply happens first,
task.mustRunAfter(applyTask);
});
rootCheckTask.configure(task -> task.dependsOn(checkTask));

// no need to run check if apply is going to run
project.getGradle().getTaskGraph().whenReady(graph -> {
if (graph.hasTask(taskName + APPLY) && graph.hasTask(taskName + CHECK)) {
checkTask.get().setEnabled(false);
}
});

// create the diagnose task
TaskProvider<SpotlessDiagnoseTask> diagnoseTask = tasks.register(taskName + DIAGNOSE, SpotlessDiagnoseTask.class, task -> {
task.source = spotlessTask.get();
task.link(spotlessTask.get());
task.mustRunAfter(cleanTask);
});
rootDiagnoseTask.configure(task -> task.dependsOn(diagnoseTask));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.diffplug.gradle.spotless;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -38,19 +37,10 @@

import com.diffplug.spotless.FormatExceptionPolicy;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;

public class SpotlessTask extends DefaultTask {
SpotlessApply applyTask;

/** Internal use only, allows coordination between check and apply when they are in the same build */
@Internal
SpotlessApply getApplyTask() {
return applyTask;
}

// set by SpotlessExtension, but possibly overridden by FormatExtension
protected String encoding = "UTF-8";

Expand Down Expand Up @@ -88,8 +78,8 @@ public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {

public void setupRatchet(GitRatchetGradle gitRatchet, String ratchetFrom) {
ratchet = gitRatchet;
rootTreeSha = gitRatchet.rootTreeShaOf(getProject(), ratchetFrom);
subtreeSha = gitRatchet.subtreeShaOf(getProject(), rootTreeSha);
rootTreeSha = gitRatchet.rootTreeShaOf(getProject().getProjectDir(), ratchetFrom);
subtreeSha = gitRatchet.subtreeShaOf(getProject().getProjectDir(), rootTreeSha);
}

@Internal
Expand Down Expand Up @@ -166,14 +156,4 @@ String formatName() {
return name;
}
}

Formatter buildFormatter() {
return Formatter.builder()
.lineEndingsPolicy(lineEndingsPolicy)
.encoding(Charset.forName(encoding))
.rootDir(getProject().getRootDir().toPath())
.steps(steps)
.exceptionPolicy(exceptionPolicy)
.build();
}
}
Loading