Populate the SourceSets with more relevant information.
This commit is contained in:
committed by
Sergey Igushkin
parent
f3482d1251
commit
1e37785de1
+18
-6
@@ -22,13 +22,16 @@ import org.jetbrains.kotlin.gradle.plugin.KOTLIN_DSL_NAME
|
||||
import org.jetbrains.kotlin.gradle.plugin.KOTLIN_JS_DSL_NAME
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
||||
import org.jetbrains.kotlin.gradle.plugin.getConvention
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
|
||||
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
|
||||
|
||||
/**
|
||||
* [ToolingModelBuilder] for [KotlinProject] models.
|
||||
* This model builder is registered for base Kotlin JVM (including Android), Kotlin JS and Kotlin Common plugins.
|
||||
*
|
||||
* [androidTarget] should always be null for none Android plugins.
|
||||
*/
|
||||
class KotlinModelBuilder(private val kotlinPluginVersion: String) : ToolingModelBuilder {
|
||||
class KotlinModelBuilder(private val kotlinPluginVersion: String, private val androidTarget: KotlinAndroidTarget?) : ToolingModelBuilder {
|
||||
|
||||
override fun canBuild(modelName: String): Boolean {
|
||||
return modelName == KotlinProject::class.java.name
|
||||
@@ -43,7 +46,7 @@ class KotlinModelBuilder(private val kotlinPluginVersion: String) : ToolingModel
|
||||
kotlinPluginVersion,
|
||||
projectType,
|
||||
kotlinCompileTasks.mapNotNull {
|
||||
if (project.isAndroid()) it.createAndroidSourceSet() else it.createSourceSet(project, projectType)
|
||||
if (androidTarget != null) it.createAndroidSourceSet(androidTarget) else it.createSourceSet(project, projectType)
|
||||
},
|
||||
getExpectedByDependencies(project),
|
||||
kotlinCompileTasks.first()!!.createExperimentalFeatures()
|
||||
@@ -103,15 +106,24 @@ class KotlinModelBuilder(private val kotlinPluginVersion: String) : ToolingModel
|
||||
* Android Gradle plugin and as such is not populated here. The important information here that is required by
|
||||
* Android Studio are the [CompilerArguments].
|
||||
*/
|
||||
private fun AbstractKotlinCompile<*>.createAndroidSourceSet(): SourceSet {
|
||||
private fun AbstractKotlinCompile<*>.createAndroidSourceSet(androidTarget: KotlinAndroidTarget): SourceSet {
|
||||
val variantName = sourceSetName
|
||||
val compilation = androidTarget.compilations.getByName(variantName)
|
||||
// Merge all sources and resource dirs from the different Source Sets that make up this variant.
|
||||
val sources = compilation.kotlinSourceSets.flatMap {
|
||||
it.kotlin.srcDirs
|
||||
}.distinctBy { it.absolutePath }
|
||||
val resources = compilation.kotlinSourceSets.flatMap {
|
||||
it.resources.srcDirs
|
||||
}.distinctBy { it.absolutePath }
|
||||
return SourceSetImpl(
|
||||
sourceSetName,
|
||||
if (sourceSetName.contains("test", true)) SourceSet.SourceSetType.TEST else SourceSet.SourceSetType.PRODUCTION,
|
||||
findFriendSourceSets(),
|
||||
emptyList(), // Obtained from the Android model
|
||||
emptyList(), // Obtained from the Android model
|
||||
sources,
|
||||
resources,
|
||||
destinationDir,
|
||||
destinationDir, // Obtained from the Android model
|
||||
compilation.output.resourcesDir,
|
||||
createCompilerArguments()
|
||||
)
|
||||
}
|
||||
|
||||
+2
-2
@@ -352,7 +352,7 @@ internal abstract class AbstractKotlinPlugin(
|
||||
|
||||
configureAttributes(target)
|
||||
configureProjectGlobalSettings(project, kotlinPluginVersion)
|
||||
registry.register(KotlinModelBuilder(kotlinPluginVersion))
|
||||
registry.register(KotlinModelBuilder(kotlinPluginVersion, null))
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -571,7 +571,7 @@ internal open class KotlinAndroidPlugin(
|
||||
project, androidTarget, tasksProvider,
|
||||
kotlinPluginVersion
|
||||
)
|
||||
registry.register(KotlinModelBuilder(kotlinPluginVersion))
|
||||
registry.register(KotlinModelBuilder(kotlinPluginVersion, androidTarget))
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.gradle.model.builder
|
||||
|
||||
import org.jetbrains.kotlin.gradle.model.KotlinProject
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
@@ -13,7 +14,7 @@ import kotlin.test.assertTrue
|
||||
class KotlinModelBuilderTest {
|
||||
@Test
|
||||
fun testCanBuild() {
|
||||
val modelBuilder = KotlinModelBuilder("version")
|
||||
val modelBuilder = KotlinModelBuilder("version", null)
|
||||
assertTrue(modelBuilder.canBuild(KotlinProject::class.java.name))
|
||||
assertFalse(modelBuilder.canBuild("wrongModel"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user