Skip to content

Commit 998f188

Browse files
committed
Convert GitRatchetGradle to be File-based, which allows us to remove all getProject() except IdeHook.
1 parent cf4bdd1 commit 998f188

File tree

7 files changed

+42
-24
lines changed

7 files changed

+42
-24
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import javax.annotation.Nullable;
2121

22-
import org.gradle.api.Project;
2322
import org.gradle.api.services.BuildService;
2423
import org.gradle.api.services.BuildServiceParameters;
2524
import org.gradle.tooling.events.FinishEvent;
@@ -28,15 +27,15 @@
2827
import com.diffplug.spotless.extra.GitRatchet;
2928

3029
/** Gradle implementation of GitRatchet. */
31-
public abstract class GitRatchetGradle extends GitRatchet<Project> implements BuildService<BuildServiceParameters.None>, OperationCompletionListener {
30+
public abstract class GitRatchetGradle extends GitRatchet<File> implements BuildService<BuildServiceParameters.None>, OperationCompletionListener {
3231
@Override
33-
protected File getDir(Project project) {
34-
return project.getProjectDir();
32+
protected File getDir(File project) {
33+
return project;
3534
}
3635

3736
@Override
38-
protected @Nullable Project getParent(Project project) {
39-
return project.getParent();
37+
protected @Nullable File getParent(File project) {
38+
return project.getParentFile();
4039
}
4140

4241
@Override

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ private static void dumpIsClean() {
3333
System.err.println("IS CLEAN");
3434
}
3535

36-
static void performHook(SpotlessTask spotlessTask) {
36+
static void performHook(SpotlessTaskImpl spotlessTask) {
3737
String path = (String) spotlessTask.getProject().property(PROPERTY);
3838
File file = new File(path);
3939
if (!file.isAbsolute()) {
@@ -43,7 +43,7 @@ static void performHook(SpotlessTask spotlessTask) {
4343
if (spotlessTask.getTarget().contains(file)) {
4444
try (Formatter formatter = spotlessTask.buildFormatter()) {
4545
if (spotlessTask.ratchet != null) {
46-
if (spotlessTask.ratchet.isClean(spotlessTask.getProject(), spotlessTask.rootTreeSha, file)) {
46+
if (spotlessTask.ratchet.isClean(spotlessTask.getProjectDir().get().getAsFile(), spotlessTask.rootTreeSha, file)) {
4747
dumpIsClean();
4848
return;
4949
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessApply.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
4242
@Override
4343
public void visitFile(FileVisitDetails fileVisitDetails) {
4444
String path = fileVisitDetails.getPath();
45-
File originalSource = new File(getProject().getProjectDir(), path);
45+
File originalSource = new File(getProjectDir().get().getAsFile(), path);
4646
try {
4747
getLogger().debug("Copying " + fileVisitDetails.getFile() + " to " + originalSource);
4848
Files.copy(fileVisitDetails.getFile().toPath(), originalSource.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void visitDir(FileVisitDetails fileVisitDetails) {
6767
@Override
6868
public void visitFile(FileVisitDetails fileVisitDetails) {
6969
String path = fileVisitDetails.getPath();
70-
File originalSource = new File(getProject().getProjectDir(), path);
70+
File originalSource = new File(getProjectDir().get().getAsFile(), path);
7171
try {
7272
// read the file on disk
7373
byte[] userFile = Files.readAllBytes(originalSource.toPath());
@@ -100,7 +100,7 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
100100
throw new GradleException(DiffMessageFormatter.builder()
101101
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
102102
.formatterFolder(
103-
getProject().getRootDir().toPath(),
103+
getProjectDir().get().getAsFile().toPath(),
104104
getSpotlessOutDirectory().get().toPath(),
105105
getEncoding().get())
106106
.problemFiles(problemFiles)
@@ -109,10 +109,18 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
109109
}
110110
}
111111

112+
@Internal
113+
abstract Property<String> getProjectPath();
114+
115+
@Override
116+
void init(SpotlessTaskImpl impl) {
117+
super.init(impl);
118+
getProjectPath().set(getProject().getPath());
119+
}
120+
112121
private String getTaskPathPrefix() {
113-
return getProject().getPath().equals(":")
114-
? ":"
115-
: getProject().getPath() + ":";
122+
String path = getProjectPath().get();
123+
return path.equals(":") ? ":" : path + ":";
116124
}
117125

118126
private static String calculateGradleCommand() {

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.eclipse.jgit.lib.ObjectId;
2929
import org.gradle.api.DefaultTask;
30+
import org.gradle.api.file.DirectoryProperty;
3031
import org.gradle.api.file.FileCollection;
3132
import org.gradle.api.tasks.Input;
3233
import org.gradle.api.tasks.InputFiles;
@@ -80,10 +81,14 @@ public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
8081

8182
public void setupRatchet(GitRatchetGradle gitRatchet, String ratchetFrom) {
8283
ratchet = gitRatchet;
83-
rootTreeSha = gitRatchet.rootTreeShaOf(getProject(), ratchetFrom);
84-
subtreeSha = gitRatchet.subtreeShaOf(getProject(), rootTreeSha);
84+
File projectDir = getProjectDir().get().getAsFile();
85+
rootTreeSha = gitRatchet.rootTreeShaOf(projectDir, ratchetFrom);
86+
subtreeSha = gitRatchet.subtreeShaOf(projectDir, rootTreeSha);
8587
}
8688

89+
@Internal
90+
abstract DirectoryProperty getProjectDir();
91+
8792
@Internal
8893
GitRatchetGradle getRatchet() {
8994
return ratchet;
@@ -163,7 +168,7 @@ Formatter buildFormatter() {
163168
return Formatter.builder()
164169
.lineEndingsPolicy(lineEndingsPolicy)
165170
.encoding(Charset.forName(encoding))
166-
.rootDir(getProject().getRootDir().toPath())
171+
.rootDir(getProjectDir().get().getAsFile().toPath())
167172
.steps(steps)
168173
.exceptionPolicy(exceptionPolicy)
169174
.build();

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void processInputFile(Formatter formatter, File input) throws IOExceptio
8686
File output = getOutputFile(input);
8787
getLogger().debug("Applying format to " + input + " and writing to " + output);
8888
PaddedCell.DirtyState dirtyState;
89-
if (ratchet != null && ratchet.isClean(getProject(), rootTreeSha, input)) {
89+
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), rootTreeSha, input)) {
9090
dirtyState = PaddedCell.isClean();
9191
} else {
9292
dirtyState = PaddedCell.calculateDirtyState(formatter, input);
@@ -118,12 +118,13 @@ private void deletePreviousResult(File input) throws IOException {
118118
}
119119

120120
private File getOutputFile(File input) {
121-
String outputFileName = FormatExtension.relativize(getProject().getProjectDir(), input);
121+
File projectDir = getProjectDir().get().getAsFile();
122+
String outputFileName = FormatExtension.relativize(projectDir, input);
122123
if (outputFileName == null) {
123124
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
124-
printer.println("Spotless error! All target files must be within the project root. In project " + getProject().getPath());
125-
printer.println(" root dir: " + getProject().getProjectDir().getAbsolutePath());
126-
printer.println(" target: " + input.getAbsolutePath());
125+
printer.println("Spotless error! All target files must be within the project root.");
126+
printer.println(" project dir: " + projectDir.getAbsolutePath());
127+
printer.println(" target: " + input.getAbsolutePath());
127128
}));
128129
}
129130
return new File(outputDirectory, outputFileName);

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.inject.Inject;
2424

2525
import org.gradle.api.DefaultTask;
26+
import org.gradle.api.file.DirectoryProperty;
2627
import org.gradle.api.model.ObjectFactory;
2728
import org.gradle.api.provider.Property;
2829
import org.gradle.api.services.BuildService;
@@ -59,12 +60,16 @@ static abstract class ClientTask extends DefaultTask {
5960
@Internal
6061
abstract Property<SpotlessTaskService> getTaskService();
6162

63+
@Internal
64+
abstract DirectoryProperty getProjectDir();
65+
6266
@Inject
6367
protected abstract ObjectFactory getConfigCacheWorkaround();
6468

6569
void init(SpotlessTaskImpl impl) {
6670
getSpotlessOutDirectory().set(impl.getOutputDirectory());
67-
getTaskService().set(impl.getTaskService());
71+
getTaskService().set(impl.getTakService());
72+
getProjectDir().set(impl.getProjectDir());
6873
}
6974

7075
String sourceTaskPath() {

0 commit comments

Comments
 (0)