Kotlin Facet: Fix platform detection in android-gradle projects

Sort candidate library names by descending length.
Suppress common library if platform-specific one is also present

 #KT-16827 Fixed
This commit is contained in:
Alexey Sedunov
2017-03-29 17:25:49 +03:00
parent 7a53b2f4c8
commit a8a9c3bbf4
4 changed files with 15 additions and 4 deletions
@@ -44,7 +44,10 @@ class PlatformAndroidGradleDetector : KotlinPlatformGradleDetector {
.flatMap { it.data.jarLibraryDependencies.asSequence() }
.forEach {
val libraryName = it.name
if (libraryName.substringBeforeLast("-") in libraryIds) return libraryName.substringAfterLast("-")
for (libraryId in libraryIds) {
val prefix = "$libraryId-"
if (libraryName.startsWith(prefix)) return libraryName.substringAfter(prefix)
}
}
return null
}
@@ -99,7 +99,8 @@ private fun detectPlatformByPlugin(moduleNode: DataNode<ModuleData>): TargetPlat
}
private fun detectPlatformByLibrary(moduleNode: DataNode<ModuleData>): TargetPlatformKind<*>? {
return TargetPlatformKind.ALL_PLATFORMS.firstOrNull { moduleNode.getResolvedKotlinStdlibVersionByModuleData(it.mavenLibraryIds) != null }
val detectedPlatforms = mavenLibraryIdToPlatform.entries.filter { moduleNode.getResolvedKotlinStdlibVersionByModuleData(listOf(it.key)) != null }.map { it.value }.distinct()
return detectedPlatforms.singleOrNull() ?: detectedPlatforms.firstOrNull { it != TargetPlatformKind.Common }
}
private fun configureFacetByGradleModule(
@@ -101,6 +101,13 @@ val TargetPlatformKind<*>.mavenLibraryIds: List<String>
is TargetPlatformKind.Common -> listOf(MAVEN_COMMON_STDLIB_ID)
}
val mavenLibraryIdToPlatform: Map<String, TargetPlatformKind<*>> by lazy {
TargetPlatformKind.ALL_PLATFORMS
.flatMap { platform -> platform.mavenLibraryIds.map { it to platform } }
.sortedByDescending { it.first.length }
.toMap()
}
fun Module.getOrCreateFacet(modelsProvider: IdeModifiableModelsProvider, useProjectSettings: Boolean): KotlinFacet {
val facetModel = modelsProvider.getModifiableFacetModel(this)
@@ -91,12 +91,12 @@ class GradleFacetImportTest_3_3 : GradleImportingTestCase() {
repositories {
mavenCentral()
maven {
url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1'
url 'http://dl.bintray.com/kotlin/kotlin-dev'
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-eap-44")
}
}