Skip to content

Commit 5244a7e

Browse files
authored
Merge pull request #584 from diffplug/fix/581
Fix `check` and `apply` in the same build
2 parents d1fb248 + f0d334f commit 5244a7e

File tree

10 files changed

+68
-34
lines changed

10 files changed

+68
-34
lines changed

plugin-gradle/CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6-
* Support for ktfmt in KotlinGradleExtension ([#583](https://github.com/diffplug/spotless/pull/583))
6+
### Added
7+
* Support for ktfmt in KotlinGradleExtension. ([#583](https://github.com/diffplug/spotless/pull/583))
8+
### Fixed
9+
* Users can now run `spotlessCheck` and `spotlessApply` in the same build. ([#584](https://github.com/diffplug/spotless/pull/584))
710

811
## [4.0.1] - 2020-05-21
912
### Fixed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public SpotlessApply createIndependentApplyTask(String taskName) {
635635
// create the apply task
636636
SpotlessApply applyTask = root.project.getTasks().create(taskName, SpotlessApply.class);
637637
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
638-
applyTask.source = spotlessTask;
638+
applyTask.linkSource(spotlessTask);
639639
applyTask.dependsOn(spotlessTask);
640640

641641
return applyTask;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
import org.gradle.api.tasks.TaskAction;
3131

3232
public class SpotlessApply extends DefaultTask {
33-
SpotlessTask source;
33+
private SpotlessTask source;
34+
35+
/** Bidirectional link between Apply and Spotless allows check to know if Apply ran or not. */
36+
void linkSource(SpotlessTask source) {
37+
this.source = source;
38+
source.applyTask = this;
39+
}
40+
3441
private File spotlessOutDirectory;
3542

3643
@PathSensitive(PathSensitivity.RELATIVE)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,22 @@ public void setSpotlessOutDirectory(File spotlessOutDirectory) {
4949
this.spotlessOutDirectory = spotlessOutDirectory;
5050
}
5151

52+
public void performActionTest() throws Exception {
53+
performAction(true);
54+
}
55+
5256
@TaskAction
5357
public void performAction() throws Exception {
58+
performAction(false);
59+
}
60+
61+
private void performAction(boolean isTest) {
5462
ConfigurableFileTree files = getProject().fileTree(spotlessOutDirectory);
5563
if (files.isEmpty()) {
5664
getState().setDidWork(source.getDidWork());
65+
} else if (!isTest && getProject().getGradle().getTaskGraph().hasTask(source.applyTask)) {
66+
// if our matching apply has already run, then we don't need to do anything
67+
getState().setDidWork(false);
5768
} else {
5869
List<File> problemFiles = new ArrayList<>();
5970
files.visit(new FileVisitor() {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ private <T extends FormatExtension> T maybeCreate(String name, Class<T> clazz) {
249249
* - "spotless{FormatName}Check" will depend on the main spotless task in `check` mode
250250
* - "spotless{FormatName}Apply" will depend on the main spotless task in `apply` mode
251251
*/
252-
@SuppressWarnings("rawtypes")
253252
private void createFormatTasks(String name, FormatExtension formatExtension) {
254253
// create the SpotlessTask
255254
String taskName = EXTENSION + SpotlessPlugin.capitalize(name);
@@ -268,9 +267,12 @@ private void createFormatTasks(String name, FormatExtension formatExtension) {
268267

269268
SpotlessApply applyTask = project.getTasks().create(taskName + APPLY, SpotlessApply.class);
270269
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
271-
applyTask.source = spotlessTask;
270+
applyTask.linkSource(spotlessTask);
272271
applyTask.dependsOn(spotlessTask);
273272

273+
// if the user runs both, make sure that apply happens first,
274+
checkTask.mustRunAfter(applyTask);
275+
274276
// set the filePatterns property
275277
project.afterEvaluate(unused -> {
276278
String filePatterns;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353

5454
@CacheableTask
5555
public class SpotlessTask extends DefaultTask {
56+
SpotlessApply applyTask;
57+
58+
/** @deprecated internal use only, allows coordination between check and apply when they are in the same build */
59+
@Internal
60+
@Deprecated
61+
public SpotlessApply getApplyTask() {
62+
return applyTask;
63+
}
64+
5665
// set by SpotlessExtension, but possibly overridden by FormatExtension
5766
protected String encoding = "UTF-8";
5867

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,16 @@
3939
public class DiffMessageFormatterTest extends ResourceHarness {
4040

4141
private class Bundle {
42-
String name;
4342
Project project = TestProvisioner.gradleProject(rootFolder());
4443
File file;
45-
File outputFile;
4644
SpotlessTask task;
4745
SpotlessCheck check;
48-
SpotlessApply apply;
4946

5047
Bundle(String name) throws IOException {
51-
this.name = name;
5248
file = setFile("src/test." + name).toContent("CCC");
5349
task = createFormatTask(name);
5450
check = createCheckTask(name, task);
55-
apply = createApplyTask(name, task);
56-
outputFile = new File(task.getOutputDirectory() + "/src", file.getName());
51+
createApplyTask(name, task);
5752
}
5853

5954
private SpotlessTask createFormatTask(String name) {
@@ -72,7 +67,7 @@ private SpotlessCheck createCheckTask(String name, SpotlessTask source) {
7267

7368
private SpotlessApply createApplyTask(String name, SpotlessTask source) {
7469
SpotlessApply task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name) + "Apply", SpotlessApply.class);
75-
task.source = source;
70+
task.linkSource(this.task);
7671
task.setSpotlessOutDirectory(source.getOutputDirectory());
7772
return task;
7873
}
@@ -86,24 +81,9 @@ String checkFailureMsg() {
8681
}
8782
}
8883

89-
void diagnose() throws IOException {
90-
SpotlessDiagnoseTask diagnose = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name) + "Diagnose", SpotlessDiagnoseTask.class);
91-
diagnose.source = task;
92-
diagnose.performAction();
93-
}
94-
95-
void format() throws Exception {
96-
execute(task);
97-
}
98-
99-
void apply() throws Exception {
100-
execute(task);
101-
apply.performAction();
102-
}
103-
10484
void check() throws Exception {
10585
execute(task);
106-
check.performAction();
86+
check.performActionTest();
10787
}
10888
}
10989

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private SpotlessCheck createCheckTask(String name, SpotlessTask source) {
7676

7777
private SpotlessApply createApplyTask(String name, SpotlessTask source) {
7878
SpotlessApply task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name) + "Apply", SpotlessApply.class);
79-
task.source = source;
79+
task.linkSource(source);
8080
task.setSpotlessOutDirectory(source.getOutputDirectory());
8181
return task;
8282
}
@@ -107,7 +107,7 @@ void apply() throws Exception {
107107

108108
void check() throws Exception {
109109
execute(task);
110-
check.performAction();
110+
check.performActionTest();
111111
}
112112
}
113113

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@
1616
package com.diffplug.gradle.spotless;
1717

1818
import java.io.IOException;
19+
import java.util.Locale;
1920

20-
import org.gradle.testkit.runner.*;
21+
import org.gradle.testkit.runner.GradleRunner;
22+
import org.gradle.testkit.runner.UnexpectedBuildFailure;
2123
import org.junit.Ignore;
2224
import org.junit.Test;
2325

26+
import com.diffplug.common.base.StandardSystemProperty;
27+
2428
public class SpecificFilesTest extends GradleIntegrationTest {
29+
private static boolean isWindows() {
30+
return StandardSystemProperty.OS_NAME.value().toLowerCase(Locale.US).contains("win");
31+
}
32+
33+
private static String regexWinSafe(String input) {
34+
return isWindows() ? input.replace("/", "\\\\") : input;
35+
}
36+
2537
private String testFilePath(int number) {
2638
return testFilePath(number, true);
2739
}
2840

2941
private String testFilePath(int number, boolean absolute) {
3042
String relPath = "src/main/java/test" + number + ".java";
43+
String returnValue;
3144
if (absolute) {
32-
return rootFolder() + "/" + relPath;
45+
returnValue = rootFolder().getAbsolutePath().replace('\\', '/') + "/" + relPath;
3346
} else {
34-
return relPath;
47+
returnValue = relPath;
3548
}
49+
// regex-escape on windows;
50+
return regexWinSafe(returnValue);
3651
}
3752

3853
private String fixture() {
@@ -141,7 +156,7 @@ public void matchesNoFiles_formatsNoFilesButDoesNotExitInError() throws IOExcept
141156
@Test
142157
public void regexp() throws IOException {
143158
createBuildScript();
144-
integration(".*/src/main/java/test(1|3).java", true, false, true);
159+
integration(regexWinSafe(".*/src/main/java/test(1|3).java"), true, false, true);
145160
}
146161

147162
@Test(expected = UnexpectedBuildFailure.class)

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,11 @@ public void testPathologicalCase() throws IOException {
9898
applyIsUpToDate(false);
9999
applyIsUpToDate(true);
100100
}
101+
102+
@Test
103+
public void checkAndApply() throws IOException {
104+
writeBuildFile();
105+
setFile("README.md").toContent("ABC");
106+
gradleRunner().withArguments("spotlessCheck", "spotlessApply").build();
107+
}
101108
}

0 commit comments

Comments
 (0)