aboutsummaryrefslogtreecommitdiff
path: root/gradle-plugin
diff options
context:
space:
mode:
authorMattia Iavarone <mat.iavarone@gmail.com>2021-09-07 10:44:33 +0200
committerlaszio <ting-yuan@users.noreply.github.com>2021-10-01 16:27:38 -0700
commit7713bb2e68f7dcfa27661006cd6061c25fcd0bd1 (patch)
tree2ff1fd13fdd172ac30f4ad324d158d7d1a8cc3cd /gradle-plugin
parent9607f898df153ede61b9ea0f974de36008d2c67d (diff)
downloadksp-7713bb2e68f7dcfa27661006cd6061c25fcd0bd1.tar.gz
Fix lint, comment wording
Diffstat (limited to 'gradle-plugin')
-rw-r--r--gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/AndroidPluginIntegration.kt1
-rw-r--r--gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt68
-rw-r--r--gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt1
-rw-r--r--gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt2
4 files changed, 33 insertions, 39 deletions
diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/AndroidPluginIntegration.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/AndroidPluginIntegration.kt
index d5650b03..cd23bb23 100644
--- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/AndroidPluginIntegration.kt
+++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/AndroidPluginIntegration.kt
@@ -19,7 +19,6 @@ package com.google.devtools.ksp.gradle
import com.android.build.api.dsl.CommonExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
-import org.gradle.api.Named
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.TaskProvider
diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt
index 45b23d6a..431dc456 100644
--- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt
+++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspConfigurations.kt
@@ -11,34 +11,34 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
*/
class KspConfigurations(private val project: Project) {
companion object {
- const val ROOT = "ksp"
+ private const val PREFIX = "ksp"
}
- // "ksp" configuration. In single-platform projects, it is applied to the "main" sourceSet.
- // In multi-platform projects, it is applied to the "main" sourceSet of all targets.
- private val rootMainConfiguration = project.configurations.create(ROOT)
+ // "ksp" configuration, applied to the "main" source set of all targets.
+ private val rootMainConfiguration = project.configurations.create(PREFIX)
- // Stores all saved configurations for quick access.
+ // Store all ksp configurations for quick retrieval.
private val kotlinConfigurations = mutableMapOf<KotlinSourceSet, Configuration>()
private val androidConfigurations = mutableMapOf<String, Configuration>()
@OptIn(ExperimentalStdlibApi::class)
- private fun <T: Any> saveConfiguration(
+ private fun <T : Any> saveConfiguration(
owner: T,
parent: Configuration?,
name: String,
cache: MutableMap<T, Configuration>
): Configuration {
- val configName = ROOT + name.replaceFirstChar { it.uppercase() }
- val existingConfig = project.configurations.findByName(configName)
- if (existingConfig != null && configName != ROOT) {
- error("Unexpected duplicate configuration ($configName).")
- }
-
- val config = existingConfig ?: project.configurations.create(configName)
- if (parent != null && parent.name != configName) {
- config.extendsFrom(parent)
- }
+ val configName = PREFIX + name.replaceFirstChar { it.uppercase() }
+ val config = if (configName == parent?.name) {
+ // trying to add config with same parent name. Can happen, for example
+ // with ksp<Target> when <Target> is "". Just use parent.
+ parent
+ } else {
+ // maybeCreate to be future-proof, but we should never have a duplicate with current logic
+ project.configurations.maybeCreate(configName).apply {
+ if (parent != null) extendsFrom(parent)
+ }
+ }!!
cache[owner] = config
return config
}
@@ -51,7 +51,7 @@ class KspConfigurations(private val project: Project) {
init {
project.plugins.withType(KotlinBasePluginWrapper::class.java).configureEach {
- // 1.6.0: decorateKotlinProject(project.kotlinExtension)
+ // 1.6.0: decorateKotlinProject(project.kotlinExtension)?
decorateKotlinProject(project.extensions.getByName("kotlin") as KotlinProjectExtension)
}
}
@@ -64,16 +64,15 @@ class KspConfigurations(private val project: Project) {
}
/**
- * Decorate the source sets belonging to [target].
- * The end goal is to have one KSP configuration per source set. Examples:
- * - in a kotlin-jvm project, we want "ksp" (applied to main set) and "kspTest" (applied to test set)
- * - in a kotlin-multiplatform project, we want "ksp<Target>" and "ksp<Target>Test" for each target.
- * This is done by reading [KotlinCompilation.kotlinSourceSets], which contains appropriately named sets.
+ * Decorate the [KotlinSourceSet]s belonging to [target] to create one KSP configuration per source set,
+ * named ksp<SourceSet>. The only exception is the main source set, for which we avoid using the
+ * "main" suffix (so what would be "kspJvmMain" becomes "kspJvm").
*
- * For Android, we prefer to use AndroidSourceSets from AGP rather than [KotlinSourceSet]s like all other
- * targets. There are very slight differences between the two - this could be re-evaluated in the future,
- * because Kotlin Plugin does already create [KotlinSourceSet]s out of AndroidSourceSets
- * ( https://kotlinlang.org/docs/mpp-configure-compilations.html#compilation-of-the-source-set-hierarchy ).
+ * For Android, we prefer to use AndroidSourceSets from AGP rather than [KotlinSourceSet]s.
+ * Even though the Kotlin Plugin does create [KotlinSourceSet]s out of AndroidSourceSets
+ * ( https://kotlinlang.org/docs/mpp-configure-compilations.html#compilation-of-the-source-set-hierarchy ),
+ * there are slight differences between the two - Kotlin creates some extra sets with unexpected word ordering,
+ * and things get worse when you add product flavors. So, we use AGP sets as the source of truth.
*/
private fun decorateKotlinTarget(target: KotlinTarget) {
if (target.platformType == KotlinPlatformType.androidJvm) {
@@ -91,8 +90,6 @@ class KspConfigurations(private val project: Project) {
saveAndroidConfiguration(setName, parent, nameWithTargetPrefix)
}
} else {
- // We could add target-specific configurations here (kspJvm, parent of kspJvmMain & kspJvmTest)
- // but we decided that kspJvm should actually mean kspJvmMain, which in turn is not created.
target.compilations.configureEach { compilation ->
val isMain = compilation.name == KotlinCompilation.MAIN_COMPILATION_NAME
compilation.kotlinSourceSets.forEach { sourceSet ->
@@ -122,17 +119,16 @@ class KspConfigurations(private val project: Project) {
/**
* Returns the user-facing configurations involved in the given compilation.
* We use [KotlinCompilation.kotlinSourceSets], not [KotlinCompilation.allKotlinSourceSets] for a few reasons:
- * 1) consistency with how we created the configurations
- * 2) all* can return sets belonging to other compilations. In this case the dependency should be tracked
- * by Gradle at the task level, not by us through configurations.
- * 3) all* can return user-defined sets belonging to no compilation, like intermediate source sets defined
- * to share code between targets. They do not currently have their own ksp configuration.
+ * 1) consistency with how we created the configurations. For example, all* can return user-defined sets
+ * that don't belong to any compilation, like user-defined intermediate source sets (e.g. iosMain).
+ * These do not currently have their own ksp configuration.
+ * 2) all* can return sets belonging to other [KotlinCompilation]s
*/
fun find(compilation: KotlinCompilation<*>): Set<Configuration> {
- val kotlinSourceSets = compilation.kotlinSourceSets
- val kotlinConfigurations = kotlinSourceSets.mapNotNull { kotlinConfigurations[it] }
+ val kotlinConfigurations = compilation.kotlinSourceSets.mapNotNull { kotlinConfigurations[it] }
val androidConfigurations = if (compilation.platformType == KotlinPlatformType.androidJvm) {
- val androidSourceSets = AndroidPluginIntegration.getCompilationSourceSets(compilation as KotlinJvmAndroidCompilation)
+ compilation as KotlinJvmAndroidCompilation
+ val androidSourceSets = AndroidPluginIntegration.getCompilationSourceSets(compilation)
androidSourceSets.mapNotNull { androidConfigurations[it] }
} else emptyList()
return (kotlinConfigurations + androidConfigurations).toSet()
diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt
index 990eb5ab..e47b663c 100644
--- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt
+++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt
@@ -20,7 +20,6 @@
package com.google.devtools.ksp.gradle
import com.google.devtools.ksp.gradle.model.builder.KspModelBuilder
-import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt
index c4bb35d5..68d75d11 100644
--- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt
+++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt
@@ -119,7 +119,7 @@ class KspIntegrationTestRule(
testProject.appModule.buildFileAdditions.add(
"""
kotlin {
- ${targets.joinToString(separator = "\n") { it.trimIndent()} }
+ ${targets.joinToString(separator = "\n") { it.trimIndent() } }
}
""".trimIndent()
)