Skip to content

Commit

Permalink
Task configuration avoidance for wiring java plugin (#267)
Browse files Browse the repository at this point in the history
* Use Task Configuration Avoidance for non auto lint task

* minor changes to use a list of TaskProvider

* Introduce GradleLintPluginTaskConfigurer and LegacyGradleLintPluginTaskConfigurer

* do not find tasks by name in GradleLintPluginTaskConfigurer
  • Loading branch information
rpalcolea authored Nov 1, 2019
1 parent dec5bdb commit c0b7324
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package com.netflix.nebula.lint.plugin

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.compile.AbstractCompile


abstract class AbstractLintPluginTaskConfigurer {
public static final String LINT_GROUP = 'lint'
public static final String AUTO_LINT_GRADLE = 'autoLintGradle'
public static final String LINT_GRADLE = 'lintGradle'
public static final String CRITICAL_LINT_GRADLE = 'criticalLintGradle'
public static final String FIX_GRADLE_LINT = 'fixGradleLint'
public static final String FIX_LINT_GRADLE = 'fixLintGradle'
public static final String GENERATE_GRADLE_LINT_REPORT = 'generateGradleLintReport'

void configure(Project project) {
def lintExt = project.extensions.create('gradleLint', GradleLintExtension)
Expand All @@ -16,15 +21,7 @@ abstract class AbstractLintPluginTaskConfigurer {

abstract void createTasks(Project project, GradleLintExtension lintExtension)

protected void wireJavaPlugin(Project project) {
project.plugins.withType(JavaBasePlugin) {
project.tasks.withType(AbstractCompile) { task ->
project.rootProject.tasks.getByName('fixGradleLint').dependsOn(task)
project.rootProject.tasks.getByName('lintGradle').dependsOn(task)
project.rootProject.tasks.getByName('fixLintGradle').dependsOn(task)
}
}
}
abstract void wireJavaPlugin(Project project)

protected static boolean hasValidTaskConfiguration(Project project, GradleLintExtension lintExt) {
boolean shouldLint = project.hasProperty('gradleLint.alwaysRun') ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package com.netflix.nebula.lint.plugin

import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.execution.TaskExecutionListener
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.TaskState
import org.gradle.api.tasks.compile.AbstractCompile

class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer{
class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer {

@Override
void createTasks(Project project, GradleLintExtension lintExt) {
if (project.rootProject == project) {
def autoLintTask = project.tasks.create(AUTO_LINT_GRADLE, LintGradleTask)
autoLintTask.listeners = lintExt.listeners

def manualLintTask = project.tasks.register('lintGradle', LintGradleTask) {
group = 'lint'
def manualLintTask = project.tasks.register(LINT_GRADLE, LintGradleTask) {
group = LINT_GROUP
failOnWarning = true
}


def criticalLintTask = project.tasks.register('criticalLintGradle', LintGradleTask) {
group = 'lint'
def criticalLintTask = project.tasks.register(CRITICAL_LINT_GRADLE, LintGradleTask) {
group = LINT_GROUP
onlyCriticalRules = true
}


def fixTask = project.tasks.register('fixGradleLint', FixGradleLintTask) {
def fixTask = project.tasks.register(FIX_GRADLE_LINT, FixGradleLintTask) {
userDefinedListeners = lintExt.listeners
}

def fixTask2 = project.tasks.register('fixLintGradle', FixGradleLintTask) {
def fixTask2 = project.tasks.register(FIX_LINT_GRADLE, FixGradleLintTask) {
userDefinedListeners = lintExt.listeners
}

Expand All @@ -43,10 +46,36 @@ class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer{
configureReportTask(project, lintExt)
}

@Override
void wireJavaPlugin(Project project) {
project.plugins.withType(JavaBasePlugin) {
project.tasks.withType(AbstractCompile) { abstractCompileTask ->
project.rootProject.tasks.named(FIX_GRADLE_LINT).configure(new Action<Task>() {
@Override
void execute(Task fixGradleLintTask) {
fixGradleLintTask.dependsOn(abstractCompileTask)
}
})
project.rootProject.tasks.named(LINT_GRADLE).configure(new Action<Task>() {
@Override
void execute(Task lintGradleTask) {
lintGradleTask.dependsOn(abstractCompileTask)
}
})
project.rootProject.tasks.named(FIX_LINT_GRADLE).configure(new Action<Task>() {
@Override
void execute(Task fixLintGradleTask) {
fixLintGradleTask.dependsOn(abstractCompileTask)
}
})
}
}
}

private void configureAutoLint(Task autoLintTask, Project project, GradleLintExtension lintExt, List<TaskProvider> lintTasks, TaskProvider criticalLintTask) {
List<TaskProvider> lintTasksToVerify = lintTasks + criticalLintTask
project.afterEvaluate {
if(lintExt.autoLintAfterFailure) {
if (lintExt.autoLintAfterFailure) {
configureAutoLintWithFailures(autoLintTask, project, lintExt, lintTasksToVerify)
} else {
configureAutoLintWithoutFailures(autoLintTask, project, lintExt, lintTasksToVerify, criticalLintTask)
Expand All @@ -64,7 +93,7 @@ class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer{
*/
private void configureAutoLintWithFailures(Task autoLintTask, Project project, GradleLintExtension lintExt, List<TaskProvider> lintTasksToVerify) {
boolean hasExplicitLintTask = project.gradle.startParameter.taskNames.any { lintTasksToVerify.name.contains(it) }
if(!hasValidTaskConfiguration(project, lintExt) || hasExplicitLintTask) {
if (!hasValidTaskConfiguration(project, lintExt) || hasExplicitLintTask) {
return
}
finalizeAllTasksWithAutoLint(project, lintTasksToVerify, autoLintTask)
Expand Down Expand Up @@ -94,10 +123,10 @@ class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer{

@Override
void afterExecute(Task task, TaskState taskState) {
if(hasExplicitLintTask(allTasks, lintTasks) || hasFailedCriticalLintTask(allTasks, criticalLintTask)) {
if (hasExplicitLintTask(allTasks, lintTasks) || hasFailedCriticalLintTask(allTasks, criticalLintTask)) {
return
}
if(task.path == lastTask.path && !taskState.failure) {
if (task.path == lastTask.path && !taskState.failure) {
autoLintTask.lint()
}
}
Expand Down Expand Up @@ -125,16 +154,20 @@ class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer{
}

private void configureReportTask(Project project, GradleLintExtension extension) {
project.tasks.register('generateGradleLintReport', GradleLintReportTask) {
reports.all { report ->
report.conventionMapping.with {
enabled = { report.name == extension.reportFormat }
destination = {
def fileSuffix = report.name == 'text' ? 'txt' : report.name
new File(project.buildDir, "reports/gradleLint/${project.name}.$fileSuffix")
TaskProvider<GradleLintReportTask> reportTask = project.tasks.register(GENERATE_GRADLE_LINT_REPORT, GradleLintReportTask)
reportTask.configure(new Action<GradleLintReportTask>() {
@Override
void execute(GradleLintReportTask gradleLintReportTask) {
gradleLintReportTask.reports.all { report ->
report.conventionMapping.with {
enabled = { report.name == extension.reportFormat }
destination = {
def fileSuffix = report.name == 'text' ? 'txt' : report.name
new File(project.buildDir, "reports/gradleLint/${project.name}.$fileSuffix")
}
}
}
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.gradle.BuildResult
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.compile.AbstractCompile

/**
* Configure gradle lint tasks for gradle versions older than 5.
Expand All @@ -16,18 +18,18 @@ class LegacyGradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigu
def autoLintTask = project.tasks.create(AUTO_LINT_GRADLE, LintGradleTask)
autoLintTask.listeners = lintExt.listeners

def manualLintTask = project.tasks.create('lintGradle', LintGradleTask)
manualLintTask.group = 'lint'
def manualLintTask = project.tasks.create(LINT_GRADLE, LintGradleTask)
manualLintTask.group = LINT_GROUP
manualLintTask.failOnWarning = true

def criticalLintTask = project.tasks.create('criticalLintGradle', LintGradleTask)
criticalLintTask.group = 'lint'
def criticalLintTask = project.tasks.create(CRITICAL_LINT_GRADLE, LintGradleTask)
criticalLintTask.group = LINT_GROUP
criticalLintTask.onlyCriticalRules = true

def fixTask = project.tasks.create('fixGradleLint', FixGradleLintTask)
def fixTask = project.tasks.create(FIX_GRADLE_LINT, FixGradleLintTask)
fixTask.userDefinedListeners = lintExt.listeners

def fixTask2 = project.tasks.create('fixLintGradle', FixGradleLintTask)
def fixTask2 = project.tasks.create(FIX_LINT_GRADLE, FixGradleLintTask)
fixTask2.userDefinedListeners = lintExt.listeners

List<Task> lintTasks = [fixTask, fixTask2, manualLintTask, autoLintTask]
Expand All @@ -39,6 +41,18 @@ class LegacyGradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigu
configureReportTask(project, lintExt)
}


@Override
void wireJavaPlugin(Project project) {
project.plugins.withType(JavaBasePlugin) {
project.tasks.withType(AbstractCompile) { task ->
project.rootProject.tasks.getByName(FIX_GRADLE_LINT).dependsOn(task)
project.rootProject.tasks.getByName(LINT_GRADLE).dependsOn(task)
project.rootProject.tasks.getByName(FIX_LINT_GRADLE).dependsOn(task)
}
}
}

/**
* Configures autoLint for build in old versions of Gradle
* This approach is not valid on new Gradle versions since lint can do configuration resolution and doing this in BuildListener is now considered un-managed thread and bad practice
Expand Down Expand Up @@ -71,7 +85,7 @@ class LegacyGradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigu
}

private void configureReportTask(Project project, GradleLintExtension extension) {
def task = project.tasks.create('generateGradleLintReport', GradleLintReportTask)
def task = project.tasks.create(GENERATE_GRADLE_LINT_REPORT, GradleLintReportTask)
task.reports.all { report ->
report.conventionMapping.with {
enabled = { report.name == extension.reportFormat }
Expand Down

0 comments on commit c0b7324

Please sign in to comment.