KPM, Model mapping: setup source paths for fragments by source set names

When a fragment is represented as a source set in the mapped model, the
source set name may differ from <fragmentName><moduleName>. For example,
some source set names don't end with *Main or *Test. In case we can
detect the source set that represents the fragment, use the
source set name for the fragment's sources directory.
This commit is contained in:
Sergey Igushkin
2022-02-01 01:14:57 +04:00
committed by Space
parent 3e8b27db4b
commit ab8284d5b8
@@ -5,7 +5,9 @@
package org.jetbrains.kotlin.gradle.plugin.mpp.pm20
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.sources.kpm.FragmentMappedKotlinSourceSet
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import java.io.File
@@ -14,16 +16,32 @@ interface KotlinSourceDirectoriesConfigurator<in T : KotlinGradleFragment>: Kotl
object DefaultKotlinSourceDirectoriesConfigurator : KotlinSourceDirectoriesConfigurator<KotlinGradleFragment> {
override fun configure(fragment: KotlinGradleFragment) {
fragment.kotlinSourceRoots.srcDir(
defaultSourceFolder(
project = fragment.project,
moduleName = fragment.containingModule.name,
fragmentName = fragment.fragmentName,
type = "kotlin"
)
fragment.project.provider {
defaultSourceFolder(
fragment,
type = "kotlin"
)
}
)
}
fun defaultSourceFolder(project: Project, moduleName: String, fragmentName: String, type: String): File {
return project.file("src/${lowerCamelCaseName(fragmentName, moduleName)}/$type")
private fun fragmentDirectoryName(fragment: KotlinGradleFragment): String {
val project = fragment.project
val isModelMappingEnabled = project.multiplatformExtensionOrNull != null
val kpmDefaultResult = lowerCamelCaseName(fragment.name, fragment.containingModule.name)
return if (!isModelMappingEnabled) {
kpmDefaultResult
} else {
val sourceSet =
project.kotlinExtension.sourceSets.find { it is FragmentMappedKotlinSourceSet && it.underlyingFragment == fragment }
if (sourceSet != null)
sourceSet.name
else kpmDefaultResult
}
}
fun defaultSourceFolder(fragment: KotlinGradleFragment, type: String): File {
val fragmentDirectoryName = fragmentDirectoryName(fragment)
return fragment.project.file("src/$fragmentDirectoryName/$type")
}
}