Populate the SourceSets with more relevant information.
This commit is contained in:
committed by
Sergey Igushkin
parent
f3482d1251
commit
1e37785de1
+109
-14
@@ -142,25 +142,120 @@ class KotlinProjectIT : BaseGradleIT() {
|
||||
"DEFAULT"
|
||||
)
|
||||
|
||||
val expectedSourceSetNames = listOf("release", "debug", "releaseUnitTest", "debugUnitTest", "debugAndroidTest")
|
||||
assertTrue(kotlinProject.expectedByDependencies.isEmpty())
|
||||
assertEquals(5, kotlinProject.sourceSets.size)
|
||||
|
||||
fun verifySourceSet(sourceSet: SourceSet) {
|
||||
// These are unused in Android projects so will be empty.
|
||||
assertEquals(0, sourceSet.sourceDirectories.size)
|
||||
assertEquals(0, sourceSet.resourcesDirectories.size)
|
||||
assertEquals(project.projectDir.resolve("build/tmp/kotlin-classes/${sourceSet.name}"), sourceSet.classesOutputDirectory)
|
||||
// This value is just a placeholder since this information is obtained from the Android Plugin models.
|
||||
assertEquals(project.projectDir.resolve("build/tmp/kotlin-classes/${sourceSet.name}"), sourceSet.resourcesOutputDirectory)
|
||||
assertNotEquals(0, sourceSet.compilerArguments.currentArguments.size)
|
||||
assertNotEquals(0, sourceSet.compilerArguments.defaultArguments.size)
|
||||
val sourceSets = kotlinProject.sourceSets.sortedBy { it.name }
|
||||
|
||||
fun SourceSet.verifySourceSet(
|
||||
name: String, type: SourceSet.SourceSetType, friends: List<String>, sources: List<String>, resources: List<String>,
|
||||
classesOutputDir: String, resourcesOutputDir: String
|
||||
) {
|
||||
assertEquals(name, name)
|
||||
assertEquals(type, this.type)
|
||||
|
||||
assertEquals(friends.size, friendSourceSets.size)
|
||||
assertEquals(friends, friendSourceSets)
|
||||
|
||||
assertEquals(sources.size, sourceDirectories.size)
|
||||
assertEquals(sources.map { project.projectDir.resolve(it) }, sourceDirectories)
|
||||
|
||||
assertEquals(resources.size, resourcesDirectories.size)
|
||||
assertEquals(resources.map { project.projectDir.resolve(it) }, resourcesDirectories)
|
||||
|
||||
assertEquals(project.projectDir.resolve(classesOutputDir), classesOutputDirectory)
|
||||
assertEquals(project.projectDir.resolve(resourcesOutputDir), resourcesOutputDirectory)
|
||||
|
||||
assertNotEquals(0, compilerArguments.currentArguments.size)
|
||||
assertNotEquals(0, compilerArguments.defaultArguments.size)
|
||||
}
|
||||
|
||||
assertEquals(0, kotlinProject.sourceSets.filter {
|
||||
verifySourceSet(it)
|
||||
expectedSourceSetNames.contains(it.name)
|
||||
}.size)
|
||||
sourceSets[0].verifySourceSet(
|
||||
"debug",
|
||||
SourceSet.SourceSetType.PRODUCTION,
|
||||
listOf(),
|
||||
listOf(
|
||||
"app/src/debug/kotlin",
|
||||
"app/src/debug/java",
|
||||
"app/src/main/kotlin",
|
||||
"app/src/main/java"
|
||||
),
|
||||
listOf(
|
||||
"app/src/debug/resources",
|
||||
"app/src/main/resources"
|
||||
),
|
||||
"app/build/tmp/kotlin-classes/debug", "app/build/processedResources/debug"
|
||||
)
|
||||
sourceSets[1].verifySourceSet(
|
||||
"debugAndroidTest",
|
||||
SourceSet.SourceSetType.TEST,
|
||||
listOf("debug"),
|
||||
listOf(
|
||||
"app/src/debugAndroidTest/kotlin",
|
||||
"app/src/androidTest/kotlin",
|
||||
"app/src/androidTest/java",
|
||||
"app/src/androidTestDebug/kotlin",
|
||||
"app/src/androidTestDebug/java"
|
||||
),
|
||||
listOf(
|
||||
"app/src/debugAndroidTest/resources",
|
||||
"app/src/androidTest/resources",
|
||||
"app/src/androidTestDebug/resources"
|
||||
),
|
||||
"app/build/tmp/kotlin-classes/debugAndroidTest", "app/build/processedResources/debugAndroidTest"
|
||||
)
|
||||
sourceSets[2].verifySourceSet(
|
||||
"debugUnitTest",
|
||||
SourceSet.SourceSetType.TEST,
|
||||
listOf("debug"),
|
||||
listOf(
|
||||
"app/src/debugUnitTest/kotlin",
|
||||
"app/src/test/kotlin",
|
||||
"app/src/test/java",
|
||||
"app/src/testDebug/kotlin",
|
||||
"app/src/testDebug/java"
|
||||
),
|
||||
listOf(
|
||||
"app/src/debugUnitTest/resources",
|
||||
"app/src/test/resources",
|
||||
"app/src/testDebug/resources"
|
||||
),
|
||||
"app/build/tmp/kotlin-classes/debugUnitTest", "app/build/processedResources/debugUnitTest"
|
||||
)
|
||||
sourceSets[3].verifySourceSet(
|
||||
"release",
|
||||
SourceSet.SourceSetType.PRODUCTION,
|
||||
listOf(),
|
||||
listOf(
|
||||
"app/src/release/kotlin",
|
||||
"app/src/release/java",
|
||||
"app/src/main/kotlin",
|
||||
"app/src/main/java"
|
||||
),
|
||||
listOf(
|
||||
"app/src/release/resources",
|
||||
"app/src/main/resources"
|
||||
),
|
||||
"app/build/tmp/kotlin-classes/release", "app/build/processedResources/release"
|
||||
)
|
||||
sourceSets[4].verifySourceSet(
|
||||
"releaseUnitTest",
|
||||
SourceSet.SourceSetType.TEST,
|
||||
listOf("release"),
|
||||
listOf(
|
||||
"app/src/releaseUnitTest/kotlin",
|
||||
"app/src/test/kotlin",
|
||||
"app/src/test/java",
|
||||
"app/src/testRelease/kotlin",
|
||||
"app/src/testRelease/java"
|
||||
),
|
||||
listOf(
|
||||
"app/src/releaseUnitTest/resources",
|
||||
"app/src/test/resources",
|
||||
"app/src/testRelease/resources"
|
||||
),
|
||||
"app/build/tmp/kotlin-classes/releaseUnitTest", "app/build/processedResources/releaseUnitTest"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+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