Create source sets for generated by kapt sources during import
#KT-30739 Fixed
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle
|
||||
|
||||
import com.intellij.openapi.roots.DependencyScope
|
||||
import org.jetbrains.jps.model.java.JavaResourceRootType
|
||||
import org.jetbrains.jps.model.java.JavaSourceRootType
|
||||
import org.jetbrains.kotlin.idea.codeInsight.gradle.MultiplePluginVersionGradleImportingTestCase
|
||||
import org.junit.Test
|
||||
|
||||
class KaptImportingTest : MultiplePluginVersionGradleImportingTestCase() {
|
||||
|
||||
override fun isApplicableTest(): Boolean {
|
||||
return !gradleVersion.startsWith("3")
|
||||
}
|
||||
|
||||
fun importProject(modulePerSourceSet: Boolean) {
|
||||
currentExternalProjectSettings.isResolveModulePerSourceSet = modulePerSourceSet
|
||||
val isCreateEmptyContentRootDirectories = currentExternalProjectSettings.isCreateEmptyContentRootDirectories
|
||||
currentExternalProjectSettings.isCreateEmptyContentRootDirectories = true
|
||||
try {
|
||||
super.importProject()
|
||||
} finally {
|
||||
currentExternalProjectSettings.isCreateEmptyContentRootDirectories = isCreateEmptyContentRootDirectories
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testModulePerSourceSet() {
|
||||
configureByFiles()
|
||||
importProject(true)
|
||||
|
||||
checkProjectStructure(myProject, projectPath, true, true, true) {
|
||||
module("project")
|
||||
module("project_main") {
|
||||
sourceFolder("build/generated/source/kapt/main", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("build/generated/source/kaptKotlin/main", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/java", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/kotlin", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/resources", JavaResourceRootType.RESOURCE)
|
||||
libraryDependency("Gradle: kaptGeneratedClasses", DependencyScope.COMPILE)
|
||||
}
|
||||
module("project_test") {
|
||||
sourceFolder("build/generated/source/kapt/test", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("build/generated/source/kaptKotlin/test", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/java", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/kotlin", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/resources", JavaResourceRootType.TEST_RESOURCE)
|
||||
moduleDependency("project_main", DependencyScope.COMPILE)
|
||||
libraryDependency("Gradle: kaptGeneratedClasses", DependencyScope.COMPILE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testModulePerSourceSetDisabled() {
|
||||
configureByFiles()
|
||||
importProject(false)
|
||||
|
||||
checkProjectStructure(myProject, projectPath, true, true, true) {
|
||||
module("project") {
|
||||
sourceFolder("build/generated/source/kapt/main", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("build/generated/source/kaptKotlin/main", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/java", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/kotlin", JavaSourceRootType.SOURCE)
|
||||
sourceFolder("src/main/resources", JavaResourceRootType.RESOURCE)
|
||||
sourceFolder("build/generated/source/kapt/test", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("build/generated/source/kaptKotlin/test", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/java", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/kotlin", JavaSourceRootType.TEST_SOURCE)
|
||||
sourceFolder("src/test/resources", JavaResourceRootType.TEST_RESOURCE)
|
||||
libraryDependency("Gradle: kaptGeneratedClasses", DependencyScope.COMPILE)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
override fun testDataDirName(): String {
|
||||
return "kaptImportingTest"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
buildscript {
|
||||
apply from: "include.gradle"
|
||||
ext.kotlin_version = gradleKotlinPluginVersion('1.3.10')
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.2.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
buildscript {
|
||||
apply from: "include.gradle"
|
||||
ext.kotlin_version = gradleKotlinPluginVersion('1.3.10')
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.2.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
+17
-9
@@ -79,23 +79,31 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
|
||||
if (kaptModel != null && kaptModel.isEnabled) {
|
||||
for (sourceSet in kaptModel.sourceSets) {
|
||||
val sourceSetDataNode = ideModule.findGradleSourceSet(sourceSet.sourceSetName) ?: continue
|
||||
val parentDataNode = ideModule.findParentForSourceSetDataNode(sourceSet.sourceSetName) ?: continue
|
||||
|
||||
fun addSourceSet(path: String, type: ExternalSystemSourceType) {
|
||||
val contentRootData = ContentRootData(GRADLE_SYSTEM_ID, path)
|
||||
contentRootData.storePath(type, path)
|
||||
sourceSetDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
parentDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
}
|
||||
|
||||
val sourceType = if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
val sourceType =
|
||||
if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
sourceSet.generatedSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
sourceSet.generatedKotlinSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
|
||||
sourceSet.generatedClassesDirFile?.let { generatedClassesDir ->
|
||||
val libraryData = LibraryData(GRADLE_SYSTEM_ID, "kaptGeneratedClasses")
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(sourceSetDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
sourceSetDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
val existingNode =
|
||||
parentDataNode.children.map { (it.data as? LibraryDependencyData)?.target }
|
||||
.firstOrNull { it?.externalName == libraryData.externalName }
|
||||
if (existingNode != null) {
|
||||
existingNode.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
} else {
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(parentDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
parentDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,17 +111,17 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
|
||||
private fun DataNode<ModuleData>.findGradleSourceSet(sourceSetName: String): DataNode<GradleSourceSetData>? {
|
||||
private fun DataNode<ModuleData>.findParentForSourceSetDataNode(sourceSetName: String): DataNode<ModuleData>? {
|
||||
val moduleName = data.id
|
||||
for (child in children) {
|
||||
val gradleSourceSetData = child.data as? GradleSourceSetData ?: continue
|
||||
if (gradleSourceSetData.id == "$moduleName:$sourceSetName") {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return child as DataNode<GradleSourceSetData>?
|
||||
return child as? DataNode<ModuleData>
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-9
@@ -79,23 +79,31 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
|
||||
if (kaptModel != null && kaptModel.isEnabled) {
|
||||
for (sourceSet in kaptModel.sourceSets) {
|
||||
val sourceSetDataNode = ideModule.findGradleSourceSet(sourceSet.sourceSetName) ?: continue
|
||||
val parentDataNode = ideModule.findParentForSourceSetDataNode(sourceSet.sourceSetName) ?: continue
|
||||
|
||||
fun addSourceSet(path: String, type: ExternalSystemSourceType) {
|
||||
val contentRootData = ContentRootData(GRADLE_SYSTEM_ID, path)
|
||||
contentRootData.storePath(type, path)
|
||||
sourceSetDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
parentDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
}
|
||||
|
||||
val sourceType = if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
val sourceType =
|
||||
if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
sourceSet.generatedSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
sourceSet.generatedKotlinSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
|
||||
sourceSet.generatedClassesDirFile?.let { generatedClassesDir ->
|
||||
val libraryData = LibraryData(GRADLE_SYSTEM_ID, "kaptGeneratedClasses")
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(sourceSetDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
sourceSetDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
val existingNode =
|
||||
parentDataNode.children.map { (it.data as? LibraryDependencyData)?.target }
|
||||
.firstOrNull { it?.externalName == libraryData.externalName }
|
||||
if (existingNode != null) {
|
||||
existingNode.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
} else {
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(parentDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
parentDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,17 +111,17 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
|
||||
private fun DataNode<ModuleData>.findGradleSourceSet(sourceSetName: String): DataNode<GradleSourceSetData>? {
|
||||
private fun DataNode<ModuleData>.findParentForSourceSetDataNode(sourceSetName: String): DataNode<ModuleData>? {
|
||||
val moduleName = data.id
|
||||
for (child in children) {
|
||||
val gradleSourceSetData = child.data as? GradleSourceSetData ?: continue
|
||||
if (gradleSourceSetData.id == "$moduleName:$sourceSetName") {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return child as DataNode<GradleSourceSetData>?
|
||||
return child as? DataNode<ModuleData>
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-9
@@ -79,23 +79,31 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
|
||||
if (kaptModel != null && kaptModel.isEnabled) {
|
||||
for (sourceSet in kaptModel.sourceSets) {
|
||||
val sourceSetDataNode = ideModule.findGradleSourceSet(sourceSet.sourceSetName) ?: continue
|
||||
val parentDataNode = ideModule.findParentForSourceSetDataNode(sourceSet.sourceSetName) ?: continue
|
||||
|
||||
fun addSourceSet(path: String, type: ExternalSystemSourceType) {
|
||||
val contentRootData = ContentRootData(GRADLE_SYSTEM_ID, path)
|
||||
contentRootData.storePath(type, path)
|
||||
sourceSetDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
parentDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
}
|
||||
|
||||
val sourceType = if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
val sourceType =
|
||||
if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
sourceSet.generatedSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
sourceSet.generatedKotlinSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
|
||||
sourceSet.generatedClassesDirFile?.let { generatedClassesDir ->
|
||||
val libraryData = LibraryData(GRADLE_SYSTEM_ID, "kaptGeneratedClasses")
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(sourceSetDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
sourceSetDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
val existingNode =
|
||||
parentDataNode.children.map { (it.data as? LibraryDependencyData)?.target }
|
||||
.firstOrNull { it?.externalName == libraryData.externalName }
|
||||
if (existingNode != null) {
|
||||
existingNode.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
} else {
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(parentDataNode.data, libraryData, LibraryLevel.MODULE)
|
||||
parentDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,17 +111,17 @@ class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
|
||||
private fun DataNode<ModuleData>.findGradleSourceSet(sourceSetName: String): DataNode<GradleSourceSetData>? {
|
||||
private fun DataNode<ModuleData>.findParentForSourceSetDataNode(sourceSetName: String): DataNode<ModuleData>? {
|
||||
val moduleName = data.id
|
||||
for (child in children) {
|
||||
val gradleSourceSetData = child.data as? GradleSourceSetData ?: continue
|
||||
if (gradleSourceSetData.id == "$moduleName:$sourceSetName") {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return child as DataNode<GradleSourceSetData>?
|
||||
return child as? DataNode<ModuleData>
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user