From da0f09b62ba9da60c4c27f63a886803630c027ee Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 11 Mar 2023 19:01:09 +0800 Subject: [PATCH] Prefer using plugin extensions over deprecated conventions https://docs.gradle.org/current/userguide/upgrading_version_7.html#all_convention_deprecation --- .../gradle/spotless/GroovyExtension.java | 54 +++++++++++++------ .../gradle/spotless/JavaExtension.java | 30 ++++++++--- .../gradle/spotless/KotlinExtension.java | 39 ++++++++++---- .../gradle/spotless/ScalaExtension.java | 41 +++++++++----- 4 files changed, 118 insertions(+), 46 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java index 5e528e1b79..40aba2560e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java @@ -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. @@ -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; @@ -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'."); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index bfe1a96955..1ebb3ef105 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -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; @@ -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 -> { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 5a7419f68d..f6cab3c28b 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -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; @@ -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); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java index 8a8765b539..9d710a1ba9 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java @@ -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. @@ -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; @@ -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); }