From 491aae8fdc531eac83bc13c857e277b08d1b5955 Mon Sep 17 00:00:00 2001 From: Vyacheslav Rusakov Date: Wed, 20 Nov 2024 15:00:36 +0700 Subject: [PATCH] add androidVariants option to use instead of sourceSets in android projects (to declare what animalsniffer tasks would be called after check task) --- CHANGELOG.md | 4 ++++ .../animalsniffer/AnimalSnifferExtension.groovy | 7 +++++++ .../animalsniffer/AnimalSnifferPlugin.groovy | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0a29f0..f379427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ * (breaking) Drop gradle 5 and 6 support +* Add android projects support (activates for android library and application plugins + and use variants (debug, release) instead of source sets) +* Add androidVariants configuration option to use instead of sourceSets in android projects + (to define which animalsniffer tasks would run with check task (together with build)) ### 1.7.2 (2024-11-18) * Update animalsniffer 1.23 -> 1.24 diff --git a/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferExtension.groovy b/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferExtension.groovy index 7b9bd0f..f5618af 100644 --- a/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferExtension.groovy +++ b/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferExtension.groovy @@ -77,6 +77,13 @@ class AnimalSnifferExtension extends CodeQualityExtension { */ CheckCacheExtension cache = new CheckCacheExtension() + /** + * When used with android, {@link #sourceSets} can't be used to configure what tasks to link "check" task. + * Instead, use this option to specify target android variants. Leave empty to not run animalsniffer after + * "check" task execution. + */ + Set androidVariants = ['debug'] as Set + /** * @param cache cache configuration closure */ diff --git a/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferPlugin.groovy b/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferPlugin.groovy index 391bb48..730e0fd 100644 --- a/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferPlugin.groovy +++ b/src/main/groovy/ru/vyarus/gradle/plugin/animalsniffer/AnimalSnifferPlugin.groovy @@ -54,6 +54,7 @@ class AnimalSnifferPlugin implements Plugin { private static final String CHECK_SIGNATURE = 'animalsniffer' private static final String BUILD_SIGNATURE = 'animalsnifferSignature' + private static final String CHECK_TASK = 'check' private Project project private AnimalSnifferExtension extension @@ -166,7 +167,7 @@ class AnimalSnifferPlugin implements Plugin { } // include required animalsniffer tasks in check lifecycle - project.tasks.named('check').configure { + project.tasks.named(CHECK_TASK).configure { dependsOn { extension.sourceSets*.getTaskName(CHECK_SIGNATURE, null) } @@ -177,6 +178,8 @@ class AnimalSnifferPlugin implements Plugin { @CompileStatic(TypeCheckingMode.SKIP) private void registerAndroidCheckTasks() { Object androidComponents = project.androidComponents + Map sourceIndex = [:] + androidComponents.onVariants(androidComponents.selector().all()) { variant -> String sourceSetName = variant.name String capitalizedSourceSetName = sourceSetName.capitalize() @@ -197,6 +200,7 @@ class AnimalSnifferPlugin implements Plugin { }) } } + sourceIndex.put(sourceSetName, checkTask) configureCheckTask(checkTask, project.provider { project.files(variant.sources.java.all, variant.sources.kotlin.all) }, @@ -204,6 +208,15 @@ class AnimalSnifferPlugin implements Plugin { classesCollectorTaskName, variant.compileClasspath) } + + // include required animalsniffer tasks in check lifecycle + project.tasks.named(CHECK_TASK).configure { task -> + extension.androidVariants.each { + if (sourceIndex.containsKey(it)) { + task.dependsOn(sourceIndex[it]) + } + } + } } @CompileStatic(TypeCheckingMode.SKIP)