User friendly presentation of KLIBs in IDEA

Adds friendly names for KLIBs imported as file dependencies from Gradle project.

Issue #KT-29613 Fixed
Issue #KT-29783 Fixed
This commit is contained in:
Dmitriy Dolovov
2019-01-31 02:21:25 +07:00
parent 06b5a42d0d
commit 75250cf1a0
42 changed files with 657 additions and 21 deletions
@@ -18,16 +18,20 @@ const val KDEFINITIONS_FILE_EXTENSION = "def"
const val KLIB_MODULE_METADATA_FILE_NAME = "module"
const val KLIB_MANIFEST_FILE_NAME = "manifest"
const val KONAN_STDLIB_NAME = "stdlib"
const val KLIB_DIR_NAME = "klib"
val KONAN_COMMON_LIBS_PATH: Path
get() = Paths.get("klib", "common")
get() = Paths.get(KLIB_DIR_NAME, "common")
val KONAN_ALL_PLATFORM_LIBS_PATH: Path
get() = Paths.get("klib", "platform")
get() = Paths.get(KLIB_DIR_NAME, "platform")
fun konanCommonLibraryPath(libraryName: String): Path = KONAN_COMMON_LIBS_PATH.resolve(libraryName)
fun konanSpecificPlatformLibrariesPath(platform: String): Path = KONAN_ALL_PLATFORM_LIBS_PATH.resolve(platform)
fun konanPlatformLibraryPath(platform: String, libraryName: String): Path = konanSpecificPlatformLibrariesPath(platform).resolve(libraryName)
fun konanPlatformLibraryPath(libraryName: String, platform: String): Path = konanSpecificPlatformLibrariesPath(platform).resolve(libraryName)
@@ -21,7 +21,7 @@ interface KonanLibraryLayout {
// This is a default implementation. Can't make it an assignment.
val target: KonanTarget? get() = null
val manifestFile get() = File(libDir, "manifest")
val manifestFile get() = File(libDir, KLIB_MANIFEST_FILE_NAME)
val resourcesDir get() = File(libDir, "resources")
val targetsDir get() = File(libDir, "targets")
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.konan.library.lite
import java.nio.file.Path
data class LiteKonanLibrary(
val path: Path,
val name: String,
val platform: String?
)
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.konan.library.lite
import org.jetbrains.kotlin.konan.library.KLIB_DIR_NAME
import org.jetbrains.kotlin.konan.library.KLIB_MANIFEST_FILE_NAME
import org.jetbrains.kotlin.konan.library.KLIB_PROPERTY_UNIQUE_NAME
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
class LiteKonanLibraryInfoProvider(customKonanHomeDir: String? = null) {
private val konanHomeDir by lazy {
customKonanHomeDir
?.takeIf { it.isNotEmpty() }
?.let { Paths.get(it) }
?.takeIf {
// small sanity check to ensure that it's a valid Kotlin/Native home directory
Files.isDirectory(it.resolve(KLIB_DIR_NAME))
}
}
private val konanDataDir by lazy {
val path = System.getenv("KONAN_DATA_DIR")?.let { Paths.get(it) }
?: Paths.get(System.getProperty("user.home"), ".konan")
path.toAbsolutePath()
}
/**
* Returns either [LiteKonanLibrary], or null if there is no such library in
* Kotlin/Native distribution.
*/
fun getDistributionLibraryInfo(libraryPath: Path): LiteKonanLibrary? {
// check whether it under Kotlin/Native root
if (!isUnderKonanRoot(libraryPath))
return null
val manifestFile = libraryPath.resolve(KLIB_MANIFEST_FILE_NAME)
if (!Files.isRegularFile(manifestFile))
return null
val parentPath = libraryPath.parent ?: return null
val parentName = parentPath.toFile().name
val platform = when (parentName) {
"common" -> null
else -> {
val grandParentName = parentPath.parent?.toFile()?.name ?: return null
when (grandParentName) {
"platform" -> parentName
else -> return null
}
}
}
val manifestProperties = Properties().apply {
try {
Files.newInputStream(manifestFile).use { load(it) }
} catch (e: IOException) {
return null
}
}
val name = manifestProperties[KLIB_PROPERTY_UNIQUE_NAME]?.toString() ?: return null
return LiteKonanLibrary(libraryPath, name, platform)
}
private fun isUnderKonanRoot(libraryPath: Path): Boolean {
if (konanHomeDir != null && libraryPath.startsWith(konanHomeDir))
return true
return libraryPath.startsWith(konanDataDir)
}
}