Detect platform only for stdlib and use only roots for detection (KT-30442)

#KT-30442 Fixed
This commit is contained in:
Nikolay Krasko
2019-04-12 11:56:35 +03:00
parent bfb13803d5
commit 04eeabcd98
@@ -50,13 +50,11 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv
JavaResourceRootType.TEST_RESOURCE JavaResourceRootType.TEST_RESOURCE
) )
private val TargetPlatform.stdlibDetector: ((Array<VirtualFile>) -> Boolean)? private val PLATFORM_TO_STDLIB_DETECTORS: Map<TargetPlatform, (Array<VirtualFile>) -> Boolean> = mapOf(
get() = when (this) { JvmPlatform to { roots: Array<VirtualFile> -> JavaRuntimeDetectionUtil.getRuntimeJar(roots.toList()) != null },
is JvmPlatform -> { roots -> JavaRuntimeDetectionUtil.getRuntimeJar(roots.toList()) != null } JsPlatform to { roots: Array<VirtualFile> -> JsLibraryStdDetectionUtil.getJsStdLibJar(roots.toList()) != null },
is JsPlatform -> { roots -> JsLibraryStdDetectionUtil.getJsStdLibJar(roots.toList()) != null } CommonPlatform to { roots: Array<VirtualFile> -> getLibraryJar(roots, PathUtil.KOTLIN_STDLIB_COMMON_JAR_PATTERN) != null }
is CommonPlatform -> { roots -> getLibraryJar(roots, PathUtil.KOTLIN_STDLIB_COMMON_JAR_PATTERN) != null } )
else -> null
}
} }
sealed class LibInfo { sealed class LibInfo {
@@ -88,14 +86,16 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv
abstract val explicitKind: PersistentLibraryKind<*>? abstract val explicitKind: PersistentLibraryKind<*>?
abstract fun getRoots(): Array<VirtualFile> abstract fun getRoots(): Array<VirtualFile>
val platform by lazy { val stdlibPlatform: TargetPlatform? by lazy {
val explicitKind = explicitKind val roots = getRoots()
val kind = if (explicitKind is KotlinLibraryKind) explicitKind else detectLibraryKind(getRoots()) for ((platform, detector) in PLATFORM_TO_STDLIB_DETECTORS) {
kind?.platform ?: JvmPlatform if (detector.invoke(roots)) {
} return@lazy platform
}
}
val isStdlib: Boolean return@lazy null
get() = platform.stdlibDetector?.invoke(getRoots()) ?: false }
} }
class ConverterImpl(private val context: ConversionContext) : ProjectConverter() { class ConverterImpl(private val context: ConversionContext) : ProjectConverter() {
@@ -144,15 +144,12 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv
.asSequence() .asSequence()
.mapNotNull { createLibInfo(it, this) } .mapNotNull { createLibInfo(it, this) }
.forEach { .forEach {
when (val platform = it.platform) { val stdlibPlatform = it.stdlibPlatform
is CommonPlatform -> { if (stdlibPlatform != null) {
if (!hasCommonStdlib && it.isStdlib) { if (stdlibPlatform == CommonPlatform) {
hasCommonStdlib = true hasCommonStdlib = true
} } else {
} return stdlibPlatform
else -> {
if (it.isStdlib) return platform
} }
} }
} }
@@ -173,6 +170,7 @@ class KotlinNonJvmSourceRootConverterProvider : ConverterProvider("kotlin-non-jv
.flatMap { it.getChildren(SourceFolderImpl.ELEMENT_NAME) } .flatMap { it.getChildren(SourceFolderImpl.ELEMENT_NAME) }
} }
@Suppress("UnstableApiUsage")
private fun ModuleSettings.isExternalModule(): Boolean { private fun ModuleSettings.isExternalModule(): Boolean {
return when { return when {
rootElement.getAttributeValue(ExternalProjectSystemRegistry.EXTERNAL_SYSTEM_ID_KEY) != null -> true rootElement.getAttributeValue(ExternalProjectSystemRegistry.EXTERNAL_SYSTEM_ID_KEY) != null -> true