[Commonizer] Friendly displaying commonized KLIBs in IDE

Issue #MMPP-195
This commit is contained in:
Dmitriy Dolovov
2020-02-03 14:55:34 +07:00
parent 23e218396e
commit ec23c25286
5 changed files with 63 additions and 15 deletions
@@ -21,7 +21,8 @@ import com.intellij.util.PathUtilRt
import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.concurrency.CancellablePromise
import org.jetbrains.kotlin.idea.caches.project.getModuleInfosFromIdeaModel
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil.isGradleLibraryName
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil.parseIDELibraryName
import org.jetbrains.kotlin.idea.versions.UnsupportedAbiVersionNotificationPanelProvider
import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion
import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME
@@ -205,11 +206,11 @@ class KotlinNativeABICompatibilityChecker(private val project: Project) : Projec
private fun parseIDELibraryName(libraryInfo: NativeLibraryInfo): Pair<String, LibraryGroup> {
val ideLibraryName = libraryInfo.library.name?.takeIf(String::isNotEmpty)
if (ideLibraryName != null) {
KotlinNativeLibraryNameUtil.parseIDELibraryName(ideLibraryName)?.let { (kotlinVersion, libraryName) ->
parseIDELibraryName(ideLibraryName)?.let { (kotlinVersion, libraryName) ->
return libraryName to LibraryGroup.FromDistribution(kotlinVersion)
}
if (KotlinNativeLibraryNameUtil.isGradleLibraryName(ideLibraryName))
if (isGradleLibraryName(ideLibraryName))
return ideLibraryName to LibraryGroup.ThirdParty
}
@@ -21,7 +21,8 @@ import com.intellij.util.PathUtilRt
import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.concurrency.CancellablePromise
import org.jetbrains.kotlin.idea.caches.project.getModuleInfosFromIdeaModel
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil.isGradleLibraryName
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil.parseIDELibraryName
import org.jetbrains.kotlin.idea.versions.UnsupportedAbiVersionNotificationPanelProvider
import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion
import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME
@@ -206,11 +207,11 @@ class KotlinNativeABICompatibilityChecker(private val project: Project) : Projec
private fun parseIDELibraryName(libraryInfo: NativeLibraryInfo): Pair<String, LibraryGroup> {
val ideLibraryName = libraryInfo.library.name?.takeIf(String::isNotEmpty)
if (ideLibraryName != null) {
KotlinNativeLibraryNameUtil.parseIDELibraryName(ideLibraryName)?.let { (kotlinVersion, libraryName) ->
parseIDELibraryName(ideLibraryName)?.let { (kotlinVersion, libraryName) ->
return libraryName to LibraryGroup.FromDistribution(kotlinVersion)
}
if (KotlinNativeLibraryNameUtil.isGradleLibraryName(ideLibraryName))
if (isGradleLibraryName(ideLibraryName))
return ideLibraryName to LibraryGroup.ThirdParty
}
@@ -11,6 +11,7 @@ import com.intellij.openapi.util.Key
import org.gradle.tooling.model.idea.IdeaModule
import org.jetbrains.kotlin.gradle.KotlinMPPGradleModel
import org.jetbrains.kotlin.idea.configuration.buildClasspathData
import org.jetbrains.kotlin.idea.configuration.klib.KotlinNativeLibraryNameUtil.buildIDELibraryName
import org.jetbrains.kotlin.idea.inspections.gradle.findKotlinPluginVersion
import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME
import org.jetbrains.plugins.gradle.ExternalDependencyId
@@ -108,10 +109,22 @@ internal class KotlinNativeLibrariesDependencySubstitutor(
private fun buildSubstituteIfNecessary(libraryFile: File): DependencySubstitute {
// need to check whether `library` points to a real KLIB,
// and if answer is yes then build a new dependency that will substitute original one
val klib = klibInfoProvider?.getKlibInfo(libraryFile) as? NativeDistributionKlibInfo ?: return DependencySubstitute.NoSubstitute
val klib = klibInfoProvider?.getKlibInfo(libraryFile) ?: return DependencySubstitute.NoSubstitute
val nonNullKotlinVersion = kotlinVersion ?: return DependencySubstitute.NoSubstitute
val newLibraryName = KotlinNativeLibraryNameUtil.buildIDELibraryName(nonNullKotlinVersion, klib.name, klib.target?.name)
val newLibraryName = when (klib) {
is NativeDistributionKlibInfo -> buildIDELibraryName(
nonNullKotlinVersion,
klib.name,
listOfNotNull(klib.target?.name)
)
is NativeDistributionCommonizedKlibInfo -> buildIDELibraryName(
nonNullKotlinVersion,
klib.name,
klib.commonizedTargets.map { it.name },
klib.ownTarget?.name
)
}
val substitute = DefaultExternalMultiLibraryDependency().apply {
classpathOrder = if (klib.name == KONAN_STDLIB_NAME) -1 else 0 // keep stdlib upper
@@ -10,12 +10,25 @@ object KotlinNativeLibraryNameUtil {
internal const val KOTLIN_NATIVE_LIBRARY_PREFIX_PLUS_SPACE = "$KOTLIN_NATIVE_LIBRARY_PREFIX "
internal const val GRADLE_LIBRARY_PREFIX = "Gradle: "
private val IDE_LIBRARY_NAME_REGEX = Regex("^$KOTLIN_NATIVE_LIBRARY_PREFIX_PLUS_SPACE([^\\s]+) - ([^\\s]+)( \\[([\\w]+)\\])?$")
private val IDE_LIBRARY_NAME_REGEX = Regex("^$KOTLIN_NATIVE_LIBRARY_PREFIX_PLUS_SPACE([^\\s]+) - ([^\\s]+)( \\[([\\w ,()*]+)])?$")
// Builds the name of Kotlin/Native library that is a part of Kotlin/Native distribution
// as it will be displayed in IDE UI.
fun buildIDELibraryName(kotlinVersion: String, libraryName: String, platform: String?): String {
val platformNamePart = platform?.let { " [$it]" }.orEmpty()
fun buildIDELibraryName(
kotlinVersion: String,
libraryName: String,
platforms: Collection<String>,
starredPlatform: String? = null
): String {
val platformNamePart = if (platforms.isNotEmpty())
buildString {
append(" [")
platforms.sorted().joinTo(this) { if (it == starredPlatform) "$it(*)" else it }
append("]")
}
else
""
return "$KOTLIN_NATIVE_LIBRARY_PREFIX_PLUS_SPACE$kotlinVersion - $libraryName$platformNamePart"
}
@@ -25,9 +38,9 @@ object KotlinNativeLibraryNameUtil {
val kotlinVersion = match.groups[1]!!.value
val libraryName = match.groups[2]!!.value
val platform = match.groups[4]?.value
val platformPart = match.groups[4]?.value
return Triple(kotlinVersion, libraryName, platform)
return Triple(kotlinVersion, libraryName, platformPart)
}
fun isGradleLibraryName(ideLibraryName: String) = ideLibraryName.startsWith(GRADLE_LIBRARY_PREFIX)
@@ -15,12 +15,22 @@ class KotlinNativeLibraryNameUtilTest : TestCase() {
fun testBuildIDELibraryName() {
assertEquals(
"Kotlin/Native 1.3.60 - stdlib",
buildIDELibraryName("1.3.60", "stdlib", null)
buildIDELibraryName("1.3.60", "stdlib", emptyList())
)
assertEquals(
"Kotlin/Native 1.3.60-eap-23 - Accelerate [macos_x64]",
buildIDELibraryName("1.3.60-eap-23", "Accelerate", "macos_x64")
buildIDELibraryName("1.3.60-eap-23", "Accelerate", listOf("macos_x64"))
)
assertEquals(
"Kotlin/Native 1.3.60-eap-23 - Accelerate [ios_arm32, ios_arm64, ios_x64]",
buildIDELibraryName("1.3.60-eap-23", "Accelerate", listOf("ios_x64", "ios_arm32", "ios_arm64"))
)
assertEquals(
"Kotlin/Native 1.3.60-eap-23 - Accelerate [ios_arm32, ios_arm64(*), ios_x64]",
buildIDELibraryName("1.3.60-eap-23", "Accelerate", listOf("ios_x64", "ios_arm32", "ios_arm64"), "ios_arm64")
)
}
@@ -35,6 +45,16 @@ class KotlinNativeLibraryNameUtilTest : TestCase() {
parseIDELibraryName("Kotlin/Native 1.3.60-eap-23 - Accelerate [macos_x64]")
)
assertEquals(
Triple("1.3.60-eap-23", "Accelerate", "ios_arm32, ios_arm64, ios_x64"),
parseIDELibraryName("Kotlin/Native 1.3.60-eap-23 - Accelerate [ios_arm32, ios_arm64, ios_x64]")
)
assertEquals(
Triple("1.3.60-eap-23", "Accelerate", "ios_arm32, ios_arm64(*), ios_x64"),
parseIDELibraryName("Kotlin/Native 1.3.60-eap-23 - Accelerate [ios_arm32, ios_arm64(*), ios_x64]")
)
assertNull(parseIDELibraryName("Kotlin/Native - something unexpected"))
assertNull(parseIDELibraryName("foo.klib"))