Use KotlinTarget.disambiguationClassifier for task & configuration names
In MPP, the disambiguation classifier is always the same as the target name. In single-platform projects, the task name suffix should be empty for the JVM targets. Using the `disambiguationClassifier` for this purpose is more consistent with other usages of `disambiguationClassifier`. The target name for all existing single-platform plugins will be just 'kotlin'.
This commit is contained in:
+19
-12
@@ -30,9 +30,8 @@ import org.gradle.api.tasks.compile.AbstractCompile
|
||||
import org.gradle.api.tasks.compile.JavaCompile
|
||||
import org.gradle.jvm.tasks.Jar
|
||||
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
|
||||
import org.jetbrains.kotlin.gradle.dsl.*
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinSingleJavaTargetExtension
|
||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin
|
||||
import org.jetbrains.kotlin.gradle.internal.KaptVariantData
|
||||
@@ -43,6 +42,7 @@ import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.*
|
||||
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
|
||||
import org.jetbrains.kotlin.gradle.tasks.*
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import org.jetbrains.kotlin.gradle.utils.*
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
@@ -394,6 +394,8 @@ internal abstract class AbstractKotlinPlugin(
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val KOTLIN_TARGET_NAME = "kotlin"
|
||||
|
||||
fun configureProjectGlobalSettings(project: Project, kotlinPluginVersion: String) {
|
||||
configureDefaultVersionsResolutionStrategy(project, kotlinPluginVersion)
|
||||
configureClassInspectionForIC(project)
|
||||
@@ -552,18 +554,19 @@ internal fun configureDefaultVersionsResolutionStrategy(project: Project, kotlin
|
||||
internal open class KotlinPlugin(
|
||||
kotlinPluginVersion: String,
|
||||
registry: ToolingModelBuilderRegistry
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetName), kotlinPluginVersion, registry) {
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetDisambiguationClassifier), kotlinPluginVersion, registry) {
|
||||
|
||||
companion object {
|
||||
private const val targetName = "" // use empty suffix for the task names
|
||||
// Don't add anything to the task names
|
||||
private const val targetDisambiguationClassifier: String = ""
|
||||
}
|
||||
|
||||
override fun buildSourceSetProcessor(project: Project, compilation: KotlinCompilation<*>, kotlinPluginVersion: String) =
|
||||
Kotlin2JvmSourceSetProcessor(project, tasksProvider, compilation, kotlinPluginVersion)
|
||||
|
||||
override fun apply(project: Project) {
|
||||
val target = KotlinWithJavaTarget<KotlinJvmOptions>(project, KotlinPlatformType.jvm, targetName).apply {
|
||||
disambiguationClassifier = null // don't add anything to the task names
|
||||
val target = KotlinWithJavaTarget<KotlinJvmOptions>(project, KotlinPlatformType.jvm, KOTLIN_TARGET_NAME).apply {
|
||||
disambiguationClassifier = targetDisambiguationClassifier
|
||||
}
|
||||
(project.kotlinExtension as KotlinJvmProjectExtension).target = target
|
||||
|
||||
@@ -576,10 +579,10 @@ internal open class KotlinPlugin(
|
||||
internal open class KotlinCommonPlugin(
|
||||
kotlinPluginVersion: String,
|
||||
registry: ToolingModelBuilderRegistry
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetName), kotlinPluginVersion, registry) {
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetDisambiguationClassifier), kotlinPluginVersion, registry) {
|
||||
|
||||
companion object {
|
||||
private const val targetName = "common"
|
||||
private const val targetDisambiguationClassifier = "common"
|
||||
}
|
||||
|
||||
override fun buildSourceSetProcessor(
|
||||
@@ -590,7 +593,9 @@ internal open class KotlinCommonPlugin(
|
||||
KotlinCommonSourceSetProcessor(project, compilation, tasksProvider, kotlinPluginVersion)
|
||||
|
||||
override fun apply(project: Project) {
|
||||
val target = KotlinWithJavaTarget<KotlinMultiplatformCommonOptions>(project, KotlinPlatformType.common, targetName)
|
||||
val target = KotlinWithJavaTarget<KotlinMultiplatformCommonOptions>(project, KotlinPlatformType.common, KOTLIN_TARGET_NAME).apply {
|
||||
disambiguationClassifier = targetDisambiguationClassifier
|
||||
}
|
||||
(project.kotlinExtension as KotlinCommonProjectExtension).target = target
|
||||
|
||||
super.apply(project)
|
||||
@@ -600,10 +605,10 @@ internal open class KotlinCommonPlugin(
|
||||
internal open class Kotlin2JsPlugin(
|
||||
kotlinPluginVersion: String,
|
||||
registry: ToolingModelBuilderRegistry
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetName), kotlinPluginVersion, registry) {
|
||||
) : AbstractKotlinPlugin(KotlinTasksProvider(targetDisambiguationClassifier), kotlinPluginVersion, registry) {
|
||||
|
||||
companion object {
|
||||
private const val targetName = "2Js"
|
||||
private const val targetDisambiguationClassifier = "2Js"
|
||||
}
|
||||
|
||||
override fun buildSourceSetProcessor(
|
||||
@@ -616,7 +621,9 @@ internal open class Kotlin2JsPlugin(
|
||||
)
|
||||
|
||||
override fun apply(project: Project) {
|
||||
val target = KotlinWithJavaTarget<KotlinJsOptions>(project, KotlinPlatformType.js, targetName)
|
||||
val target = KotlinWithJavaTarget<KotlinJsOptions>(project, KotlinPlatformType.js, KOTLIN_TARGET_NAME).apply {
|
||||
disambiguationClassifier = targetDisambiguationClassifier
|
||||
}
|
||||
|
||||
(project.kotlinExtension as Kotlin2JsProjectExtension).target = target
|
||||
super.apply(project)
|
||||
|
||||
+1
-1
@@ -359,7 +359,7 @@ open class KotlinTargetConfigurator<KotlinCompilationType : KotlinCompilation<*>
|
||||
val testCompilation = target.compilations.findByName(KotlinCompilation.TEST_COMPILATION_NAME) as? KotlinCompilationToRunnableFiles<*>
|
||||
?: return // Otherwise, there is no runtime classpath
|
||||
|
||||
target.project.tasks.create(lowerCamelCaseName(target.targetName, testTaskNameSuffix), Test::class.java).apply {
|
||||
target.project.tasks.create(lowerCamelCaseName(target.disambiguationClassifier, testTaskNameSuffix), Test::class.java).apply {
|
||||
project.afterEvaluate {
|
||||
// use afterEvaluate to override the JavaPlugin defaults for Test tasks
|
||||
conventionMapping.map("testClassesDirs") { testCompilation.output.classesDirs }
|
||||
|
||||
+3
-3
@@ -33,17 +33,17 @@ internal abstract class TaskToFriendTaskMapper {
|
||||
sealed internal class RegexTaskToFriendTaskMapper(
|
||||
private val prefix: String,
|
||||
suffix: String,
|
||||
private val targetName: String,
|
||||
private val targetDisambiguationClasssifier: String,
|
||||
private val postfixReplacement: String
|
||||
) : TaskToFriendTaskMapper() {
|
||||
class Default(targetName: String) : RegexTaskToFriendTaskMapper("compile", "TestKotlin", targetName, "Kotlin")
|
||||
class Android(targetName: String) : RegexTaskToFriendTaskMapper("compile", "(Unit|Android)TestKotlin", targetName, "Kotlin")
|
||||
|
||||
private val regex = "$prefix(.*)$suffix${targetName.capitalize()}".toRegex()
|
||||
private val regex = "$prefix(.*)$suffix${targetDisambiguationClasssifier.capitalize()}".toRegex()
|
||||
|
||||
override fun getFriendByName(name: String): String? {
|
||||
val match = regex.matchEntire(name) ?: return null
|
||||
val variant = match.groups[1]?.value ?: ""
|
||||
return prefix + variant + postfixReplacement + targetName.capitalize()
|
||||
return prefix + variant + postfixReplacement + targetDisambiguationClasssifier.capitalize()
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -23,13 +23,19 @@ import java.util.concurrent.Callable
|
||||
|
||||
internal fun KotlinCompilation<*>.composeName(prefix: String? = null, suffix: String? = null): String {
|
||||
val compilationNamePart = compilationName.takeIf { it != KotlinCompilation.MAIN_COMPILATION_NAME }
|
||||
val targetNamePart = target.disambiguationClassifier
|
||||
val targetNamePart =
|
||||
if (target is KotlinWithJavaTarget<*>)
|
||||
"" // no name disambiguation is needed for single-platform projects
|
||||
else target.disambiguationClassifier
|
||||
|
||||
return lowerCamelCaseName(prefix, targetNamePart, compilationNamePart, suffix)
|
||||
}
|
||||
|
||||
internal val KotlinCompilation<*>.defaultSourceSetName: String
|
||||
get() = lowerCamelCaseName(target.disambiguationClassifier, compilationName)
|
||||
get() = when (this) {
|
||||
is KotlinWithJavaCompilation<*> -> compilationName // no name disambiguation is needed for single-platform projects
|
||||
else -> lowerCamelCaseName(target.disambiguationClassifier, compilationName)
|
||||
}
|
||||
|
||||
abstract class AbstractKotlinCompilation<T : KotlinCommonOptions>(
|
||||
target: KotlinTarget,
|
||||
@@ -145,7 +151,7 @@ abstract class AbstractKotlinCompilation<T : KotlinCommonOptions>(
|
||||
"compile",
|
||||
compilationName.takeIf { it != KotlinCompilation.MAIN_COMPILATION_NAME },
|
||||
"Kotlin",
|
||||
target.targetName
|
||||
target.disambiguationClassifier
|
||||
)
|
||||
|
||||
override val compileAllTaskName: String
|
||||
|
||||
+1
-1
@@ -240,7 +240,7 @@ abstract class AbstractKotlinTarget(
|
||||
}
|
||||
|
||||
internal fun KotlinTarget.disambiguateName(simpleName: String) =
|
||||
lowerCamelCaseName(targetName, simpleName)
|
||||
lowerCamelCaseName(disambiguationClassifier, simpleName)
|
||||
|
||||
internal fun javaApiUsageForMavenScoping() =
|
||||
if (isGradleVersionAtLeast(5, 3)) {
|
||||
|
||||
+2
-2
@@ -39,7 +39,7 @@ internal fun <T : Task> registerTask(project: Project, name: String, type: Class
|
||||
}
|
||||
}
|
||||
|
||||
internal open class KotlinTasksProvider(val targetName: String) {
|
||||
internal open class KotlinTasksProvider(targetDisambiguationClassifier: String) {
|
||||
open fun registerKotlinJVMTask(
|
||||
project: Project,
|
||||
name: String,
|
||||
@@ -109,7 +109,7 @@ internal open class KotlinTasksProvider(val targetName: String) {
|
||||
}
|
||||
|
||||
protected open val taskToFriendTaskMapper: TaskToFriendTaskMapper =
|
||||
RegexTaskToFriendTaskMapper.Default(targetName)
|
||||
RegexTaskToFriendTaskMapper.Default(targetDisambiguationClassifier)
|
||||
|
||||
private inline fun <reified Task, reified WorkersTask : Task> taskOrWorkersTask(properties: PropertiesProvider): Class<out Task> =
|
||||
if (properties.parallelTasksInProject != true) Task::class.java else WorkersTask::class.java
|
||||
|
||||
Reference in New Issue
Block a user