From 1367e6f303fbeaa7d130844bc230223d68764b80 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 10 Jul 2017 15:42:53 +0200 Subject: [PATCH] Get library versions directly, not through LibraryPresentationProvider --- .../framework/JavaRuntimeDetectionUtil.java | 7 +++++++ .../framework/JsLibraryStdDetectionUtil.kt | 2 ++ ...nVersionInfoProviderByModuleDependencies.kt | 14 +++++++------- .../kotlin/idea/framework/KotlinLibraryUtil.kt | 9 --------- .../idea/versions/KotlinRuntimeLibraryUtil.kt | 4 +--- .../versions/OutdatedKotlinRuntimeChecker.kt | 18 +++++++----------- .../configuration/ConfigureKotlinTest.java | 12 +++++------- 7 files changed, 29 insertions(+), 37 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JavaRuntimeDetectionUtil.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JavaRuntimeDetectionUtil.java index 307d82bd4d2..e93d63a7bb8 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JavaRuntimeDetectionUtil.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JavaRuntimeDetectionUtil.java @@ -16,6 +16,8 @@ package org.jetbrains.kotlin.idea.framework; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.util.io.JarUtil; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; @@ -23,10 +25,15 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.utils.PathUtil; +import java.util.Arrays; import java.util.List; import java.util.jar.Attributes; public class JavaRuntimeDetectionUtil { + public static String getJavaRuntimeVersion(@NotNull Library library) { + return getJavaRuntimeVersion(Arrays.asList(library.getFiles(OrderRootType.CLASSES))); + } + public static String getJavaRuntimeVersion(@NotNull List classesRoots) { VirtualFile stdJar = getRuntimeJar(classesRoots); if (stdJar != null) { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JsLibraryStdDetectionUtil.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JsLibraryStdDetectionUtil.kt index a255683356d..fe435e39334 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JsLibraryStdDetectionUtil.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/framework/JsLibraryStdDetectionUtil.kt @@ -42,6 +42,8 @@ object JsLibraryStdDetectionUtil { return getJsLibraryStdVersion(classes) != null } + fun getJsLibraryStdVersion(library: Library): String? = getJsLibraryStdVersion(library.getFiles(OrderRootType.CLASSES).toList()) + fun getJsLibraryStdVersion(classesRoots: List): String? { if (JavaRuntimeDetectionUtil.getJavaRuntimeVersion(classesRoots) != null) { // Prevent clashing with java runtime, in case when library collects all roots. diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProviderByModuleDependencies.kt b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProviderByModuleDependencies.kt index 883e76bb6dc..438af7f3102 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProviderByModuleDependencies.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/KotlinVersionInfoProviderByModuleDependencies.kt @@ -20,26 +20,26 @@ import com.intellij.openapi.module.Module import com.intellij.openapi.roots.LibraryOrderEntry import com.intellij.openapi.roots.ModuleRootManager import com.intellij.openapi.roots.ModuleRootModel -import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.roots.libraries.Library import org.jetbrains.kotlin.config.TargetPlatformKind -import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider -import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider +import org.jetbrains.kotlin.idea.framework.JavaRuntimeDetectionUtil +import org.jetbrains.kotlin.idea.framework.JsLibraryStdDetectionUtil import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion class KotlinVersionInfoProviderByModuleDependencies : KotlinVersionInfoProvider { override fun getCompilerVersion(module: Module) = bundledRuntimeVersion() override fun getLibraryVersions(module: Module, targetPlatform: TargetPlatformKind<*>, rootModel: ModuleRootModel?): Collection { - val presentationProvider = when (targetPlatform) { - is TargetPlatformKind.JavaScript -> JSLibraryStdPresentationProvider.getInstance() - is TargetPlatformKind.Jvm -> JavaRuntimePresentationProvider.getInstance() + val versionProvider: (Library) -> String? = when (targetPlatform) { + is TargetPlatformKind.JavaScript -> JsLibraryStdDetectionUtil::getJsLibraryStdVersion + is TargetPlatformKind.Jvm -> JavaRuntimeDetectionUtil::getJavaRuntimeVersion is TargetPlatformKind.Common -> return emptyList() } return (rootModel ?: ModuleRootManager.getInstance(module)) .orderEntries .asSequence() .filterIsInstance() - .mapNotNull { it.library?.let { presentationProvider.detect(it.getFiles(OrderRootType.CLASSES).toList())?.versionString } } + .mapNotNull { it.library?.let { versionProvider(it) } } .toList() } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/framework/KotlinLibraryUtil.kt b/idea/src/org/jetbrains/kotlin/idea/framework/KotlinLibraryUtil.kt index 69428b9d716..cfeb5a5d58f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/framework/KotlinLibraryUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/framework/KotlinLibraryUtil.kt @@ -24,15 +24,6 @@ import com.intellij.openapi.roots.libraries.LibraryPresentationProvider import com.intellij.openapi.roots.libraries.LibraryProperties import java.util.* -fun > isDetected(provider: LibraryPresentationProvider, library: Library): Boolean { - return getLibraryProperties(provider, library) != null -} - -fun > getLibraryProperties(provider: LibraryPresentationProvider, library: Library): LP? { - if (isExternalLibrary(library)) return null - return provider.detect(Arrays.asList(*library.getFiles(OrderRootType.CLASSES))) -} - private val MAVEN_SYSTEM_ID = ProjectSystemId("MAVEN") val GRADLE_SYSTEM_ID = ProjectSystemId("GRADLE") diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt index daad1fb9347..8699ea06267 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt @@ -45,8 +45,6 @@ import org.jetbrains.kotlin.config.JvmTarget import org.jetbrains.kotlin.idea.KotlinPluginUtil import org.jetbrains.kotlin.idea.configuration.* import org.jetbrains.kotlin.idea.framework.JavaRuntimeDetectionUtil -import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider -import org.jetbrains.kotlin.idea.framework.isDetected import org.jetbrains.kotlin.idea.framework.isExternalLibrary import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.idea.util.application.runWriteAction @@ -98,7 +96,7 @@ fun updateLibraries(project: Project, libraries: Collection) { // TODO use module SDK for (library in libraries) { - val libraryJarDescriptors = if (isDetected(JavaRuntimePresentationProvider.getInstance(), library)) + val libraryJarDescriptors = if (JavaRuntimeDetectionUtil.getJavaRuntimeVersion(library) != null) kJvmConfigurator.getLibraryJarDescriptors(sdk) else kJsConfigurator.getLibraryJarDescriptors(sdk) diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/OutdatedKotlinRuntimeChecker.kt b/idea/src/org/jetbrains/kotlin/idea/versions/OutdatedKotlinRuntimeChecker.kt index ccfe8afde45..9d52248e9dc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/OutdatedKotlinRuntimeChecker.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/OutdatedKotlinRuntimeChecker.kt @@ -28,12 +28,10 @@ import com.intellij.openapi.roots.LibraryOrderEntry import com.intellij.openapi.roots.ModuleRootManager import com.intellij.openapi.roots.OrderRootType import com.intellij.openapi.roots.libraries.Library -import com.intellij.util.PathUtil.getLocalFile import com.intellij.util.text.VersionComparatorUtil import org.jetbrains.kotlin.idea.KotlinPluginUtil -import org.jetbrains.kotlin.idea.framework.* -import org.jetbrains.kotlin.idea.util.application.runWriteAction -import java.io.IOException +import org.jetbrains.kotlin.idea.framework.JavaRuntimeDetectionUtil +import org.jetbrains.kotlin.idea.framework.JsLibraryStdDetectionUtil import javax.swing.event.HyperlinkEvent data class VersionedLibrary(val library: Library, val version: String?, val usedInModules: Collection) @@ -59,18 +57,16 @@ fun findOutdatedKotlinLibraries(project: Project): List { } fun getOutdatedRuntimeLibraryVersion(library: Library): String? { - val libraryVersionProperties = getKotlinLibraryVersionProperties(library) ?: return null - - val libraryVersion = libraryVersionProperties.versionString - + val libraryVersion = getKotlinLibraryVersion(library) ?: return null val runtimeVersion = bundledRuntimeVersion() return if (isRuntimeOutdated(libraryVersion, runtimeVersion)) libraryVersion else null } -private fun getKotlinLibraryVersionProperties(library: Library) = - getLibraryProperties(JavaRuntimePresentationProvider.getInstance(), library) ?: - getLibraryProperties(JSLibraryStdPresentationProvider.getInstance(), library) +private fun getKotlinLibraryVersion(library: Library): String? { + val roots = library.getFiles(OrderRootType.CLASSES).toList() + return JavaRuntimeDetectionUtil.getJavaRuntimeVersion(roots) ?: JsLibraryStdDetectionUtil.getJsLibraryStdVersion(roots) +} fun findKotlinRuntimeLibrary(module: Module, predicate: (Library) -> Boolean = ::isKotlinRuntime): Library? { val orderEntries = ModuleRootManager.getInstance(module).orderEntries.filterIsInstance() diff --git a/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java index d58ab7187e6..1fec6a016e9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.idea.configuration; -import com.intellij.framework.library.LibraryVersionProperties; import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl; import com.intellij.openapi.module.Module; import com.intellij.openapi.roots.libraries.Library; @@ -26,8 +25,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; import org.jetbrains.kotlin.config.*; import org.jetbrains.kotlin.idea.facet.FacetUtilsKt; import org.jetbrains.kotlin.idea.facet.KotlinFacet; -import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider; -import org.jetbrains.kotlin.idea.framework.KotlinLibraryUtilKt; +import org.jetbrains.kotlin.idea.framework.JsLibraryStdDetectionUtil; import org.jetbrains.kotlin.idea.project.PlatformKt; import org.jetbrains.kotlin.idea.versions.KotlinRuntimeLibraryUtilKt; @@ -145,14 +143,14 @@ public class ConfigureKotlinTest extends AbstractConfigureKotlinTest { public void testJsLibraryVersion11() { Library jsRuntime = KotlinRuntimeLibraryUtilKt.findAllUsedLibraries(myProject).keySet().iterator().next(); - LibraryVersionProperties properties = KotlinLibraryUtilKt.getLibraryProperties(JSLibraryStdPresentationProvider.getInstance(), jsRuntime); - assertEquals("1.1.0", properties.getVersionString()); + String version = JsLibraryStdDetectionUtil.INSTANCE.getJsLibraryStdVersion(jsRuntime); + assertEquals("1.1.0", version); } public void testJsLibraryVersion106() { Library jsRuntime = KotlinRuntimeLibraryUtilKt.findAllUsedLibraries(myProject).keySet().iterator().next(); - LibraryVersionProperties properties = KotlinLibraryUtilKt.getLibraryProperties(JSLibraryStdPresentationProvider.getInstance(), jsRuntime); - assertEquals("1.0.6", properties.getVersionString()); + String version = JsLibraryStdDetectionUtil.INSTANCE.getJsLibraryStdVersion(jsRuntime); + assertEquals("1.0.6", version); } @SuppressWarnings("ConstantConditions")