All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.nebula.lint.plugin.GradleLintPluginTaskConfigurer.groovy Maven / Gradle / Ivy

Go to download

Pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts

There is a newer version: 20.2.2
Show newest version
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

abstract class GradleLintPluginTaskConfigurer extends AbstractLintPluginTaskConfigurer {
    abstract Action configureReportAction(Project project, GradleLintExtension extension)

    @Override
    void createTasks(Project project, GradleLintExtension lintExt) {
        if (project.rootProject == project) {
            def autoLintTask = project.tasks.register(AUTO_LINT_GRADLE, LintGradleTask)
            autoLintTask.configure {
                group = LINT_GROUP
                listeners = lintExt.listeners
                projectRootDir.set(project.rootDir)
                notCompatibleWithConfigurationCache("Gradle Lint Plugin is not compatible with configuration cache because it requires project model")
            }

            def manualLintTask = project.tasks.register(LINT_GRADLE, LintGradleTask)
            manualLintTask.configure {
                group = LINT_GROUP
                failOnWarning.set(true)
                projectRootDir.set(project.rootDir)
                notCompatibleWithConfigurationCache("Gradle Lint Plugin is not compatible with configuration cache because it requires project model")
            }


            def criticalLintTask = project.tasks.register(CRITICAL_LINT_GRADLE, LintGradleTask)
            criticalLintTask.configure {
                group = LINT_GROUP
                onlyCriticalRules.set(true)
                projectRootDir.set(project.rootDir)
                notCompatibleWithConfigurationCache("Gradle Lint Plugin is not compatible with configuration cache because it requires project model")
            }


            def fixTask = project.tasks.register(FIX_GRADLE_LINT, FixGradleLintTask)
            fixTask.configure {
                userDefinedListeners.set(lintExt.listeners)
                notCompatibleWithConfigurationCache("Gradle Lint Plugin is not compatible with configuration cache because it requires project model")
            }

            def fixTask2 = project.tasks.register(FIX_LINT_GRADLE, FixGradleLintTask)
            fixTask2.configure {
                userDefinedListeners.set(lintExt.listeners)
                notCompatibleWithConfigurationCache("Gradle Lint Plugin is not compatible with configuration cache because it requires project model")
            }

            List lintTasks = [fixTask, fixTask2, manualLintTask]

            configureAutoLint(autoLintTask, project, lintExt, lintTasks, criticalLintTask)

        }

        configureReportTask(project, lintExt)
    }

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

    protected void configureAutoLint(TaskProvider autoLintTask, Project project, GradleLintExtension lintExt, List lintTasks, TaskProvider criticalLintTask) {
        List lintTasksToVerify = lintTasks + criticalLintTask
        project.afterEvaluate {
            if (lintExt.autoLintAfterFailure) {
                configureAutoLintWithFailures(autoLintTask, project, lintExt, lintTasksToVerify)
            } else {
                configureAutoLintWithoutFailures(autoLintTask, project, lintExt, lintTasksToVerify, criticalLintTask)
            }
        }
    }

    /**
     * finalizes all tasks with autoLint if the build doesn't have explicit lint task and has valid configuration
     * Hooks into failed tasks, too
     * @param autoLintTask
     * @param project
     * @param lintExt
     * @param lintTasksToVerify
     */
    protected void configureAutoLintWithFailures(TaskProvider autoLintTask, Project project, GradleLintExtension lintExt, List lintTasksToVerify) {
        boolean hasExplicitLintTask = project.gradle.startParameter.taskNames.any { lintTasksToVerify.name.contains(it) }
        if (!hasValidTaskConfiguration(project, lintExt) || hasExplicitLintTask) {
            return
        }
        finalizeAllTasksWithAutoLint(project, lintTasksToVerify, autoLintTask, lintExt)

    }

    /**
     * finalizes all tasks with autoLint if the build doesn't have explicit lint task and has valid configuration
     * Does not hook into failed tasks, too
     * @param autoLintTask
     * @param project
     * @param lintExt
     * @param lintTasks
     * @param criticalLintTask
     */
    protected void configureAutoLintWithoutFailures(TaskProvider autoLintTask, Project project, GradleLintExtension lintExt, List lintTasks, TaskProvider criticalLintTask) {
        project.gradle.taskGraph.whenReady { taskGraph ->
            List allTasks = taskGraph.allTasks
            if (hasValidTaskConfiguration(project, lintExt)) {
                LinkedList tasks = taskGraph.executionPlan.executionQueue
                Task lastTask = tasks.last?.task
                taskGraph.addTaskExecutionListener(new TaskExecutionListener() {
                    @Override
                    void beforeExecute(Task task) {
                        //DO NOTHING
                    }

                    @Override
                    void afterExecute(Task task, TaskState taskState) {
                        if (hasExplicitLintTask(allTasks, lintTasks) || hasFailedCriticalLintTask(allTasks, criticalLintTask)) {
                            return
                        }
                        if (task.path == lastTask.path && !taskState.failure) {
                            autoLintTask.get().lint()
                        }
                    }
                })
            }
        }
    }

    /**
     * Finalizes all tasks that aren't lint related with autoLint
     * This works with --parallel and failed tasks
     * @param project
     * @param lintTasks
     * @param autoLintTask
     */
    protected void finalizeAllTasksWithAutoLint(Project project, List lintTasks, TaskProvider autoLintTask, GradleLintExtension lintExt) {
        project.tasks.configureEach { task ->
            boolean skipForSpecificTask = lintExt.skipForTasks.any { taskToSkip -> task.name.endsWith(taskToSkip) }

            if (!lintTasks.contains(task) && !task.name.contains(AUTO_LINT_GRADLE) && !task.name.contains(CLEAN_TASK_NAME) && !skipForSpecificTask) {
                task.finalizedBy autoLintTask
            }
        }
        project.childProjects.values().each { subProject ->
            finalizeAllTasksWithAutoLint(subProject, lintTasks, autoLintTask, lintExt)
        }
    }

    protected void configureReportTask(Project project, GradleLintExtension extension) {
        TaskProvider reportTask = project.tasks.register(GENERATE_GRADLE_LINT_REPORT, GradleLintReportTask)
        reportTask.configure(configureReportAction(project, extension))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy