Kotlin Facet: Detect module platform by gradle plugin

#KT-16703 Fixed
 #KT-16342 Fixed
This commit is contained in:
Alexey Sedunov
2017-03-07 16:35:17 +03:00
parent 26537cd8fc
commit c5ee28da05
4 changed files with 104 additions and 12 deletions
@@ -33,19 +33,22 @@ interface KotlinGradleModel : Serializable {
val currentCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet val currentCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet
val defaultCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet val defaultCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet
val coroutines: String? val coroutines: String?
val platformPluginId: String?
} }
class KotlinGradleModelImpl( class KotlinGradleModelImpl(
override val implements: String?, override val implements: String?,
override val currentCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet, override val currentCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet,
override val defaultCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet, override val defaultCompilerArgumentsBySourceSet: CompilerArgumentsBySourceSet,
override val coroutines: String? override val coroutines: String?,
override val platformPluginId: String?
) : KotlinGradleModel ) : KotlinGradleModel
class KotlinGradleModelBuilder : ModelBuilderService { class KotlinGradleModelBuilder : ModelBuilderService {
companion object { companion object {
val kotlinCompileTaskClasses = listOf("org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated", val kotlinCompileTaskClasses = listOf("org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated",
"org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile_Decorated") "org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile_Decorated")
val platformPluginIds = listOf("kotlin-platform-jvm", "kotlin-platform-js", "kotlin-platform-common")
} }
override fun getErrorMessageBuilder(project: Project, e: Exception): ErrorMessageBuilder { override fun getErrorMessageBuilder(project: Project, e: Exception): ErrorMessageBuilder {
@@ -127,7 +130,8 @@ class KotlinGradleModelBuilder : ModelBuilderService {
getImplements(project), getImplements(project),
currentCompilerArgumentsBySourceSet, currentCompilerArgumentsBySourceSet,
defaultCompilerArgumentsBySourceSet, defaultCompilerArgumentsBySourceSet,
getCoroutines(project) getCoroutines(project),
platformPluginIds.singleOrNull { project.plugins.findPlugin(it) != null }
) )
} }
} }
@@ -40,6 +40,8 @@ var DataNode<ModuleData>.defaultCompilerArgumentsBySourceSet
by UserDataProperty(Key.create<CompilerArgumentsBySourceSet>("DEFAULT_COMPILER_ARGUMENTS")) by UserDataProperty(Key.create<CompilerArgumentsBySourceSet>("DEFAULT_COMPILER_ARGUMENTS"))
var DataNode<ModuleData>.coroutines var DataNode<ModuleData>.coroutines
by UserDataProperty(Key.create<String>("KOTLIN_COROUTINES")) by UserDataProperty(Key.create<String>("KOTLIN_COROUTINES"))
var DataNode<ModuleData>.platformPluginId
by UserDataProperty(Key.create<String>("PLATFORM_PLUGIN_ID"))
class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension() { class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension() {
override fun getToolingExtensionsClasses(): Set<Class<out Any>> { override fun getToolingExtensionsClasses(): Set<Class<out Any>> {
@@ -68,6 +70,7 @@ class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension()
ideModule.currentCompilerArgumentsBySourceSet = gradleModel.currentCompilerArgumentsBySourceSet ideModule.currentCompilerArgumentsBySourceSet = gradleModel.currentCompilerArgumentsBySourceSet
ideModule.defaultCompilerArgumentsBySourceSet = gradleModel.defaultCompilerArgumentsBySourceSet ideModule.defaultCompilerArgumentsBySourceSet = gradleModel.defaultCompilerArgumentsBySourceSet
ideModule.coroutines = gradleModel.coroutines ideModule.coroutines = gradleModel.coroutines
ideModule.platformPluginId = gradleModel.platformPluginId
super.populateModuleDependencies(gradleModule, ideModule, ideProject) super.populateModuleDependencies(gradleModule, ideModule, ideProject)
} }
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.idea.inspections.gradle.findKotlinPluginVersion
import org.jetbrains.kotlin.idea.inspections.gradle.getResolvedKotlinStdlibVersionByModuleData import org.jetbrains.kotlin.idea.inspections.gradle.getResolvedKotlinStdlibVersionByModuleData
import org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData import org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
import org.jetbrains.plugins.gradle.service.project.data.ExternalProjectDataService
import java.util.* import java.util.*
interface GradleProjectImportHandler { interface GradleProjectImportHandler {
@@ -90,15 +89,11 @@ class KotlinGradleProjectDataService : AbstractProjectDataService<ModuleData, Vo
} }
private fun detectPlatformByPlugin(moduleNode: DataNode<ModuleData>): TargetPlatformKind<*>? { private fun detectPlatformByPlugin(moduleNode: DataNode<ModuleData>): TargetPlatformKind<*>? {
val projectNode = ExternalSystemApiUtil.findParent(moduleNode, ProjectKeys.PROJECT) return when (moduleNode.platformPluginId) {
val externalProjectNode = ExternalSystemApiUtil.find(projectNode as DataNode<*>, ExternalProjectDataService.KEY) "kotlin-platform-jvm" -> TargetPlatformKind.Jvm[JvmTarget.JVM_1_6]
return externalProjectNode?.let { "kotlin-platform-js" -> TargetPlatformKind.JavaScript
when (it.data.plugins.values.map { it.id }.firstOrNull { it.startsWith("kotlin-platform-") }) { "kotlin-platform-common" -> TargetPlatformKind.Common
"kotlin-platform-jvm" -> TargetPlatformKind.Jvm[JvmTarget.JVM_1_6] else -> null
"kotlin-platform-js" -> TargetPlatformKind.JavaScript
"kotlin-platform-common" -> TargetPlatformKind.Common
else -> null
}
} }
} }
@@ -481,4 +481,94 @@ class GradleFacetImportTest : GradleImportingTestCase() {
Assert.assertEquals(TargetPlatformKind.Common, targetPlatformKind) Assert.assertEquals(TargetPlatformKind.Common, targetPlatformKind)
} }
} }
@Test
fun testJvmImportByPlatformPlugin() {
createProjectSubFile("build.gradle", """
group 'Again'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
maven {
url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1'
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
}
}
apply plugin: 'kotlin-platform-jvm'
""")
importProject()
with (facetSettings) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.1", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Jvm[JvmTarget.JVM_1_6], targetPlatformKind)
}
}
@Test
fun testJsImportByPlatformPlugin() {
createProjectSubFile("build.gradle", """
group 'Again'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
maven {
url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1'
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
}
}
apply plugin: 'kotlin-platform-js'
""")
importProject()
with (facetSettings) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.1", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.JavaScript, targetPlatformKind)
}
}
@Test
fun testCommonImportByPlatformPlugin() {
createProjectSubFile("build.gradle", """
group 'Again'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
maven {
url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1'
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
}
}
apply plugin: 'kotlin-platform-common'
""")
importProject()
with (facetSettings) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.1", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Common, targetPlatformKind)
}
}
} }