Skip to content

Commit

Permalink
Prefer using plugin extensions over deprecated conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Mar 11, 2023
1 parent 7477651 commit da0f09b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,8 +27,11 @@
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.plugins.GroovyBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.GroovySourceDirectorySet;
import org.gradle.api.tasks.GroovySourceSet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.util.GradleVersion;

import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
Expand Down Expand Up @@ -105,22 +108,43 @@ public void configFile(Object... configFiles) {
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
JavaPluginConvention convention = getProject().getConvention().getPlugin(JavaPluginConvention.class);
if (convention == null || !getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) {
throw new GradleException("You must apply the groovy plugin before the spotless plugin if you are using the groovy extension.");
}
//Add all Groovy files (may contain Java files as well)

FileCollection union = getProject().files();
for (SourceSet sourceSet : convention.getSourceSets()) {
GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class);
if (excludeJava) {
union = union.plus(groovySourceSet.getAllGroovy());
} else {
union = union.plus(groovySourceSet.getGroovy());
if (GradleVersion.current().compareTo(GradleVersion.version("7.1")) >= 0) {
JavaPluginExtension javaPluginExtension = getProject().getExtensions().findByType(JavaPluginExtension.class);
if (javaPluginExtension == null || !getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) {
throw new GradleException("You must apply the groovy plugin before the spotless plugin if you are using the groovy extension.");
}
//Add all Groovy files (may contain Java files as well)

FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPluginExtension.getSourceSets()) {
union = union.plus(sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class).filter(file -> {
String name = file.getName();
if (excludeJava) {
return name.endsWith(".groovy");
} else {
return name.endsWith(".groovy") || name.endsWith(".java");
}
}));
}
target = union;
} else {
JavaPluginConvention convention = getProject().getConvention().getPlugin(JavaPluginConvention.class);
if (convention == null || !getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) {
throw new GradleException("You must apply the groovy plugin before the spotless plugin if you are using the groovy extension.");
}
//Add all Groovy files (may contain Java files as well)

FileCollection union = getProject().files();
for (SourceSet sourceSet : convention.getSourceSets()) {
GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class);
if (excludeJava) {
union = union.plus(groovySourceSet.getAllGroovy());
} else {
union = union.plus(groovySourceSet.getGroovy());
}
}
target = union;
}
target = union;
} else if (excludeJava) {
throw new IllegalArgumentException("'excludeJava' is not supported in combination with a custom 'target'.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.util.GradleVersion;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
Expand Down Expand Up @@ -359,15 +361,27 @@ private FormatterStep createStep() {
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'java' plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllJava());
if (GradleVersion.current().compareTo(GradleVersion.version("7.1")) >= 0) {
JavaPluginExtension javaPluginExtension = getProject().getExtensions().findByType(JavaPluginExtension.class);
if (javaPluginExtension == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'java' plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPluginExtension.getSourceSets()) {
union = union.plus(sourceSet.getAllJava());
}
target = union;
} else {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'java' plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllJava());
}
target = union;
}
target = union;
}

steps.replaceAll(step -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.util.GradleVersion;

import com.diffplug.common.collect.ImmutableSortedMap;
import com.diffplug.spotless.FileSignature;
Expand Down Expand Up @@ -230,18 +232,33 @@ private FormatterStep createStep() {
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".kt") || name.endsWith(".kts");
}));
if (GradleVersion.current().compareTo(GradleVersion.version("7.1")) >= 0) {
JavaPluginExtension javaPluginExtension = getProject().getExtensions().findByType(JavaPluginExtension.class);
if (javaPluginExtension == null) {
throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPluginExtension.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".kt") || name.endsWith(".kts");
}));
}
target = union;
} else {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".kt") || name.endsWith(".kts");
}));
}
target = union;
}
target = union;
}
super.setupTask(task);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,9 @@
import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.util.GradleVersion;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.scala.ScalaFmtStep;
Expand Down Expand Up @@ -79,18 +81,33 @@ private FormatterStep createStep() {
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin.");
if (GradleVersion.current().compareTo(GradleVersion.version("7.1")) >= 0) {
JavaPluginExtension javaPluginExtension = getProject().getExtensions().findByType(JavaPluginExtension.class);
if (javaPluginExtension == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPluginExtension.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".scala") || name.endsWith(".sc");
}));
}
target = union;
} else {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".scala") || name.endsWith(".sc");
}));
}
target = union;
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".scala") || name.endsWith(".sc");
}));
}
target = union;
}
super.setupTask(task);
}
Expand Down

0 comments on commit da0f09b

Please sign in to comment.