From 14a6cf3c8f519033a835d25ce1a6cd9d1dbf7685 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Sun, 24 Mar 2013 23:20:12 +0400 Subject: [PATCH] Remove old js during configuring new one --- .../FrameworksCompatibilityUtils.java | 35 +++++++++++++++---- .../framework/JSFrameworkSupportProvider.java | 2 ++ .../JSLibraryStdPresentationProvider.java | 17 +++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/framework/FrameworksCompatibilityUtils.java b/idea/src/org/jetbrains/jet/plugin/framework/FrameworksCompatibilityUtils.java index 5496a524f51..4736213f58a 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/FrameworksCompatibilityUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/FrameworksCompatibilityUtils.java @@ -47,7 +47,7 @@ public class FrameworksCompatibilityUtils { for (OrderEntry entry : rootModel.getOrderEntries()) { if (!(entry instanceof LibraryOrderEntry)) continue; - final Library library = ((LibraryOrderEntry)entry).getLibrary(); + Library library = ((LibraryOrderEntry)entry).getLibrary(); if (library == null) continue; for (LibraryKind kind : kinds) { @@ -57,15 +57,36 @@ public class FrameworksCompatibilityUtils { } } - if (!existingEntries.isEmpty()) { - int result = Messages.showYesNoDialog( - String.format("Current module is already configured with '%s' framework.\nDo you want to remove it?", frameworkType.getPresentableName()), - "Framework Conflict", - Messages.getWarningIcon()); + removeWithConfirm(rootModel, existingEntries, + String.format("Current module is already configured with '%s' framework.\nDo you want to remove it?", + frameworkType.getPresentableName()), + "Framework Conflict"); + } + + public static void suggestRemoveOldJsLibrary(@NotNull ModifiableRootModel rootModel) { + List oldJsLibraries = new ArrayList(); + for (OrderEntry entry : rootModel.getOrderEntries()) { + if (!(entry instanceof LibraryOrderEntry)) continue; + Library library = ((LibraryOrderEntry)entry).getLibrary(); + if (library == null) continue; + + if (JSLibraryStdPresentationProvider.detectOld(library)) { + oldJsLibraries.add(entry); + } + } + + removeWithConfirm(rootModel, oldJsLibraries, + "Current module is configured with old js library.\nDo you want to remove it?", + "Old JS Library"); + } + + private static void removeWithConfirm(ModifiableRootModel rootModel, List orderEntries, String message, String title) { + if (!orderEntries.isEmpty()) { + int result = Messages.showYesNoDialog(message, title, Messages.getWarningIcon()); if (result == 0) { - for (OrderEntry entry : existingEntries) { + for (OrderEntry entry : orderEntries) { rootModel.removeOrderEntry(entry); } } diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JSFrameworkSupportProvider.java b/idea/src/org/jetbrains/jet/plugin/framework/JSFrameworkSupportProvider.java index ac0ee3960cf..fea7a79d0ae 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/JSFrameworkSupportProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/JSFrameworkSupportProvider.java @@ -68,6 +68,8 @@ public class JSFrameworkSupportProvider extends FrameworkSupportInModuleProvider rootModel, new JavaRuntimeLibraryDescription(), JavaFrameworkType.getInstance()); + + FrameworksCompatibilityUtils.suggestRemoveOldJsLibrary(rootModel); } }; } diff --git a/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryStdPresentationProvider.java b/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryStdPresentationProvider.java index ccdb18b2521..73da839e882 100644 --- a/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryStdPresentationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/framework/JSLibraryStdPresentationProvider.java @@ -17,7 +17,9 @@ package org.jetbrains.jet.plugin.framework; import com.intellij.framework.library.LibraryVersionProperties; +import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.roots.libraries.JarVersionDetectionUtil; +import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryPresentationProvider; import com.intellij.openapi.vfs.JarFile; import com.intellij.openapi.vfs.JarFileSystem; @@ -71,4 +73,19 @@ public class JSLibraryStdPresentationProvider extends LibraryPresentationProvide return null; } + + /** + * Detect js standard library according to M5.1 rules. + */ + public static boolean detectOld(@NotNull Library library) { + if (library.getFiles(OrderRootType.CLASSES).length == 0) { + for (VirtualFile file : library.getFiles(OrderRootType.SOURCES)) { + if (file.getName().equals(PathUtil.JS_LIB_JAR_NAME)) { + return true; + } + } + } + + return false; + } }