diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index e95baf2ed9..185e5df421 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -70,10 +70,7 @@ spotless { // Everything before the first #include or #pragma will // be replaced with whatever is in `spotless.license.cpp` - licenseHeader { - headerFile 'spotless.license.cpp' - delimiter '#' - } + licenseHeaderFile 'spotless.license.cpp', '#' } } ``` @@ -314,18 +311,14 @@ The `licenseHeader` and `licenseHeaderFile` steps will generate license headers `/* Licensed under Apache-2.0 1990-2003. */` * the `$YEAR` token is otherwise missing -The separator for the year range defaults to the hyphen character, e.g `1990-2003`, but can be customized with the `yearSeparator` property using the block configuration syntax. +The separator for the year range defaults to the hyphen character, e.g `1990-2003`, but can be customized with the `yearSeparator` property. + For instance, the following configuration treats `1990, 2003` as a valid year range. ```gradle spotless { format java { - licenseHeader { - header 'Licensed under Apache-2.0 $YEAR' - headerFile 'path-to-file' // header and headerFile are exclusive - yearSeparator ', ' - delimiter // Defaults to 'package' for Java, but can be customized here - } + licenseHeader(''Licensed under Apache-2.0 $YEAR').yearSeparator(', ') } } ``` diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 86c1681b8c..6bb763c8f6 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -31,11 +31,9 @@ import javax.annotation.Nullable; -import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.file.FileCollection; -import org.gradle.util.ConfigureUtil; import com.diffplug.spotless.FormatExceptionPolicyStrict; import com.diffplug.spotless.FormatterFunc; @@ -347,93 +345,40 @@ public void indentWithTabs() { indentWithTabs(4); } - LicenseHeaderExtension licenseHeaderExtension; + LicenseHeaderConfig licenseHeaderConfig; /** - * Syntactic sugar which allows - * - * - * licenseHeader { - * header 'header-string' - * delimiter 'package' - * } - * - * - * to be written as - * - * licenseHeader 'header-string', 'package' - * - * - * Moreover language specific extensions can overload this method to pre-define the delimiter, - * allowing the even simpler - * - * - * licenseHeader 'header-string' - * - * * @param licenseHeader * Content that should be at the top of every file. * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void licenseHeader(String licenseHeader, String delimiter) { - licenseHeader(extension -> extension.header(licenseHeader), delimiter); + public LicenseHeaderConfig licenseHeader(String licenseHeader, String delimiter) { + return getOrCreateLicenseHeader(delimiter).header(licenseHeader); } /** - * Syntactic sugar which allows - * - * - * licenseHeaderFile { - * headerFile 'path-to-file' - * delimiter 'package' - * } - * - * - * to be written as - * - * - * licenseHeaderFile 'path-to-file', 'package' - * - * - * Moreover language specific extensions can overload this method to pre-define the delimiter, - * allowing the even simpler - * - * - * licenseHeaderFile 'path-to-file' - * - * * @param licenseHeaderFile * Content that should be at the top of every file. * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void licenseHeaderFile(Object licenseHeaderFile, String delimiter) { - licenseHeader(extension -> extension.headerFile(licenseHeaderFile), delimiter); - } - - /** - * Configures the license header extension. - * - * @param delimiter - * Spotless will look for a line that starts with this to know what the "top" is. - */ - public void licenseHeader(Closure closure, String delimiter) { - licenseHeader(ConfigureUtil.configureUsing(closure), delimiter); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile, String delimiter) { + return getOrCreateLicenseHeader(delimiter).headerFile(licenseHeaderFile); } /** - * Configures the license header extension. + * Get or create the license header configuration. * * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void licenseHeader(Action closure, String delimiter) { - requireNonNull(closure); - if (licenseHeaderExtension == null) { - licenseHeaderExtension = new LicenseHeaderExtension(this, delimiter); + private LicenseHeaderConfig getOrCreateLicenseHeader(String delimiter) { + requireNonNull(delimiter); + if (licenseHeaderConfig == null) { + licenseHeaderConfig = new LicenseHeaderConfig(delimiter); } - closure.execute(licenseHeaderExtension); + return licenseHeaderConfig; } /** Sets up a format task according to the values in this extension. */ @@ -443,8 +388,8 @@ protected void setupTask(SpotlessTask task) { task.setExceptionPolicy(exceptionPolicy); task.setTarget(target); task.setSteps(steps); - if (licenseHeaderExtension != null) { - licenseHeaderExtension.setupTask(task); + if (licenseHeaderConfig != null) { + licenseHeaderConfig.setupTask(this, task); } task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> task.target)); } 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 7a08c0616b..f297e901d5 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 @@ -36,8 +36,6 @@ import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.java.ImportOrderStep; -import groovy.lang.Closure; - public class GroovyExtension extends FormatExtension { static final String NAME = "groovy"; @@ -57,16 +55,12 @@ public void excludeJava(boolean excludeJava) { this.excludeJava = excludeJava; } - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, JavaExtension.LICENSE_HEADER_DELIMITER); - } - - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, JavaExtension.LICENSE_HEADER_DELIMITER); } - public void licenseHeader(Closure closure) { - licenseHeader(closure, JavaExtension.LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER); } /** Method interface has been changed to 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 f92c8a6956..a3873d3c88 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 @@ -35,8 +35,6 @@ import com.diffplug.spotless.java.ImportOrderStep; import com.diffplug.spotless.java.RemoveUnusedImportsStep; -import groovy.lang.Closure; - public class JavaExtension extends FormatExtension { static final String NAME = "java"; @@ -48,16 +46,12 @@ public JavaExtension(SpotlessExtension rootExtension) { // testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java as well static final String LICENSE_HEADER_DELIMITER = "package "; - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); - } - - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); } - public void licenseHeader(Closure closure) { - licenseHeader(closure, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); } /** Method interface has been changed to 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 54b9a3b627..45a81a2630 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 @@ -24,8 +24,6 @@ import com.diffplug.spotless.kotlin.KtLintStep; -import groovy.lang.Closure; - public class KotlinExtension extends FormatExtension { // '^' is prepended to the regex in LICENSE_HEADER_DELIMITER later in FormatExtension.licenseHeader(String, String) private static final String LICENSE_HEADER_DELIMITER = "(package |@file)"; @@ -35,16 +33,12 @@ public KotlinExtension(SpotlessExtension rootExtension) { super(rootExtension); } - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); - } - - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); } - public void licenseHeader(Closure closure) { - licenseHeader(closure, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); } /** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */ diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderConfig.java similarity index 76% rename from plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderExtension.java rename to plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderConfig.java index ae2e90bd66..f6c83eb614 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/LicenseHeaderConfig.java @@ -21,19 +21,16 @@ import com.diffplug.spotless.generic.LicenseHeaderStep; -public class LicenseHeaderExtension { - - final FormatExtension parent; +public class LicenseHeaderConfig { String header; Object headerFile; String delimiter; static final String DEFAULT_LICENSE_YEAR_DELIMITER = "-"; - String licenceyearSeparator = DEFAULT_LICENSE_YEAR_DELIMITER; + String yearSeparator = DEFAULT_LICENSE_YEAR_DELIMITER; - public LicenseHeaderExtension(FormatExtension parent, String delimiter) { - this.parent = Objects.requireNonNull(parent); + public LicenseHeaderConfig(String delimiter) { this.delimiter = Objects.requireNonNull(delimiter, "delimiter"); } @@ -41,46 +38,50 @@ public LicenseHeaderExtension(FormatExtension parent, String delimiter) { * @param header * Content that should be at the top of every file. */ - public void header(String header) { + public LicenseHeaderConfig header(String header) { if (headerFile != null) { throw new GradleException("cannot set both licenseHeader and licenseHeaderFile"); } this.header = header; + return this; } /** * @param headerFile * Content that should be at the top of every file. */ - public void headerFile(Object headerFile) { + public LicenseHeaderConfig headerFile(Object headerFile) { if (header != null) { throw new GradleException("cannot set both header and headerFile"); } this.headerFile = Objects.requireNonNull(headerFile, "headerFile"); + return this; } /** * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void delimiter(String delimiter) { + public LicenseHeaderConfig delimiter(String delimiter) { this.delimiter = Objects.requireNonNull(delimiter, "delimiter"); + return this; } /** * @param yearSeparator * The characters used to separate the first and last years in multi years patterns. */ - public void yearSeparator(String yearSeparator) { - this.licenceyearSeparator = Objects.requireNonNull(yearSeparator, "yearSeparator"); + public LicenseHeaderConfig yearSeparator(String yearSeparator) { + this.yearSeparator = Objects.requireNonNull(yearSeparator, "yearSeparator"); + return this; } - public void setupTask(SpotlessTask task) { + void setupTask(FormatExtension parent, SpotlessTask task) { if (headerFile != null) { parent.addStep(LicenseHeaderStep.createFromFile(parent.getProject().file(headerFile), parent.getEncoding(), delimiter, - licenceyearSeparator)); + yearSeparator)); } else if (header != null) { - parent.addStep(LicenseHeaderStep.createFromHeader(header, delimiter, licenceyearSeparator)); + parent.addStep(LicenseHeaderStep.createFromHeader(header, delimiter, yearSeparator)); } } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index ab90367748..547d151810 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -102,7 +102,7 @@ public void testWithCustomHeaderSeparator() throws IOException { } @Test - public void testWithHeaderExtension() throws IOException { + public void testWithYearSeparator() throws IOException { write("build.gradle", "plugins {", " id 'nebula.kotlin' version '1.0.6'", @@ -111,11 +111,7 @@ public void testWithHeaderExtension() throws IOException { "repositories { mavenCentral() }", "spotless {", " kotlin {", - " licenseHeader {", - " licenseHeader('" + HEADER + "')", - " delimiter('@file')", - " yearSeparator('-')", - " }", + " licenseHeader('" + HEADER + "').yearSeparator('-')", " ktlint()", " }", "}");