Introduce lazy tasks except for ktlint #184
Changes from 17 commits
0ef4a03
d755090
006d143
451ca3b
77d79d2
a52f78c
531aedc
758cf8f
eed3a7c
0597e2d
9384b6a
542432a
eb86bc1
71058cf
1771f1e
78e1e05
bf3eace
96c7093
c6eb082
d18b48e
5ae58ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,19 @@ import org.gradle.api.Project | |
import org.gradle.api.Task | ||
import org.gradle.api.plugins.quality.CodeQualityExtension | ||
import org.gradle.api.tasks.SourceTask | ||
import org.gradle.api.tasks.VerificationTask | ||
|
||
abstract class CodeQualityConfigurator<T extends SourceTask, E extends CodeQualityExtension> implements Configurator { | ||
import static com.novoda.staticanalysis.internal.TasksCompat.configureEach | ||
import static com.novoda.staticanalysis.internal.TasksCompat.createTask | ||
|
||
abstract class CodeQualityConfigurator<T extends SourceTask & VerificationTask, E extends CodeQualityExtension> implements Configurator { | ||
|
||
protected final Project project | ||
protected final Violations violations | ||
protected final Task evaluateViolations | ||
protected final SourceFilter sourceFilter | ||
protected final VariantFilter variantFilter | ||
protected boolean configured = false | ||
|
||
protected CodeQualityConfigurator(Project project, Violations violations, Task evaluateViolations) { | ||
this.project = project | ||
|
@@ -38,29 +43,57 @@ abstract class CodeQualityConfigurator<T extends SourceTask, E extends CodeQuali | |
} | ||
project.plugins.withId('com.android.application') { | ||
configureAndroidWithVariants(variantFilter.filteredApplicationVariants) | ||
configureToolTasks() | ||
} | ||
project.plugins.withId('com.android.library') { | ||
configureAndroidWithVariants(variantFilter.filteredLibraryVariants) | ||
configureToolTasks() | ||
} | ||
project.plugins.withId('java') { | ||
configureJavaProject() | ||
configureToolTasks() | ||
} | ||
configureEach(project.tasks.withType(taskClass)) { task -> | ||
configureToolTask(task) | ||
} | ||
} | ||
} | ||
|
||
protected void configureJavaProject() { | ||
if (configured) return | ||
|
||
project.sourceSets.all { sourceSet -> | ||
def collectViolations = createCollectViolations(getToolTaskNameFor(sourceSet), violations) | ||
evaluateViolations.dependsOn collectViolations | ||
} | ||
configured = true | ||
} | ||
|
||
def configureAndroidWithVariants(DomainObjectSet variants) { | ||
variants.all { configureAndroidVariant(it) } | ||
variantFilter.filteredTestVariants.all { configureAndroidVariant(it) } | ||
variantFilter.filteredUnitTestVariants.all { configureAndroidVariant(it) } | ||
protected void configureAndroidWithVariants(DomainObjectSet variants) { | ||
if (configured) return | ||
|
||
project.android.sourceSets.all { sourceSet -> | ||
createToolTaskForAndroid(sourceSet) | ||
createCollectViolations(getToolTaskNameFor(sourceSet), violations) | ||
} | ||
variants.all { configureVariant(it) } | ||
variantFilter.filteredTestVariants.all { configureVariant(it) } | ||
variantFilter.filteredUnitTestVariants.all { configureVariant(it) } | ||
configured = true | ||
} | ||
|
||
def configureToolTasks() { | ||
project.tasks.withType(taskClass) { task -> | ||
protected void configureVariant(variant) { | ||
def collectViolations = createVariantMetaTask(variant) | ||
evaluateViolations.dependsOn collectViolations | ||
} | ||
|
||
private def createVariantMetaTask(variant) { | ||
createTask(project, "collect${getToolTaskNameFor(variant)}VariantViolations", Task) { task -> | ||
task.group = 'verification' | ||
configureReportEvaluation(task, violations) | ||
task.description = "Runs $toolName analysis on all sources for android ${variant.name} variant" | ||
task.mustRunAfter javaCompile(variant) | ||
|
||
variant.sourceSets.forEach { sourceSet -> | ||
def toolTaskName = getToolTaskNameFor(sourceSet) | ||
task.dependsOn "collect${toolTaskName.capitalize()}Violations" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -78,17 +111,29 @@ abstract class CodeQualityConfigurator<T extends SourceTask, E extends CodeQuali | |
} | ||
} | ||
|
||
protected abstract void configureAndroidVariant(variant) | ||
|
||
protected void configureJavaProject() { | ||
project.tasks.withType(taskClass) { task -> | ||
sourceFilter.applyTo(task) | ||
task.exclude '**/*.kt' | ||
protected static def javaCompile(variant) { | ||
if (variant.hasProperty('javaCompileProvider')) { | ||
variant.javaCompileProvider.get() | ||
} else { | ||
variant.javaCompile | ||
} | ||
} | ||
|
||
protected final String getToolTaskNameFor(sourceSet) { | ||
"$toolName${sourceSet.name.capitalize()}" | ||
} | ||
|
||
protected abstract Class<T> getTaskClass() | ||
|
||
protected abstract void configureReportEvaluation(T task, Violations violations) | ||
protected abstract void createToolTaskForAndroid(sourceSet) | ||
|
||
protected void configureToolTask(T task) { | ||
sourceFilter.applyTo(task) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collected all common functionality here. It is automatically applied to all tool tasks thanks to |
||
task.group = 'verification' | ||
task.exclude '**/*.kt' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this ok because this configurator is bound to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, here |
||
task.ignoreFailures = true | ||
task.metaClass.getLogger = { QuietLogger.INSTANCE } | ||
} | ||
|
||
protected abstract def createCollectViolations(String taskName, Violations violations) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.novoda.staticanalysis.internal | ||
|
||
import org.gradle.api.Action | ||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
import org.gradle.api.tasks.TaskCollection | ||
import org.gradle.util.GradleVersion | ||
|
||
class TasksCompat { | ||
|
||
private static boolean IS_GRADLE_MIN_49 = GradleVersion.current() >= GradleVersion.version("4.9") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
static <T extends Task> Object createTask(Project project, String name, Class<T> type, Action<? super T> configuration) { | ||
if (IS_GRADLE_MIN_49) { | ||
return project.tasks.register(name, type, configuration) | ||
} else { | ||
return project.tasks.create(name, type, configuration) | ||
} | ||
} | ||
|
||
static <T extends Task> void configureEach(TaskCollection<T> tasks, Action<? super T> configuration) { | ||
if (IS_GRADLE_MIN_49) { | ||
tasks.configureEach(configuration) | ||
} else { | ||
tasks.all(configuration) | ||
} | ||
} | ||
|
||
static <T extends Task> void configureNamed(Project project, String taskName, Action<? super T> configuration) { | ||
if (IS_GRADLE_MIN_49) { | ||
project.tasks.named(taskName).configure(configuration) | ||
} else { | ||
project.tasks.getByName(taskName).configure(configuration) | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically this isn't always a sourceset right? maybe we can just called it
named
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Soooo, this was actually only for
sourceSet
. Then I realized thatFindBugs
needs to be configured per variant because it needsjavaCompile
task. I will go over and make sure that naming is consistent.