Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a common interface for the license header #235

Merged
merged 4 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222))
* Updated default ktlint from 0.14.0 to 0.21.0
* Add ability to pass custom options to ktlint. See README for details.
* Added interface `HasBuiltinDelimiterForLicense` to language extensions that have pre-defined licence header delimiter. ([#235](https://github.com/diffplug/spotless/pull/235))

### Version 3.10.0 - February 15th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.10.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.10.0))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.diffplug.spotless.generic.LicenseHeaderStep;
import com.diffplug.spotless.java.ImportOrderStep;

public class GroovyExtension extends FormatExtension {
public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "groovy";

public GroovyExtension(SpotlessExtension rootExtension) {
Expand All @@ -55,10 +55,12 @@ public void excludeJava(boolean excludeJava) {
this.excludeJava = excludeJava;
}

@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, JavaExtension.LICENSE_HEADER_DELIMITER);
}

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

/**
* Every {@link FormatExtension} has a method
* {@link FormatExtension#licenseHeader(String, String) license(licenseContent, licenseDelimiter)},
* where licenseDelimiter is a regex that separates the license part of the code from the content.
* For some kinds of format -
* such as {@link JavaExtension java}, {@link KotlinExtension kotlin}, and {@link GroovyExtension groovy} -
* we already have a defined delimiter, so users don't have to provide it.
* By having the java, kotlin, and groovy formats implement this interface,
* you can write generic code for enforcing whitespace and licenses.
*/
public interface HasBuiltinDelimiterForLicense {

/**
* @param licenseHeader
* Content that should be at the top of every file.
*/
FormatExtension.LicenseHeaderConfig licenseHeader(String licenseHeader);

/**
* @param licenseHeaderFile
* Content that should be at the top of every file.
*/
FormatExtension.LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;

public class JavaExtension extends FormatExtension {
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "java";

public JavaExtension(SpotlessExtension rootExtension) {
Expand All @@ -46,10 +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 ";

@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER);
}

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.kotlin.KtLintStep;

public class KotlinExtension extends FormatExtension {
public class KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "kotlin";

public KotlinExtension(SpotlessExtension rootExtension) {
super(rootExtension);
}

@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER);
}

@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

import java.io.IOException;

import org.junit.Test;

public class HasBuiltinDelimiterForLicenseTest extends GradleIntegrationTest {

@Test
public void testWithCommonInterfaceForConfiguringLicences() throws IOException {
// TODO: JLL Convert this to a Kotlin example when supported: https://github.com/gradle/kotlin-dsl/issues/492
setFile("build.gradle").toLines(
"import com.diffplug.gradle.spotless.HasBuiltinDelimiterForLicense",
"plugins {",
" id(\"org.jetbrains.kotlin.jvm\") version \"1.2.31\"",
" id(\"com.diffplug.gradle.spotless\")",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
" java {",
" assert (it instanceof HasBuiltinDelimiterForLicense) : \"Was `$it`\"",
" }",
"}");
gradleRunner()
.withGradleVersion("4.6")
.withArguments("spotlessApply")
.forwardOutput()
.build();
}
}