Skip to content

Commit

Permalink
Using a config object instead of extension
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalkaspi-delphix committed Feb 7, 2018
1 parent 1d2a827 commit bd38e49
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 129 deletions.
15 changes: 4 additions & 11 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', '#'
}
}
```
Expand Down Expand Up @@ -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(', ')
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -347,93 +345,40 @@ public void indentWithTabs() {
indentWithTabs(4);
}

LicenseHeaderExtension licenseHeaderExtension;
LicenseHeaderConfig licenseHeaderConfig;

/**
* Syntactic sugar which allows
*
* <code>
* licenseHeader {
* header 'header-string'
* delimiter 'package'
* }
* </code>
*
* to be written as
* <code>
* licenseHeader 'header-string', 'package'
* </code>
*
* Moreover language specific extensions can overload this method to pre-define the delimiter,
* allowing the even simpler
*
* <code>
* licenseHeader 'header-string'
* </code>
*
* @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
*
* <code>
* licenseHeaderFile {
* headerFile 'path-to-file'
* delimiter 'package'
* }
* </code>
*
* to be written as
*
* <code>
* licenseHeaderFile 'path-to-file', 'package'
* </code>
*
* Moreover language specific extensions can overload this method to pre-define the delimiter,
* allowing the even simpler
*
* <code>
* licenseHeaderFile 'path-to-file'
* </code>
*
* @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<LicenseHeaderExtension> 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. */
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand All @@ -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). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,67 @@

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");
}

/**
* @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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand All @@ -111,11 +111,7 @@ public void testWithHeaderExtension() throws IOException {
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" licenseHeader {",
" licenseHeader('" + HEADER + "')",
" delimiter('@file')",
" yearSeparator('-')",
" }",
" licenseHeader('" + HEADER + "').yearSeparator('-')",
" ktlint()",
" }",
"}");
Expand Down

0 comments on commit bd38e49

Please sign in to comment.