ask and optionally delete script/kotlin.js during update libraries

This commit is contained in:
Michael Nedzelsky
2014-12-11 16:53:19 +03:00
parent 492a2a1dc8
commit 73a929c78d
@@ -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<VersionedLibrary> versionedOutdatedLibraries = findOutdatedKotlinLibraries(myProject, pluginVersion);
Collection<Library> 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<Library> 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(
"<p>File 'script/kotlin.js' was probably created by an older version of the Kotlin plugin.</p>" +
"<p>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.</p>" +
"<p><a href=\"delete\">Delete this file</a> <a href=\"ignore\">Ignore</a></p>");
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<Library> extractLibraries(Collection<VersionedLibrary> libraries) {
return Collections2.transform(libraries, new com.google.common.base.Function<VersionedLibrary, Library>() {
@Override