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

Make use of Gradle Task Configuration Avoidance for registering tasks #210

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskProvider

class LicenseBasePlugin implements Plugin<Project> {

Expand Down Expand Up @@ -96,7 +97,7 @@ class LicenseBasePlugin implements Plugin<Project> {
* @param task
*/
protected void configureTaskRule() {
project.tasks.withType(License) { License task ->
project.tasks.withType(License).configureEach { License task ->
logger.info("Applying license defaults to task: ${task.path}");
configureTaskDefaults(task)
}
Expand Down Expand Up @@ -140,27 +141,27 @@ class LicenseBasePlugin implements Plugin<Project> {
def sourceSetTaskName = "${LICENSE_TASK_BASE_NAME}${taskInfix}${sourceSet.name.capitalize()}"
logger.info("Adding ${sourceSetTaskName} task for sourceSet ${sourceSet.name}");

License checkTask = project.tasks.create(sourceSetTaskName, LicenseCheck)
TaskProvider<LicenseCheck> checkTask = project.tasks.register(sourceSetTaskName, LicenseCheck)
configureForSourceSet(sourceSet, checkTask, sourceSetSources)

// Add independent license task, which will perform format
def sourceSetFormatTaskName = "${FORMAT_TASK_BASE_NAME}${taskInfix}${sourceSet.name.capitalize()}"
License formatTask = project.tasks.create(sourceSetFormatTaskName, LicenseFormat)
TaskProvider<LicenseFormat> formatTask = project.tasks.register(sourceSetFormatTaskName, LicenseFormat)
configureForSourceSet(sourceSet, formatTask, sourceSetSources)

// Add independent clean task to remove headers
// TODO
}
}

protected void configureForSourceSet(sourceSet, License task, Closure<Iterable<File>> sourceSetSources) {
task.with {
protected static void configureForSourceSet(sourceSet, TaskProvider task, Closure<Iterable<File>> sourceSetSources) {
task.configure {
// Explicitly set description
description = "Scanning license on ${sourceSet.name} files"
}
it.description = "Scanning license on ${sourceSet.name} files"

// Default to all source files from SourceSet
task.source = sourceSetSources(sourceSet)
// Default to all source files from SourceSet
it.source = sourceSetSources(sourceSet)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import nl.javadude.gradle.plugins.license.DownloadLicensesReportExtension
import nl.javadude.gradle.plugins.license.LicensesReport
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.tasks.TaskProvider

class LicenseReportingPlugin implements Plugin<Project> {
static final String DOWNLOAD_LICENSES_TASK_NAME = 'downloadLicenses'
Expand All @@ -31,22 +31,23 @@ class LicenseReportingPlugin implements Plugin<Project> {

protected DownloadLicensesExtension downloadLicensesExtension

protected Task downloadLicenseTask
protected TaskProvider<DownloadLicenses> downloadLicenseTask
private Project project

@Override
void apply(Project project) {
this.project = project
project.plugins.apply(ReportingBasePlugin)
// Create a single task to run all license checks and reformattings
downloadLicenseTask = project.tasks.create(DOWNLOAD_LICENSES_TASK_NAME, DownloadLicenses)
downloadLicenseTask = project.tasks.register(DOWNLOAD_LICENSES_TASK_NAME, DownloadLicenses) {
it.group = "License"
it.description = "Generates reports on your runtime dependencies."
}

downloadLicenseTask.group = "License"
downloadLicenseTask.description = "Generates reports on your runtime dependencies."
downloadLicensesExtension = createDownloadLicensesExtension()


project.tasks.withType(DownloadLicenses) { DownloadLicenses task ->
project.tasks.withType(DownloadLicenses).configureEach { DownloadLicenses task ->
project.logger.info("Applying defaults to download task: ${task.path}");
configureTaskDefaults(task)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ import com.hierynomus.gradle.license.tasks.LicenseCheck
import com.hierynomus.gradle.license.tasks.LicenseFormat
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.TaskProvider

class LicensePlugin implements Plugin<Project> {
protected Task baseCheckTask
protected Task baseFormatTask
private static final TASK_GROUP = "License"

protected TaskProvider baseCheckTask
protected TaskProvider baseFormatTask
@Override
void apply(Project project) {
project.apply plugin: LicenseBasePlugin
project.apply plugin: LicenseReportingPlugin

baseCheckTask = project.task(LicenseBasePlugin.LICENSE_TASK_BASE_NAME)
baseFormatTask = project.task(LicenseBasePlugin.FORMAT_TASK_BASE_NAME)

baseCheckTask.group = baseFormatTask.group = "License"
baseCheckTask.description = "Checks for header consistency."
baseFormatTask.description = "Applies the license found in the header file in files missing the header."
baseCheckTask = project.tasks.register(LicenseBasePlugin.LICENSE_TASK_BASE_NAME) { task ->
task.group = TASK_GROUP
task.description = "Checks for header consistency."
}
baseFormatTask = project.tasks.register(LicenseBasePlugin.FORMAT_TASK_BASE_NAME) { task ->
task.group = TASK_GROUP
task.description = "Applies the license found in the header file in files missing the header."
}

// Add license checking into check lifecycle, since its a type of code quality plugin

Expand All @@ -51,12 +55,18 @@ class LicensePlugin implements Plugin<Project> {
}

private void linkTasks(Project project) {
project.tasks[JavaBasePlugin.CHECK_TASK_NAME].dependsOn baseCheckTask
project.tasks.withType(LicenseCheck) { lt ->
baseCheckTask.dependsOn lt
project.tasks.named(JavaBasePlugin.CHECK_TASK_NAME).configure { task ->
task.dependsOn baseCheckTask
}
baseCheckTask.configure { task ->
// Tasks are eagerly resolved here since running the base check task is expected to run
// all the LicenseCheck tasks
task.dependsOn project.tasks.withType(LicenseCheck)
}
project.tasks.withType(LicenseFormat) { lt ->
baseFormatTask.dependsOn lt
baseFormatTask.configure { task ->
// Tasks are eagerly resolved here since running the base format task is expected to
// run all the LicenseFormat tasks
task.dependsOn project.tasks.withType(LicenseFormat)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class AndroidLicensePluginTest {
def task = project.tasks.create('licenseManual', LicenseCheck)

Set<Task> dependsOn = project.tasks['check'].getDependsOn()
assertThat dependsOn, hasItem(project.tasks['license'])
assertThat dependsOn, hasItem(project.tasks.named('license'))

// Manual tests don't get registered with check
assertThat dependsOn, not(hasItem(task))
Expand All @@ -179,15 +179,15 @@ class AndroidLicensePluginTest {

def manualFormat = project.tasks.create('licenseManualFormat', LicenseFormat)

Set<Task> dependsOn = project.tasks['license'].getDependsOn()
Set<Task> dependsOn = project.tasks['license'].getDependsOn().flatten()
assertThat dependsOn, hasItem(project.tasks['licenseAndroidMain'])
assertThat dependsOn, hasItem(project.tasks['licenseAndroidAndroidTest'])

// Manual tests also get registered with check.
assertThat dependsOn, hasItem(manual)
assertThat dependsOn, not(hasItem(manualFormat))

Set<Task> dependsOnFormat = project.tasks['licenseFormat'].getDependsOn()
Set<Task> dependsOnFormat = project.tasks['licenseFormat'].getDependsOn().flatten()
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatAndroidMain'])
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatAndroidAndroidTest'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class LicensePluginTest {
def task = project.tasks.create('licenseManual', License)

Set<Task> dependsOn = project.tasks['check'].getDependsOn()
assertThat dependsOn, hasItem(project.tasks['license'])
assertThat dependsOn, hasItem(project.tasks.named('license'))

// Manual tests don't get registered with check
assertThat dependsOn, not(hasItem(task))
Expand All @@ -137,15 +137,15 @@ class LicensePluginTest {
def task = project.tasks.create('licenseManual', LicenseCheck)
def formatTask = project.tasks.create('licenseManualFormat', LicenseFormat)

Set<Task> dependsOn = project.tasks['license'].getDependsOn()
Set<Task> dependsOn = project.tasks['license'].getDependsOn().flatten()
assertThat dependsOn, hasItem(project.tasks['licenseMain'])
assertThat dependsOn, hasItem(project.tasks['licenseTest'])

// Manual tests don't get registered with check
assertThat dependsOn, hasItem(task)
assertThat dependsOn, not(hasItem(formatTask))

Set<Task> dependsOnFormat = project.tasks['licenseFormat'].getDependsOn()
Set<Task> dependsOnFormat = project.tasks['licenseFormat'].getDependsOn().flatten()
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatMain'])
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatTest'])

Expand Down