Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extent SourceTask for check and format tasks. #146

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- ?
### Changed
- Update Android gradle plugin version to `3.2.0`
- Check and format tasks now extend `SourceTask` (#85)
### Fixed
- ?
### Removed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jlleitschuh.gradle.ktlint

import net.swiftzer.semver.SemVer
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
Expand All @@ -21,6 +20,7 @@ import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskAction
import org.gradle.process.JavaExecSpec
import org.jlleitschuh.gradle.ktlint.reporter.KtlintReport
Expand All @@ -30,23 +30,11 @@ import javax.inject.Inject
@CacheableTask
open class KtlintCheckTask @Inject constructor(
private val objectFactory: ObjectFactory
) : DefaultTask() {
) : SourceTask() {

@get:Classpath
internal val classpath: ConfigurableFileCollection = project.files()

@get:Internal
internal val sourceDirectories: ConfigurableFileCollection = project.files()

@get:SkipWhenEmpty
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:InputFiles
internal val sources: FileTree = sourceDirectories.asFileTree.matching { filterable ->
KOTLIN_EXTENSIONS.forEach {
filterable.include("**/*.$it")
}
}

@get:PathSensitive(PathSensitivity.RELATIVE)
@get:InputFiles
internal val editorConfigFiles: FileCollection = getEditorConfigFiles(project)
Expand Down Expand Up @@ -75,13 +63,24 @@ open class KtlintCheckTask @Inject constructor(
get() = reporters.get()
.map {
KtlintReport(
objectFactory.property() { set(it.isAvailable()) },
objectFactory.property { set(it.isAvailable()) },
it,
newOutputFile().apply { set(it.getOutputFile()) }
)
}
.filter { it.enabled.get() }

init {
KOTLIN_EXTENSIONS.forEach {
include("**/*.$it")
}
}

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.RELATIVE)
override fun getSource(): FileTree { return super.getSource() }

@TaskAction
fun lint() {
checkMinimalSupportedKtlintVersion()
Expand All @@ -97,11 +96,7 @@ open class KtlintCheckTask @Inject constructor(
): (JavaExecSpec) -> Unit = { javaExecSpec ->
javaExecSpec.classpath = classpath
javaExecSpec.main = "com.github.shyiko.ktlint.Main"
javaExecSpec.args(sourceDirectories.flatMap { dir ->
KOTLIN_EXTENSIONS.map { extension ->
"${dir.path}/**/*.$extension"
}
})
javaExecSpec.args(getSource())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're sure this works? We only lint the sources we're supposed to?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this actually returns paths to kotlin files, not directories passed to task.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check added init {} block that adds include filters to this source task. I assume internally it filters out all passed sources.

if (verbose.get()) {
javaExecSpec.args("--verbose")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ open class KtlintPlugin : Plugin<Project> {
kotlinSourceDirectories: Iterable<*>
) {
classpath.setFrom(ktLintConfig)
sourceDirectories.setFrom(kotlinSourceDirectories)
setSource(kotlinSourceDirectories)
ktlintVersion.set(extension.version)
verbose.set(extension.verbose)
debug.set(extension.debug)
Expand Down