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"
|
"DEFAULT"
|
||||||
)
|
)
|
||||||
|
|
||||||
val expectedSourceSetNames = listOf("release", "debug", "releaseUnitTest", "debugUnitTest", "debugAndroidTest")
|
|
||||||
assertTrue(kotlinProject.expectedByDependencies.isEmpty())
|
assertTrue(kotlinProject.expectedByDependencies.isEmpty())
|
||||||
assertEquals(5, kotlinProject.sourceSets.size)
|
assertEquals(5, kotlinProject.sourceSets.size)
|
||||||
|
|
||||||
fun verifySourceSet(sourceSet: SourceSet) {
|
val sourceSets = kotlinProject.sourceSets.sortedBy { it.name }
|
||||||
// These are unused in Android projects so will be empty.
|
|
||||||
assertEquals(0, sourceSet.sourceDirectories.size)
|
fun SourceSet.verifySourceSet(
|
||||||
assertEquals(0, sourceSet.resourcesDirectories.size)
|
name: String, type: SourceSet.SourceSetType, friends: List<String>, sources: List<String>, resources: List<String>,
|
||||||
assertEquals(project.projectDir.resolve("build/tmp/kotlin-classes/${sourceSet.name}"), sourceSet.classesOutputDirectory)
|
classesOutputDir: String, resourcesOutputDir: String
|
||||||
// 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)
|
assertEquals(name, name)
|
||||||
assertNotEquals(0, sourceSet.compilerArguments.currentArguments.size)
|
assertEquals(type, this.type)
|
||||||
assertNotEquals(0, sourceSet.compilerArguments.defaultArguments.size)
|
|
||||||
|
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 {
|
sourceSets[0].verifySourceSet(
|
||||||
verifySourceSet(it)
|
"debug",
|
||||||
expectedSourceSetNames.contains(it.name)
|
SourceSet.SourceSetType.PRODUCTION,
|
||||||
}.size)
|
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
|
@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.KOTLIN_JS_DSL_NAME
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
||||||
import org.jetbrains.kotlin.gradle.plugin.getConvention
|
import org.jetbrains.kotlin.gradle.plugin.getConvention
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
|
||||||
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [ToolingModelBuilder] for [KotlinProject] models.
|
* [ToolingModelBuilder] for [KotlinProject] models.
|
||||||
* This model builder is registered for base Kotlin JVM (including Android), Kotlin JS and Kotlin Common plugins.
|
* 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 {
|
override fun canBuild(modelName: String): Boolean {
|
||||||
return modelName == KotlinProject::class.java.name
|
return modelName == KotlinProject::class.java.name
|
||||||
@@ -43,7 +46,7 @@ class KotlinModelBuilder(private val kotlinPluginVersion: String) : ToolingModel
|
|||||||
kotlinPluginVersion,
|
kotlinPluginVersion,
|
||||||
projectType,
|
projectType,
|
||||||
kotlinCompileTasks.mapNotNull {
|
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),
|
getExpectedByDependencies(project),
|
||||||
kotlinCompileTasks.first()!!.createExperimentalFeatures()
|
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 Gradle plugin and as such is not populated here. The important information here that is required by
|
||||||
* Android Studio are the [CompilerArguments].
|
* 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(
|
return SourceSetImpl(
|
||||||
sourceSetName,
|
sourceSetName,
|
||||||
if (sourceSetName.contains("test", true)) SourceSet.SourceSetType.TEST else SourceSet.SourceSetType.PRODUCTION,
|
if (sourceSetName.contains("test", true)) SourceSet.SourceSetType.TEST else SourceSet.SourceSetType.PRODUCTION,
|
||||||
findFriendSourceSets(),
|
findFriendSourceSets(),
|
||||||
emptyList(), // Obtained from the Android model
|
sources,
|
||||||
emptyList(), // Obtained from the Android model
|
resources,
|
||||||
destinationDir,
|
destinationDir,
|
||||||
destinationDir, // Obtained from the Android model
|
compilation.output.resourcesDir,
|
||||||
createCompilerArguments()
|
createCompilerArguments()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -352,7 +352,7 @@ internal abstract class AbstractKotlinPlugin(
|
|||||||
|
|
||||||
configureAttributes(target)
|
configureAttributes(target)
|
||||||
configureProjectGlobalSettings(project, kotlinPluginVersion)
|
configureProjectGlobalSettings(project, kotlinPluginVersion)
|
||||||
registry.register(KotlinModelBuilder(kotlinPluginVersion))
|
registry.register(KotlinModelBuilder(kotlinPluginVersion, null))
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -571,7 +571,7 @@ internal open class KotlinAndroidPlugin(
|
|||||||
project, androidTarget, tasksProvider,
|
project, androidTarget, tasksProvider,
|
||||||
kotlinPluginVersion
|
kotlinPluginVersion
|
||||||
)
|
)
|
||||||
registry.register(KotlinModelBuilder(kotlinPluginVersion))
|
registry.register(KotlinModelBuilder(kotlinPluginVersion, androidTarget))
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
+2
-1
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.gradle.model.builder
|
package org.jetbrains.kotlin.gradle.model.builder
|
||||||
|
|
||||||
import org.jetbrains.kotlin.gradle.model.KotlinProject
|
import org.jetbrains.kotlin.gradle.model.KotlinProject
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import kotlin.test.assertFalse
|
import kotlin.test.assertFalse
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
@@ -13,7 +14,7 @@ import kotlin.test.assertTrue
|
|||||||
class KotlinModelBuilderTest {
|
class KotlinModelBuilderTest {
|
||||||
@Test
|
@Test
|
||||||
fun testCanBuild() {
|
fun testCanBuild() {
|
||||||
val modelBuilder = KotlinModelBuilder("version")
|
val modelBuilder = KotlinModelBuilder("version", null)
|
||||||
assertTrue(modelBuilder.canBuild(KotlinProject::class.java.name))
|
assertTrue(modelBuilder.canBuild(KotlinProject::class.java.name))
|
||||||
assertFalse(modelBuilder.canBuild("wrongModel"))
|
assertFalse(modelBuilder.canBuild("wrongModel"))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user