Skip to content

Commit

Permalink
Migrate away from using SourceTask
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Jan 24, 2023
1 parent b68c994 commit a05ebaf
Showing 1 changed file with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
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
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
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLED_RULES
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<Boolean> = objectFactory.property(default = DEFAULT_EXPERIMENTAL_RULES)
Expand All @@ -51,31 +57,46 @@ 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
protected fun getKtLintParams(): KtLintParams = KtLintParams(
experimentalRules = experimentalRules.get(),
disabledRules = disabledRules.get(),
)

@Internal
override fun getIncludes(): MutableSet<String> = patternFilterable.includes

@Internal
override fun getExcludes(): MutableSet<String> = patternFilterable.excludes

override fun setIncludes(includes: MutableIterable<String>) = also { patternFilterable.setIncludes(includes) }
override fun setExcludes(excludes: MutableIterable<String>) = also { patternFilterable.setExcludes(excludes) }
override fun include(vararg includes: String?) = also { patternFilterable.include(*includes) }
override fun include(includes: MutableIterable<String>) = also { patternFilterable.include(includes) }
override fun include(includeSpec: Spec<FileTreeElement>) = 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<String>) = also { patternFilterable.exclude(excludes) }
override fun exclude(excludeSpec: Spec<FileTreeElement>) = also { patternFilterable.exclude(excludeSpec) }
override fun exclude(excludeSpec: Closure<*>) = also { patternFilterable.exclude(excludeSpec) }
}

internal inline fun <reified T> ObjectFactory.property(default: T? = null): Property<T> =
Expand Down

0 comments on commit a05ebaf

Please sign in to comment.