Skip to content

Commit

Permalink
Merge pull request #126 from usefulness/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Nov 3, 2023
2 parents 3b71bd5 + 7102a17 commit 4474414
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ktlint {
ktlintVersion = "1.0.0-SNAPSHOT"
chunkSize = 50
baselineFile.set(file("config/ktlint_baseline.xml"))
ignoreKspGeneratedSources = true
ignoreFilesUnderBuildDir = true
}
```

Expand All @@ -84,7 +84,7 @@ ktlint {

- `chunkSize` - defines how many files will be processed by a single gradle worker in parallel
- `baselineFile` - points at location of baseline file containing _known_ offenses that will be ignored during `lintKotlin` task execution
- `ignoreKspGeneratedSources` - indicates if the plugin should automatically register tasks for KSP generated sources (hence triggering compilation tasks). This is an incubating, rather primitive workaround for the issue reported: https://github.com/google/ksp/issues/1261.
- `ignoreFilesUnderBuildDir` - This allows to ignore generated sources. This is a workaround for https://youtrack.jetbrains.com/issue/KT-45161. Setting the value to `false` restores default behavior and will run ktlint against all sources returned by KGP

### Customizing Tasks

Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ maven-commons = "2.15.0"

[libraries]
agp-gradle = { module = "com.android.tools.build:gradle", version.ref = "google-agp" }
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" }
kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api" }
kotlin-gradle-impl = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" }
ktlint-rule-engine = { module = "com.pinterest.ktlint:ktlint-rule-engine", version.ref = "maven-ktlint" }
ktlint-cli-ruleset-core = { module = "com.pinterest.ktlint:ktlint-cli-ruleset-core", version.ref = "maven-ktlint" }
ktlint-cli-reporter-core = { module = "com.pinterest.ktlint:ktlint-cli-reporter-core", version.ref = "maven-ktlint" }
Expand Down
8 changes: 4 additions & 4 deletions ktlint-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jacoco {
}

dependencies {
compileOnly(libs.kotlin.gradle)
compileOnly(libs.kotlin.gradle.api)
compileOnly(libs.agp.gradle)
compileOnly(libs.ktlint.rule.engine)
compileOnly(libs.ktlint.cli.ruleset.core)
Expand All @@ -39,7 +39,7 @@ dependencies {
testImplementation(libs.commons.io)
testImplementation(libs.assertj.core)

testRuntimeDependencies(libs.kotlin.gradle)
testRuntimeDependencies(libs.kotlin.gradle.impl)
testRuntimeDependencies(libs.agp.gradle)
testRuntimeDependencies(libs.google.ksp.gradle)
}
Expand All @@ -66,8 +66,8 @@ tasks.named("processResources") {

tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
apiVersion = "1.4"
languageVersion = "1.4"
apiVersion = "1.8"
languageVersion = "1.8"
}
}
tasks.withType(Test).configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ public open class KtlintGradleExtension internal constructor(
public val baselineFile: RegularFileProperty = objectFactory.fileProperty()

@Incubating
public val ignoreKspGeneratedSources: Property<Boolean> = objectFactory.property(default = true)
public val ignoreFilesUnderBuildDir: Property<Boolean> = objectFactory.property(default = true)

@Deprecated(message = "Will be removed in the next version", replaceWith = ReplaceWith(expression = "ignoreFilesUnderBuildDir"))
@Incubating
public val ignoreKspGeneratedSources: Property<Boolean> = ignoreFilesUnderBuildDir
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class KtlintGradlePlugin : Plugin<Project> {

sourceResolver.applyToAll(this, pluginExtension) { id, resolvedSources ->
val checkWorker = tasks.register(
"lintKotlin${id.capitalize()}",
"lintKotlin${id.replaceFirstChar(Char::titlecase)}",
LintTask::class.java,
) { task ->
task.source(resolvedSources)
Expand All @@ -67,7 +67,7 @@ public class KtlintGradlePlugin : Plugin<Project> {
lintKotlin.configure { it.dependsOn(checkWorker) }

val formatWorker = tasks.register(
"formatKotlin${id.capitalize()}",
"formatKotlin${id.replaceFirstChar(Char::titlecase)}",
FormatTask::class.java,
) { task ->
task.source(resolvedSources)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.github.usefulness

import org.gradle.api.Project
import org.gradle.api.file.FileTree
import org.gradle.api.provider.Provider

internal typealias SourceSetAction = (String, Provider<FileTree>) -> Unit
internal typealias SourceSetAction = (String, Any) -> Unit

internal interface SourceSetApplier {
fun applyToAll(project: Project, extension: KtlintGradleExtension, action: SourceSetAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal object AndroidSourceSetApplier : SourceSetApplier {
android.finalizeDsl { commonExtension ->
commonExtension.sourceSets.configureEach { sourceSet ->
val id = sourceSet.name.id
action(id, project.provider { getKotlinFiles(project, sourceSet) })
action(id, getKotlinFiles(project, sourceSet))
}
}
}

private fun getKotlinFiles(project: Project, sourceSet: AndroidSourceSet): FileTree? {
private fun getKotlinFiles(project: Project, sourceSet: AndroidSourceSet): FileTree {
val javaSources = sourceSet.java.srcDirs
val kotlinSources = sourceSet.kotlin.srcDirs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ import io.github.usefulness.SourceSetAction
import io.github.usefulness.SourceSetApplier
import io.github.usefulness.id
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer
import java.io.File

internal object KotlinSourceSetApplier : SourceSetApplier {

override fun applyToAll(project: Project, extension: KtlintGradleExtension, action: SourceSetAction) {
project.extensions.getByType(KotlinProjectExtension::class.java).sourceSets.configureEach { sourceSet ->
if (!sourceSet.name.startsWith("generatedByKsp") || !extension.ignoreKspGeneratedSources.get()) {
action(sourceSet.kotlin.name.id, project.provider { sourceSet.kotlin })
project.extensions.getByType(KotlinSourceSetContainer::class.java).sourceSets.configureEach { sourceSet ->
val directories = project.provider {
if (extension.ignoreFilesUnderBuildDir.get()) {
sourceSet.kotlin.srcDirTrees.mapNotNull { dirTree ->
dirTree.dir.takeUnless { it.isUnder(project.layout.buildDirectory.asFile.get()) }
}
} else {
sourceSet.kotlin.sourceDirectories
}
}

action(sourceSet.kotlin.name.id, directories)
}
}

private fun File.isUnder(rootDirectory: File) = canonicalPath.startsWith(rootDirectory.canonicalPath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import io.github.usefulness.functional.utils.kotlinClass
import io.github.usefulness.functional.utils.resolve
import io.github.usefulness.functional.utils.settingsFile
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class ThirdPartyPlugins : WithGradleTest.Android() {

@Disabled("This doesn't work with Kotlin 1.9.20 :/ https://github.com/google/ksp/pull/1558")
@Test
fun kspKotlin() {
testProjectDir.apply {
Expand All @@ -32,7 +30,9 @@ class ThirdPartyPlugins : WithGradleTest.Android() {
repositories.mavenCentral()
dependencies {
implementation "com.google.dagger:dagger:2.48.1"
ksp "com.google.dagger:dagger-compiler:2.48.1"
}
kotlin {
Expand All @@ -53,7 +53,24 @@ class ThirdPartyPlugins : WithGradleTest.Android() {
resolve("src/main/kotlin/KotlinClass.kt") {
writeText(kotlinClass("KotlinClass"))
}
resolve("src/test/kotlin/KotlinTestClass.kt") {
writeText(kotlinClass("KotlinTestClass"))
}
resolve("src/main/kotlin/FooModule.kt") {
writeText(
// language=kotlin
"""
@dagger.Module
object FooModule {
@dagger.Provides
fun foo(): String = ""
}
""".trimIndent(),
)
}
}
build("assemble") // generate files under `build` directory

val result = build("lintKotlin")

Expand All @@ -79,9 +96,9 @@ class ThirdPartyPlugins : WithGradleTest.Android() {

assertThat(onlyMain.tasks.map { it.path }).containsAll(
listOf(
":kspKotlin",
":compileKotlin",
":compileJava",
":lintKotlinGeneratedByKspTestKotlin",
":lintKotlinTest",
":lintKotlinMain",
":lintKotlin",
Expand Down Expand Up @@ -116,7 +133,7 @@ class ThirdPartyPlugins : WithGradleTest.Android() {
repositories.mavenCentral()
dependencies {
ksp "com.google.dagger:dagger-compiler:2.48"
ksp "com.google.dagger:dagger-compiler:2.48.1"
}
""".trimIndent(),
Expand Down

0 comments on commit 4474414

Please sign in to comment.