Skip to content

Commit ff9bb5f

Browse files
committed
Add SpotlessTaskImpl.init to simplify that task as well.
1 parent e0b850e commit ff9bb5f

File tree

7 files changed

+29
-12
lines changed

7 files changed

+29
-12
lines changed

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
@@ -763,7 +763,7 @@ public SpotlessApply createIndependentApplyTask(String taskName) {
763763
Preconditions.checkArgument(!taskName.endsWith(SpotlessExtension.APPLY), "Task name must not end with " + SpotlessExtension.APPLY);
764764
// create and setup the task
765765
SpotlessTaskImpl spotlessTask = spotless.project.getTasks().create(taskName + SpotlessTaskService.INDEPENDENT_HELPER, SpotlessTaskImpl.class);
766-
spotlessTask.getTaskService().set(spotless.getTaskService());
766+
spotlessTask.init(spotless.getTaskService());
767767
setupTask(spotlessTask);
768768
// enforce the clean ordering
769769
Task clean = spotless.project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
7878
// create the SpotlessTask
7979
String taskName = EXTENSION + SpotlessPlugin.capitalize(name);
8080
TaskProvider<SpotlessTaskImpl> spotlessTask = tasks.register(taskName, SpotlessTaskImpl.class, task -> {
81-
task.getTaskService().set(taskService);
81+
task.init(taskService);
8282
task.setEnabled(!isIdeHook);
8383
// clean removes the SpotlessCache, so we have to run after clean
8484
task.mustRunAfter(cleanTask);

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import javax.inject.Inject;
2525

2626
import org.gradle.api.GradleException;
27+
import org.gradle.api.file.DirectoryProperty;
2728
import org.gradle.api.file.FileSystemOperations;
2829
import org.gradle.api.provider.Property;
30+
import org.gradle.api.provider.Provider;
2931
import org.gradle.api.tasks.CacheableTask;
3032
import org.gradle.api.tasks.Internal;
3133
import org.gradle.api.tasks.TaskAction;
@@ -40,14 +42,22 @@
4042
@CacheableTask
4143
public abstract class SpotlessTaskImpl extends SpotlessTask {
4244
@Internal
43-
abstract Property<SpotlessTaskService> getTaskService();
45+
abstract Property<SpotlessTaskService> getTakService();
46+
47+
@Internal
48+
abstract DirectoryProperty getProjectDir();
49+
50+
void init(Provider<SpotlessTaskService> service) {
51+
getTakService().set(service);
52+
getProjectDir().set(getProject().getProjectDir());
53+
}
4454

4555
@Inject
4656
protected abstract FileSystemOperations getFs();
4757

4858
@TaskAction
4959
public void performAction(InputChanges inputs) throws Exception {
50-
getTaskService().get().registerSourceAlreadyRan(this);
60+
getTakService().get().registerSourceAlreadyRan(this);
5161
if (target == null) {
5262
throw new GradleException("You must specify 'Iterable<File> target'");
5363
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.assertj.core.api.Assertions;
2828
import org.gradle.api.Project;
29+
import org.gradle.api.provider.Provider;
2930
import org.gradle.api.services.BuildServiceParameters;
3031
import org.junit.jupiter.api.Test;
3132

@@ -41,12 +42,12 @@ class DiffMessageFormatterTest extends ResourceHarness {
4142

4243
private class Bundle {
4344
Project project = TestProvisioner.gradleProject(rootFolder());
44-
SpotlessTaskService taskService = new SpotlessTaskService() {
45+
Provider<SpotlessTaskService> taskService = GradleIntegrationHarness.providerOf(new SpotlessTaskService() {
4546
@Override
4647
public BuildServiceParameters.None getParameters() {
4748
return null;
4849
}
49-
};
50+
});
5051

5152
File file;
5253
SpotlessTaskImpl task;
@@ -61,7 +62,7 @@ public BuildServiceParameters.None getParameters() {
6162

6263
private SpotlessTaskImpl createFormatTask(String name) {
6364
SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class);
64-
task.getTaskService().set(taskService);
65+
task.init(taskService);
6566
task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
6667
task.setTarget(Collections.singletonList(file));
6768
return task;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ void createTask() {
3636
Project project = TestProvisioner.gradleProject(rootFolder());
3737
spotlessTask = project.getTasks().create("spotlessTaskUnderTest", SpotlessTaskImpl.class);
3838
spotlessTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
39-
spotlessTask.getTaskService().set(new SpotlessTaskService() {
39+
spotlessTask.init(GradleIntegrationHarness.providerOf(new SpotlessTaskService() {
4040
@Override
4141
public BuildServiceParameters.None getParameters() {
4242
return null;
4343
}
44-
});
44+
}));
4545
}
4646

4747
@Test

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Predicate;
2525
import java.util.stream.Collectors;
2626

27+
import org.gradle.api.provider.Provider;
2728
import org.gradle.testkit.runner.BuildResult;
2829
import org.gradle.testkit.runner.BuildTask;
2930
import org.gradle.testkit.runner.GradleRunner;
@@ -59,6 +60,10 @@ public enum GradleVersionSupport {
5960
}
6061
}
6162

63+
public static <T> Provider<T> providerOf(T value) {
64+
return org.gradle.api.internal.provider.Providers.of(value);
65+
}
66+
6267
/**
6368
* Each test gets its own temp folder, and we create a gradle
6469
* build there and run it.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collections;
2525

2626
import org.gradle.api.Project;
27+
import org.gradle.api.provider.Provider;
2728
import org.gradle.api.services.BuildServiceParameters;
2829
import org.junit.jupiter.api.Assertions;
2930
import org.junit.jupiter.api.Test;
@@ -40,12 +41,12 @@ class PaddedCellTaskTest extends ResourceHarness {
4041
private class Bundle {
4142
String name;
4243
Project project = TestProvisioner.gradleProject(rootFolder());
43-
SpotlessTaskService taskService = new SpotlessTaskService() {
44+
Provider<SpotlessTaskService> taskService = GradleIntegrationHarness.providerOf(new SpotlessTaskService() {
4445
@Override
4546
public BuildServiceParameters.None getParameters() {
4647
return null;
4748
}
48-
};
49+
});
4950
File file;
5051
File outputFile;
5152
SpotlessTaskImpl source;
@@ -64,7 +65,7 @@ public BuildServiceParameters.None getParameters() {
6465

6566
private SpotlessTaskImpl createFormatTask(String name, FormatterStep step) {
6667
SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class);
67-
task.getTaskService().set(taskService);
68+
task.init(taskService);
6869
task.addStep(step);
6970
task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
7071
task.setTarget(Collections.singletonList(file));

0 commit comments

Comments
 (0)