From 241e1dc476dd5b9ed483725bb5c9c11964dacde1 Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 27 Dec 2011 13:28:36 +0400 Subject: [PATCH 1/2] added 'clean' action to 'Build Artifacts' popup (IDEA-79174) --- .../compiler/actions/BuildArtifactAction.java | 92 +++++++++++++++++-- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java b/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java index de755eef5ed5..4c48887f958c 100644 --- a/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java +++ b/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java @@ -15,16 +15,26 @@ */ package com.intellij.compiler.actions; -import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonShortcuts; +import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.compiler.CompileScope; import com.intellij.openapi.compiler.CompilerManager; import com.intellij.openapi.keymap.KeymapUtil; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService; +import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.ListSeparator; import com.intellij.openapi.ui.popup.PopupStep; import com.intellij.openapi.ui.popup.util.BaseListPopupStep; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.Artifact; import com.intellij.packaging.impl.artifacts.ArtifactUtil; import com.intellij.packaging.impl.compiler.ArtifactCompileScope; @@ -37,9 +47,8 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.event.ActionEvent; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.io.File; +import java.util.*; /** * @author nik @@ -102,10 +111,9 @@ public class BuildArtifactAction extends AnAction { } protected static void doBuild(@NotNull Project project, final @NotNull ArtifactPopupItem item, boolean rebuild) { - final Artifact artifact = item.getArtifact(); - final List artifacts = artifact != null ? Collections.singletonList(artifact) : ArtifactUtil.getArtifactWithOutputPaths(project); + final List artifacts = item.getArtifacts(project); final CompileScope scope = ArtifactCompileScope.createArtifactsScope(project, artifacts); - ArtifactsWorkspaceSettings.getInstance(project).setArtifactsToBuild(ContainerUtil.createMaybeSingletonList(artifact)); + ArtifactsWorkspaceSettings.getInstance(project).setArtifactsToBuild(ContainerUtil.createMaybeSingletonList(item.getArtifact())); if (!rebuild) { CompilerManager.getInstance(project).make(scope, null); } @@ -125,6 +133,70 @@ public class BuildArtifactAction extends AnAction { } } + private static class CleanArtifactItem extends ArtifactActionItem { + private CleanArtifactItem(@NotNull ArtifactPopupItem item, @NotNull Project project) { + super(item, project, "Clean"); + } + + @Override + public void run() { + Set parents = new HashSet(); + final VirtualFile[] roots = ProjectRootManager.getInstance(myProject).getContentSourceRoots(); + for (VirtualFile root : roots) { + VirtualFile parent = root; + while (parent != null && !parents.contains(parent)) { + parents.add(parent); + parent = parent.getParent(); + } + } + + Map outputPathContainingSourceRoots = new HashMap(); + final List files = new ArrayList(); + for (Artifact artifact : myArtifactPopupItem.getArtifacts(myProject)) { + String outputPath = artifact.getOutputFilePath(); + if (outputPath != null) { + files.add(new File(FileUtil.toSystemDependentName(outputPath))); + final VirtualFile outputFile = LocalFileSystem.getInstance().findFileByPath(outputPath); + if (parents.contains(outputFile)) { + outputPathContainingSourceRoots.put(artifact.getName(), outputPath); + } + } + } + + if (!outputPathContainingSourceRoots.isEmpty()) { + final String message; + if (outputPathContainingSourceRoots.size() == 1 && outputPathContainingSourceRoots.values().size() == 1) { + final String name = ContainerUtil.getFirstItem(outputPathContainingSourceRoots.keySet()); + final String output = outputPathContainingSourceRoots.get(name); + message = "The output directory '" + output + "' of '" + name + "' artifact contains source roots of the project. Do you want to continue and clear it?"; + } + else { + StringBuilder info = new StringBuilder(); + for (String name : outputPathContainingSourceRoots.keySet()) { + info.append(" '").append(name).append("' artifact ('").append(outputPathContainingSourceRoots.get(name)).append("')\n"); + } + message = "The output directories of the following artifacts contains source roots:\n" + + info + "Do you want to continue and clear these directories?"; + } + final int answer = Messages.showYesNoDialog(myProject, message, "Clean Artifacts", null); + if (answer != 0) { + return; + } + } + + new Task.Backgroundable(myProject, "Cleaning artifacts...", true) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + for (File file : files) { + indicator.checkCanceled(); + FileUtil.delete(file); + } + LocalFileSystem.getInstance().refreshIoFiles(files, true, true, null); + } + }.queue(); + } + } + private static class RebuildArtifactItem extends ArtifactActionItem { private RebuildArtifactItem(ArtifactPopupItem item, Project project) { super(item, project, "Rebuild"); @@ -189,6 +261,11 @@ public class BuildArtifactAction extends AnAction { public Icon getIcon() { return myIcon; } + + public List getArtifacts(Project project) { + final Artifact artifact = getArtifact(); + return artifact != null ? Collections.singletonList(artifact) : ArtifactUtil.getArtifactWithOutputPaths(project); + } } private static class ChooseArtifactStep extends BaseListPopupStep { @@ -244,6 +321,7 @@ public class BuildArtifactAction extends AnAction { final List actions = new ArrayList(); actions.add(new BuildArtifactItem(selectedValue, myProject)); actions.add(new RebuildArtifactItem(selectedValue, myProject)); + actions.add(new CleanArtifactItem(selectedValue, myProject)); if (mySettingsService != null) { actions.add(new EditArtifactItem(selectedValue, myProject, mySettingsService)); } From d0793827aac777cccb9ed7c45497dbe0075da6ad Mon Sep 17 00:00:00 2001 From: Serega Vasiliev Date: Tue, 27 Dec 2011 10:32:56 +0100 Subject: [PATCH 2/2] catch AssertionError --- .../persistence/BasicSentUsagesPersistenceComponent.java | 3 +-- .../persistence/UsageStatisticsPersistenceComponent.java | 6 +++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/internal/statistic/persistence/BasicSentUsagesPersistenceComponent.java b/platform/platform-impl/src/com/intellij/internal/statistic/persistence/BasicSentUsagesPersistenceComponent.java index b77ea2367816..0e195bc87d55 100644 --- a/platform/platform-impl/src/com/intellij/internal/statistic/persistence/BasicSentUsagesPersistenceComponent.java +++ b/platform/platform-impl/src/com/intellij/internal/statistic/persistence/BasicSentUsagesPersistenceComponent.java @@ -78,8 +78,7 @@ public class BasicSentUsagesPersistenceComponent extends SentUsagesPersistence { @NotNull - public Map> getSentUsages - () { + public Map> getSentUsages () { return mySentDescriptors; } } diff --git a/platform/platform-impl/src/com/intellij/internal/statistic/persistence/UsageStatisticsPersistenceComponent.java b/platform/platform-impl/src/com/intellij/internal/statistic/persistence/UsageStatisticsPersistenceComponent.java index d840a0f88994..5e459e3f9483 100644 --- a/platform/platform-impl/src/com/intellij/internal/statistic/persistence/UsageStatisticsPersistenceComponent.java +++ b/platform/platform-impl/src/com/intellij/internal/statistic/persistence/UsageStatisticsPersistenceComponent.java @@ -79,7 +79,11 @@ public class UsageStatisticsPersistenceComponent extends BasicSentUsagesPersiste String valueData = groupElement.getAttributeValue(DATA_ATTR); if (!StringUtil.isEmptyOrSpaces(groupId) && !StringUtil.isEmptyOrSpaces(valueData)) { - getSentUsages().putAll(ConvertUsagesUtil.convertValueString(GroupDescriptor.create(groupId, groupPriority), valueData)); + try { + getSentUsages().putAll(ConvertUsagesUtil.convertValueString(GroupDescriptor.create(groupId, groupPriority), valueData)); + } catch (AssertionError e) { + //don't load incorrect groups + } } }