From 73a929c78d7fda621c15f2d09b7b6239084e9588 Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Thu, 11 Dec 2014 16:53:19 +0300 Subject: [PATCH] ask and optionally delete script/kotlin.js during update libraries --- .../OutdatedKotlinRuntimeNotification.java | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/idea/src/org/jetbrains/jet/plugin/versions/OutdatedKotlinRuntimeNotification.java b/idea/src/org/jetbrains/jet/plugin/versions/OutdatedKotlinRuntimeNotification.java index 98f7ad182b4..a08aed5f3d7 100644 --- a/idea/src/org/jetbrains/jet/plugin/versions/OutdatedKotlinRuntimeNotification.java +++ b/idea/src/org/jetbrains/jet/plugin/versions/OutdatedKotlinRuntimeNotification.java @@ -24,6 +24,7 @@ import com.intellij.notification.Notification; import com.intellij.notification.NotificationListener; import com.intellij.notification.NotificationType; import com.intellij.notification.Notifications; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.AbstractProjectComponent; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.libraries.Library; @@ -31,6 +32,7 @@ import com.intellij.openapi.startup.StartupManager; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Function; import com.intellij.util.text.VersionComparatorUtil; import org.jetbrains.annotations.NotNull; @@ -41,11 +43,15 @@ import org.jetbrains.jet.plugin.framework.JavaRuntimePresentationProvider; import org.jetbrains.jet.plugin.framework.LibraryPresentationProviderUtil; import javax.swing.event.HyperlinkEvent; +import java.io.IOException; import java.util.Collection; import java.util.List; +import static com.intellij.util.PathUtil.getLocalFile; + public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent { private static final String SUPPRESSED_PROPERTY_NAME = "oudtdated.runtime.suppressed.plugin.version"; + private static final String OUTDATED_RUNTIME_GROUP_DISPLAY_ID = "Outdated Kotlin Runtime"; public OutdatedKotlinRuntimeNotification(Project project) { super(project); @@ -118,7 +124,7 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent } - Notifications.Bus.notify(new Notification("Outdated Kotlin Runtime", "Outdated Kotlin Runtime", message, + Notifications.Bus.notify(new Notification(OUTDATED_RUNTIME_GROUP_DISPLAY_ID, "Outdated Kotlin Runtime", message, NotificationType.WARNING, new NotificationListener() { @Override public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) { @@ -127,6 +133,7 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent Collection versionedOutdatedLibraries = findOutdatedKotlinLibraries(myProject, pluginVersion); Collection outdatedLibraries = extractLibraries(versionedOutdatedLibraries); KotlinRuntimeLibraryUtil.updateLibraries(myProject, outdatedLibraries); + suggestDeleteKotlinJsIfNeeded(outdatedLibraries); } else if ("ignore".equals(event.getDescription())) { PropertiesComponent.getInstance(myProject).setValue(SUPPRESSED_PROPERTY_NAME, pluginVersion); @@ -142,6 +149,75 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent }); } + private void deleteKotlinJs() { + ApplicationManager.getApplication().invokeLater(new Runnable() { + @Override + public void run() { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + VirtualFile kotlinJsFile = myProject.getBaseDir().findFileByRelativePath("script/kotlin.js"); + if (kotlinJsFile == null) return; + + VirtualFile fileToDelete = getLocalFile(kotlinJsFile); + try { + VirtualFile parent = fileToDelete.getParent(); + fileToDelete.delete(this); + parent.refresh(false, true); + } + catch (IOException ex) { + Notifications.Bus.notify( + new Notification(OUTDATED_RUNTIME_GROUP_DISPLAY_ID, "Error", "Could not delete 'script/kotlin.js': " + ex.getMessage(), NotificationType.ERROR)); + } + } + }); + } + }); + } + + private void suggestDeleteKotlinJsIfNeeded(Collection outdatedLibraries) { + VirtualFile kotlinJsFile = myProject.getBaseDir().findFileByRelativePath("script/kotlin.js"); + if (kotlinJsFile == null) return; + + boolean addNotification = false; + for(Library library : outdatedLibraries) { + if (LibraryPresentationProviderUtil.isDetected(JSLibraryStdPresentationProvider.getInstance(), library)) { + VirtualFile jsStdlibJar = JSLibraryStdPresentationProvider.getJsStdLibJar(library); + assert jsStdlibJar != null : "jslibFile should not be null"; + + if (jsStdlibJar.findFileByRelativePath("kotlin.js") == null) { + addNotification = true; + break; + } + } + } + if (!addNotification) return; + + String message = String.format( + "

File 'script/kotlin.js' was probably created by an older version of the Kotlin plugin.

" + + "

The new Kotlin plugin copies an up-to-date version of this file to the output directory automatically, so the old version of it can be deleted.

" + + "

Delete this file Ignore

"); + + Notifications.Bus.notify(new Notification(OUTDATED_RUNTIME_GROUP_DISPLAY_ID, "Outdated Kotlin Runtime", message, + NotificationType.WARNING, new NotificationListener() { + @Override + public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) { + if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { + if ("delete".equals(event.getDescription())) { + deleteKotlinJs(); + } + else if ("ignore".equals(event.getDescription())) { + // pass + } + else { + throw new AssertionError(); + } + notification.expire(); + } + } + }), myProject); + } + private static Collection extractLibraries(Collection libraries) { return Collections2.transform(libraries, new com.google.common.base.Function() { @Override