Get library versions directly, not through LibraryPresentationProvider

This commit is contained in:
Dmitry Jemerov
2017-07-10 15:42:53 +02:00
parent 2d1ac69986
commit 1367e6f303
7 changed files with 29 additions and 37 deletions
@@ -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<VirtualFile> classesRoots) {
VirtualFile stdJar = getRuntimeJar(classesRoots);
if (stdJar != null) {
@@ -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<VirtualFile>): String? {
if (JavaRuntimeDetectionUtil.getJavaRuntimeVersion(classesRoots) != null) {
// Prevent clashing with java runtime, in case when library collects all roots.
@@ -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<String> {
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<LibraryOrderEntry>()
.mapNotNull { it.library?.let { presentationProvider.detect(it.getFiles(OrderRootType.CLASSES).toList())?.versionString } }
.mapNotNull { it.library?.let { versionProvider(it) } }
.toList()
}
}
@@ -24,15 +24,6 @@ import com.intellij.openapi.roots.libraries.LibraryPresentationProvider
import com.intellij.openapi.roots.libraries.LibraryProperties
import java.util.*
fun <LP : LibraryProperties<out Any>> isDetected(provider: LibraryPresentationProvider<LP>, library: Library): Boolean {
return getLibraryProperties(provider, library) != null
}
fun <LP : LibraryProperties<out Any>> getLibraryProperties(provider: LibraryPresentationProvider<LP>, 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")
@@ -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<Library>) {
// 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)
@@ -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<Module>)
@@ -59,18 +57,16 @@ fun findOutdatedKotlinLibraries(project: Project): List<VersionedLibrary> {
}
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<LibraryOrderEntry>()
@@ -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")