Skip to content

Commit 9c03d4d

Browse files
committed
Create a spotlessSetup plugin, and repurpose RegisterDependenciesTask so that it stores a value which can be injected into the SpotlessTaskService on every build.
1 parent 33fba68 commit 9c03d4d

File tree

5 files changed

+88
-19
lines changed

5 files changed

+88
-19
lines changed

plugin-gradle/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ gradlePlugin {
4545
displayName = 'Spotless formatting plugin'
4646
description = project.description
4747
}
48+
spotlessSetupPlugin {
49+
id = 'com.diffplug.spotless-setup'
50+
implementationClass = 'com.diffplug.gradle.spotless.SpotlessSetupPlugin'
51+
displayName = 'Spotless formatting plugin setup'
52+
description = project.description
53+
}
4854
spotlessPluginLegacy {
4955
id = 'com.diffplug.gradle.spotless'
5056
implementationClass = 'com.diffplug.gradle.spotless.SpotlessPluginRedirect'
@@ -78,6 +84,9 @@ if (version.endsWith('-SNAPSHOT')) {
7884
'clang-format'
7985
]
8086
plugins {
87+
spotlessSetupPlugin {
88+
id = 'com.diffplug.spotless-setup'
89+
}
8190
spotlessPlugin {
8291
id = 'com.diffplug.spotless'
8392
}

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,17 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18-
import java.io.File;
19-
import java.io.IOException;
20-
import java.nio.charset.StandardCharsets;
21-
2218
import javax.inject.Inject;
2319

2420
import org.gradle.api.DefaultTask;
2521
import org.gradle.api.provider.Property;
2622
import org.gradle.api.services.BuildServiceRegistry;
23+
import org.gradle.api.tasks.Input;
2724
import org.gradle.api.tasks.Internal;
28-
import org.gradle.api.tasks.OutputFile;
2925
import org.gradle.api.tasks.TaskAction;
3026
import org.gradle.build.event.BuildEventsListenerRegistry;
3127

3228
import com.diffplug.common.base.Preconditions;
33-
import com.diffplug.common.io.Files;
3429

3530
/**
3631
* NOT AN END-USER TASK, DO NOT USE FOR ANYTHING!
@@ -59,26 +54,24 @@ void hookSubprojectTask(SpotlessTask task) {
5954
task.dependsOn(this);
6055
}
6156

62-
File unitOutput;
63-
64-
@OutputFile
65-
public File getUnitOutput() {
66-
return unitOutput;
67-
}
68-
6957
void setup() {
7058
Preconditions.checkArgument(getProject().getRootProject() == getProject(), "Can only be used on the root project");
71-
unitOutput = new File(getProject().getBuildDir(), "tmp/spotless-register-dependencies");
7259

7360
BuildServiceRegistry buildServices = getProject().getGradle().getSharedServices();
7461
getTaskService().set(buildServices.registerIfAbsent("SpotlessTaskService", SpotlessTaskService.class, spec -> {}));
7562
getBuildEventsListenerRegistry().onTaskCompletion(getTaskService());
7663
}
7764

65+
boolean enableConfigCacheDaemonLocal;
66+
67+
@Input
68+
public boolean getEnableConfigCacheDaemonLocal() {
69+
return enableConfigCacheDaemonLocal;
70+
}
71+
7872
@TaskAction
79-
public void trivialFunction() throws IOException {
80-
Files.createParentDirs(unitOutput);
81-
Files.write(Integer.toString(1), unitOutput, StandardCharsets.UTF_8);
73+
public void trivialFunction() {
74+
getTaskService().get().registerDependenciesTask(this);
8275
}
8376

8477
@Internal
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import org.gradle.api.Project;
19+
import org.gradle.api.tasks.TaskProvider;
20+
21+
public class SpotlessSetup {
22+
static final String NAME = "spotlessSetup";
23+
24+
private final TaskProvider<RegisterDependenciesTask> task;
25+
26+
public SpotlessSetup(Project project) {
27+
task = (TaskProvider<RegisterDependenciesTask>) (Object) project.getTasks().named(RegisterDependenciesTask.TASK_NAME);
28+
}
29+
30+
public boolean isEnableConfigCacheDaemonLocal() {
31+
return task.get().getEnableConfigCacheDaemonLocal();
32+
}
33+
34+
public boolean setEnableConfigCacheDaemonLocal(boolean enabled) {
35+
return task.get().enableConfigCacheDaemonLocal = enabled;
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import org.gradle.api.Plugin;
19+
import org.gradle.api.Project;
20+
21+
import com.diffplug.common.base.Preconditions;
22+
23+
public class SpotlessSetupPlugin implements Plugin<Project> {
24+
@Override
25+
public void apply(Project rootProject) {
26+
Preconditions.checkArgument(rootProject.getProject() == rootProject.getRootProject(), "com.diffplug.spotless-setup must be applied to only the root project");
27+
rootProject.getPlugins().apply(SpotlessPlugin.class);
28+
rootProject.getExtensions().create(SpotlessSetup.NAME, SpotlessSetup.class, rootProject);
29+
}
30+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ void testPathologicalCase() throws IOException {
8888
// the format task is UP-TO-DATE (same inputs), but the apply tasks will run again
8989
pauseForFilesystem();
9090
BuildResult buildResult = gradleRunner().withArguments("spotlessApply").build();
91-
Assertions.assertThat(buildResult.taskPaths(TaskOutcome.UP_TO_DATE)).containsExactly(":spotlessInternalRegisterDependencies", ":spotlessMisc");
92-
Assertions.assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).containsExactly(":spotlessMiscApply", ":spotlessApply");
91+
Assertions.assertThat(buildResult.taskPaths(TaskOutcome.UP_TO_DATE)).containsExactly(":spotlessMisc");
92+
Assertions.assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).containsExactly(":spotlessInternalRegisterDependencies", ":spotlessMiscApply", ":spotlessApply");
9393
assertFile("README.md").hasContent("abc");
9494

9595
// and it'll take two more runs to get to fully UP-TO-DATE

0 commit comments

Comments
 (0)