Skip to content

Commit

Permalink
Move the FILES_PROPERTY support into the task creation point, so th…
Browse files Browse the repository at this point in the history
…at we can avoid `getTasks().withType(SpotlessTask.class`. Allegedly `withType` triggers task configuration, although our test indicates that it doesn't at least in 4.9. Better to use the recommended API regardless, especially since we can do so without breaking 2.x support.
  • Loading branch information
nedtwigg committed Sep 30, 2019
1 parent c4c226e commit e97a580
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class SpotlessExtension {
private static final String CHECK_DESCRIPTION = "Checks that sourcecode satisfies formatting steps.";
private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place.";

private static final String FILES_PROPERTY = "spotlessFiles";

public SpotlessExtension(Project project) {
this.project = requireNonNull(project);
rootCheckTask = project.task(EXTENSION + CHECK);
Expand Down Expand Up @@ -268,6 +270,18 @@ public Object doCall(TaskExecutionGraph graph) {
}
});

// set the filePatterns property
project.afterEvaluate(unused -> {
String filePatterns;
if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) {
filePatterns = (String) project.property(FILES_PROPERTY);
} else {
// needs to be non-null since it is an @Input property of the task
filePatterns = "";
}
spotlessTask.setFilePatterns(filePatterns);
});

// the root tasks depend on the control tasks
rootCheckTask.dependsOn(checkTask);
rootApplyTask.dependsOn(applyTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
public class SpotlessPlugin implements Plugin<Project> {
SpotlessExtension spotlessExtension;

private static final String FILES_PROPERTY = "spotlessFiles";

@Override
public void apply(Project project) {
// make sure there's a `clean` task
Expand All @@ -41,16 +39,6 @@ public void apply(Project project) {
clean.doLast(unused -> SpotlessCache.clear());

project.afterEvaluate(unused -> {
// set the filePatterns property
String filePatterns;
if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) {
filePatterns = (String) project.property(FILES_PROPERTY);
} else {
// needs to be non-null since it is an @Input property of the task
filePatterns = "";
}
project.getTasks().withType(SpotlessTask.class, task -> task.setFilePatterns(filePatterns));

// Add our check task as a dependency on the global check task
// getTasks() returns a "live" collection, so this works even if the
// task doesn't exist at the time this call is made
Expand Down

0 comments on commit e97a580

Please sign in to comment.