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

Do not create tasks for KSP sourcesets #167

Merged
merged 1 commit into from
Jul 15, 2023
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
3 changes: 3 additions & 0 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
kotlin("jvm")
id("com.ncorti.ktfmt.gradle")
id("com.google.devtools.ksp") version "1.9.0-1.0.11"
}

ktfmt {
Expand All @@ -11,6 +12,8 @@ dependencies {
testImplementation(platform(libs.junit.bom))
testImplementation(libs.jupiter)
testRuntimeOnly(libs.jupiter.platform.launcher)
implementation("com.squareup.moshi:moshi-kotlin:1.14.0")
ksp("com.squareup.moshi:moshi-kotlin-codegen:1.14.0")
}

tasks.withType<Test> {
Expand Down
3 changes: 3 additions & 0 deletions example/src/main/java/KspSample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true) data class KspSample(val something: String)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ internal object KtfmtPluginUtils {

internal const val TASK_NAME_CHECK = "ktfmtCheck"

internal fun shouldCreateTasks(srcSetName: String): Boolean {
// KSP is adding new source sets called `generatedByKspKotlin`, `generatedByKspTestKotlin`
// For those source sets we don't want to run KSP as they're only generated code.
return !srcSetName.startsWith("generatedByKsp")
}

@Suppress("LongParameterList")
internal fun createTasksForSourceSet(
project: Project,
Expand All @@ -26,6 +32,10 @@ internal object KtfmtPluginUtils {
topLevelFormat: TaskProvider<Task>,
topLevelCheck: TaskProvider<Task>
) {
if (shouldCreateTasks(srcSetName).not()) {
return
}

val srcCheckTask = createCheckTask(project, ktfmtExtension, srcSetName, srcSetDir)
val srcFormatTask = createFormatTask(project, ktfmtExtension, srcSetName, srcSetDir)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ncorti.ktfmt.gradle.util

import com.google.common.truth.Truth.assertThat
import com.ncorti.ktfmt.gradle.KtfmtPluginUtils.shouldCreateTasks
import org.junit.jupiter.api.Test

class KtfmtPluginUtilsTest {

@Test
fun `shouldCreateTasks returns true for main sourceset`() {
assertThat(shouldCreateTasks("main")).isTrue()
}

@Test
fun `shouldCreateTasks returns false for KSP sourceset`() {
assertThat(shouldCreateTasks("generatedByKspKotlin")).isFalse()
}
}