diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt index 2a70418c..94cd2b52 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt @@ -1,13 +1,16 @@ package org.jmailen.gradle.kotlinter.tasks +import groovy.lang.Closure +import org.gradle.api.DefaultTask import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.FileCollection -import org.gradle.api.file.FileTree +import org.gradle.api.file.FileTreeElement import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Property +import org.gradle.api.specs.Spec import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.IgnoreEmptyDirectories import org.gradle.api.tasks.Input @@ -15,7 +18,9 @@ import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity -import org.gradle.api.tasks.SourceTask +import org.gradle.api.tasks.SkipWhenEmpty +import org.gradle.api.tasks.util.PatternFilterable +import org.gradle.api.tasks.util.PatternSet import org.gradle.work.FileChange import org.gradle.work.Incremental import org.gradle.work.InputChanges @@ -23,11 +28,12 @@ import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLE import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_EXPERIMENTAL_RULES import org.jmailen.gradle.kotlinter.support.KtLintParams import org.jmailen.gradle.kotlinter.support.findApplicableEditorConfigFiles +import java.util.concurrent.Callable abstract class ConfigurableKtLintTask( projectLayout: ProjectLayout, objectFactory: ObjectFactory, -) : SourceTask() { +) : DefaultTask(), PatternFilterable { @Input val experimentalRules: Property = objectFactory.property(default = DEFAULT_EXPERIMENTAL_RULES) @@ -51,24 +57,22 @@ abstract class ConfigurableKtLintTask( @Classpath val ruleSetsClasspath: ConfigurableFileCollection = objectFactory.fileCollection() - @Internal - val sourceFiles = project.objects.fileCollection() + private val allSourceFiles = project.objects.fileCollection() + + @get:Internal + internal val patternFilterable: PatternFilterable = PatternSet() @SkipWhenEmpty // Marks the input incremental: https://github.com/gradle/gradle/issues/17593 @InputFiles @PathSensitive(PathSensitivity.RELATIVE) @IgnoreEmptyDirectories - val source = objectFactory.fileCollection().from({ sourceFiles.asFileTree.matching(patternSet) }) - - override fun source(vararg sources: Any?): SourceTask { - sourceFiles.setFrom(*sources) - return this - } + val source: FileCollection = objectFactory.fileCollection() + .from(Callable { allSourceFiles.asFileTree.matching(patternFilterable) }) - override fun getSource(): FileTree = source.asFileTree + fun source(vararg sources: Any?) = also { allSourceFiles.setFrom(*sources) } - override fun setSource(source: Any) { - sourceFiles.from(source) + fun setSource(source: Any) { + allSourceFiles.setFrom(source) } @Internal @@ -76,6 +80,23 @@ abstract class ConfigurableKtLintTask( experimentalRules = experimentalRules.get(), disabledRules = disabledRules.get(), ) + + @Internal + override fun getIncludes(): MutableSet = patternFilterable.includes + + @Internal + override fun getExcludes(): MutableSet = patternFilterable.excludes + + override fun setIncludes(includes: MutableIterable) = also { patternFilterable.setIncludes(includes) } + override fun setExcludes(excludes: MutableIterable) = also { patternFilterable.setExcludes(excludes) } + override fun include(vararg includes: String?) = also { patternFilterable.include(*includes) } + override fun include(includes: MutableIterable) = also { patternFilterable.include(includes) } + override fun include(includeSpec: Spec) = also { patternFilterable.include(includeSpec) } + override fun include(includeSpec: Closure<*>) = also { patternFilterable.include(includeSpec) } + override fun exclude(vararg excludes: String?) = also { patternFilterable.exclude(*excludes) } + override fun exclude(excludes: MutableIterable) = also { patternFilterable.exclude(excludes) } + override fun exclude(excludeSpec: Spec) = also { patternFilterable.exclude(excludeSpec) } + override fun exclude(excludeSpec: Closure<*>) = also { patternFilterable.exclude(excludeSpec) } } internal inline fun ObjectFactory.property(default: T? = null): Property =