Skip to content

Commit

Permalink
Add experimental Android support
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-gh committed May 7, 2016
1 parent 407e8a2 commit 19a6a2d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = 'net.minecrell'
version = '0.1.6-SNAPSHOT'
version = '0.2-SNAPSHOT'
description = 'A simple license header manager for Gradle'

sourceCompatibility = '1.6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.minecrell.gradle.licenser

import net.minecrell.gradle.licenser.header.HeaderFormatRegistry
import org.gradle.api.Incubating
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.api.tasks.util.PatternSet
Expand Down Expand Up @@ -60,6 +61,15 @@ class LicenseExtension implements PatternFilterable {
*/
Collection<SourceSet> sourceSets

/**
* The Android source sets to scan for files with license headers.
* By default this includes all source sets of the project.
*
* <p><b>Note:</b> This is only used when the Android Gradle plugin is applied.</p>
*/
@Incubating
Collection androidSourceSets

/**
* The charset to read/write the files with.
* By default this is {@code UTF-8}.
Expand Down
30 changes: 27 additions & 3 deletions src/main/groovy/net/minecrell/gradle/licenser/Licenser.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ import net.minecrell.gradle.licenser.tasks.LicenseTask
import net.minecrell.gradle.licenser.tasks.LicenseUpdate
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.SourceSet

class Licenser implements Plugin<Project> {

private static final String CHECK_TASK = 'checkLicense'
private static final String FORMAT_TASK = 'updateLicense'
private static final String ANDROID_TASK = 'Android'

private Project project
private LicenseExtension extension
Expand All @@ -54,6 +56,12 @@ class Licenser implements Plugin<Project> {
extension.sourceSets = project.sourceSets
}

['com.android.library', 'com.android.application'].each {
plugins.withId(it) {
extension.androidSourceSets = android.sourceSets
}
}

def globalCheck = task(CHECK_TASK + 's')
task('licenseCheck', dependsOn: globalCheck)
def globalFormat = task(FORMAT_TASK + 's')
Expand Down Expand Up @@ -84,10 +92,17 @@ class Licenser implements Plugin<Project> {
}, extension.newLine)

extension.sourceSets.each {
def check = createTask(it.getTaskName(CHECK_TASK, null), LicenseCheck, header, it)
def check = createTask(CHECK_TASK, LicenseCheck, header, it)
check.ignoreFailures = extension.ignoreFailures
globalCheck.dependsOn check
globalFormat.dependsOn createTask(it.getTaskName(FORMAT_TASK, null), LicenseUpdate, header, it)
globalFormat.dependsOn createTask(FORMAT_TASK, LicenseUpdate, header, it)
}

extension.androidSourceSets.each {
def check = createAndroidTask(CHECK_TASK, LicenseCheck, header, it)
check.ignoreFailures = extension.ignoreFailures
globalCheck.dependsOn check
globalFormat.dependsOn createAndroidTask(FORMAT_TASK, LicenseUpdate, header, it)
}
}
}
Expand All @@ -102,9 +117,18 @@ class Licenser implements Plugin<Project> {
}

private <T extends LicenseTask> T createTask(String name, Class<T> type, Header expectedHeader, SourceSet sourceSet) {
return makeTask(sourceSet.getTaskName(name, null), type, expectedHeader, sourceSet.allSource)
}

private <T extends LicenseTask> T createAndroidTask(String name, Class<T> type, Header expectedHeader, Object sourceSet) {
return makeTask(name + ANDROID_TASK + sourceSet.name.capitalize(), type, expectedHeader,
project.files(sourceSet.java.sourceFiles, sourceSet.res.sourceFiles))
}

private <T extends LicenseTask> T makeTask(String name, Class<T> type, Header expectedHeader, FileCollection files) {
return (T) project.task(name, type: type) { T task ->
task.header = expectedHeader
task.files = sourceSet.allSource
task.files = files
task.filter = extension.filter
task.charset = extension.charset
}
Expand Down

0 comments on commit 19a6a2d

Please sign in to comment.