Skip to content

Commit

Permalink
Add ability to add custom header definitions (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarpall authored and hierynomus committed Mar 21, 2017
1 parent a0ed859 commit 0e6514c
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 39 deletions.
25 changes: 25 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Here is a general overview of the options:
|excludes(Collection<String> patterns) |Add ANT style patterns to exclude files from license absence reporting and license application
|include(String pattern) |Add an ANT style pattern to include files into license absence reporting and license application
|includes(Collection<String> patterns) |Add ANT style patterns to include files into license absence reporting and license application
|headerDefinition(HeaderDefinitionBuilder headerDefinition) |Add a custom header definition that will be added to the defaults.
|headerDefinitions(Closure) | Add a custom header definition that will be added to the defaults.
|====

=== Header Locations
Expand Down Expand Up @@ -199,6 +201,29 @@ license {
licenseMain.ext.year = 2012
----

=== Creating your own header definition
In some cases the default header definitions can not be used for a specific project
so the ability to specify your own is beneficial.

Adding a new header definition is done through the license extension

[source,groovy]
----
license {
headerDefinitions {
custom_definition {
firstLine = "//"
endLine = "//"
firstLineDetectionPattern = "//"
lastLineDetectionPattern = "//"
allowBlankLines = false
skipLinePattern = "//"
isMultiline = false
}
}
}
----

=== Include/Exclude files from license absence reporting and license application
By default all files in the sourceSets configured are required to carry a license. Just like with Gradle `SourceSet` you can use include/exclude patterns to control this behaviour.

Expand Down
21 changes: 18 additions & 3 deletions src/main/groovy/nl/javadude/gradle/plugins/license/License.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

package nl.javadude.gradle.plugins.license

import com.google.code.mojo.license.header.HeaderDefinition
import nl.javadude.gradle.plugins.license.header.HeaderDefinitionBuilder
import nl.javadude.gradle.plugins.license.maven.AbstractLicenseMojo
import nl.javadude.gradle.plugins.license.maven.CallbackWithFailure
import nl.javadude.gradle.plugins.license.maven.LicenseCheckMojo
import nl.javadude.gradle.plugins.license.maven.LicenseFormatMojo
import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.*

/**
* Task to back License. Using convention of naming Task types with just their name, which makes calls
* like tasks.withType(License) nicer, consistent with most internal Gradle tasks.
Expand Down Expand Up @@ -73,7 +75,7 @@ public class License extends SourceTask implements VerificationTask {
File header

/**
* In leiu of a header file on the local filesystem, this property lets you provide a URL that could be
* In lieu of a header file on the local filesystem, this property lets you provide a URL that could be
* in the classpath or on a remote system. When configured across a few projects, it would mean that a
* header file doesn't have to be in each project.
*/
Expand All @@ -90,6 +92,9 @@ public class License extends SourceTask implements VerificationTask {
Map<String, String> inheritedProperties;
Map<String, String> inheritedMappings;

// Container for all custom header definitions
NamedDomainObjectContainer<HeaderDefinitionBuilder> headerDefinitions;

@TaskAction
protected void process() {
// Plain weird, but this ensures that the lazy closure from the extension is properly wired into the excludes field of the SourceTask.
Expand All @@ -112,7 +117,7 @@ public class License extends SourceTask implements VerificationTask {

URI uri = getURI()

new AbstractLicenseMojo(validHeaders, getProject().rootDir, initial, isDryRun(), isSkipExistingHeaders(), isUseDefaultMappings(), isStrictCheck(), uri, source, combinedMappings, getEncoding())
new AbstractLicenseMojo(validHeaders, getProject().rootDir, initial, isDryRun(), isSkipExistingHeaders(), isUseDefaultMappings(), isStrictCheck(), uri, source, combinedMappings, getEncoding(), buildHeaderDefinitions())
.execute(callback);

altered = callback.getAffected()
Expand Down Expand Up @@ -164,6 +169,16 @@ public class License extends SourceTask implements VerificationTask {
return combinedMappings
}

List<HeaderDefinition> buildHeaderDefinitions() {
List<HeaderDefinition> definitions = new ArrayList<>();
getHeaderDefinitions().all { headerDefinition ->
logger.debug("Adding extra header definition ${headerDefinition.toString()}")
definitions.add(headerDefinition.build())
}

return definitions
}

Map<String, String> internalMappings = new HashMap<String, String>();
public void mapping(String fileType, String headerType) {
internalMappings.put(fileType, headerType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

package nl.javadude.gradle.plugins.license

import nl.javadude.gradle.plugins.license.header.HeaderDefinitionBuilder
import org.gradle.api.DomainObjectCollection
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.tasks.SourceSet

/**
* Extension in the license namespace, which drives the License tasks.
*
Expand Down Expand Up @@ -74,6 +75,11 @@ class LicenseExtension {

boolean strictCheck

/**
* Additional header definitions
*/
NamedDomainObjectContainer<HeaderDefinitionBuilder> headerDefinitions

/**
* The encoding used for opening files. It is the system encoding by default
*/
Expand Down Expand Up @@ -112,4 +118,11 @@ class LicenseExtension {
includePatterns.addAll(patterns)
}

def headerDefinitions(final Closure configureHeaderDefinition) {
headerDefinitions.configure(configureHeaderDefinition);
}

void headerDefinition(HeaderDefinitionBuilder definition) {
headerDefinitions.add(definition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package nl.javadude.gradle.plugins.license

import nl.javadude.gradle.plugins.license.header.HeaderDefinitionBuilder
import org.gradle.api.Action
import org.gradle.api.DomainObjectCollection
import org.gradle.api.Plugin
Expand Down Expand Up @@ -107,6 +108,7 @@ class LicensePlugin implements Plugin<Project> {
conventionMapping.with {
sourceSets = { [] as DomainObjectCollection<SourceSet> }
}
headerDefinitions = project.container(HeaderDefinitionBuilder)
}


Expand Down Expand Up @@ -217,6 +219,7 @@ class LicensePlugin implements Plugin<Project> {
excludes = { extension.excludePatterns }
includes = { extension.includePatterns }
encoding = { extension.encoding }
headerDefinitions = { extension.headerDefinitions }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package nl.javadude.gradle.plugins.license.header

import com.google.code.mojo.license.header.HeaderDefinition
import org.gradle.api.Named

class HeaderDefinitionBuilder implements Named {
String type
String firstLine
String beforeEachLine
String endLine
boolean allowBlankLines = false

String skipLinePattern
String firstLineDetectionPattern
String lastLineDetectionPattern
boolean isMultiline = false

static HeaderDefinitionBuilder headerDefinition(String name) {
return new HeaderDefinitionBuilder(name)
}

HeaderDefinitionBuilder(String type) {
this.type = type
}

HeaderDefinitionBuilder withFirstLine(String firstLine) {
this.firstLine = firstLine
return this
}

HeaderDefinitionBuilder withBeforeEachLine(String beforeEachLine) {
this.beforeEachLine = beforeEachLine
return this
}

HeaderDefinitionBuilder withEndLine(String endLine) {
this.endLine = endLine
return this
}

HeaderDefinitionBuilder withNoBlankLines() {
this.allowBlankLines = false
return this
}

HeaderDefinitionBuilder withBlankLines() {
this.allowBlankLines = true
return this
}

HeaderDefinitionBuilder withSkipLinePattern(String skipLinePattern) {
this.skipLinePattern = skipLinePattern
return this
}

HeaderDefinitionBuilder withFirstLineDetectionDetectionPattern(String firstLineDetectionPattern) {
this.firstLineDetectionPattern = firstLineDetectionPattern
return this
}

HeaderDefinitionBuilder withLastLineDetectionDetectionPattern(String lastLineDetectionPattern) {
this.lastLineDetectionPattern = lastLineDetectionPattern
return this
}

HeaderDefinitionBuilder multiline() {
this.isMultiline = true
return this
}

HeaderDefinitionBuilder noMultiLine() {
this.isMultiline = false
return this
}

HeaderDefinition build() {
return new HeaderDefinition(type,
firstLine,
beforeEachLine,
endLine,
skipLinePattern,
firstLineDetectionPattern,
lastLineDetectionPattern,
allowBlankLines,
isMultiline)
}

@Override
String toString() {
return "{" +
"type='" + type + '\'' +
", firstLine='" + firstLine + '\'' +
", beforeEachLine='" + beforeEachLine + '\'' +
", endLine='" + endLine + '\'' +
", allowBlankLines=" + allowBlankLines +
", skipLinePattern='" + skipLinePattern + '\'' +
", firstLineDetectionPattern='" + firstLineDetectionPattern + '\'' +
", lastLineDetectionPattern='" + lastLineDetectionPattern + '\'' +
", isMultiline=" + isMultiline +
'}'
}

@Override
String getName() {
return type
}
}
Loading

0 comments on commit 0e6514c

Please sign in to comment.