From 6782eab93b285ed9462557a85e156a668672a4d0 Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 21 Apr 2011 14:24:24 +0400 Subject: [PATCH] IDEA-65343: Add Option to Force Clean Artifact build --- .../compiler/actions/BuildArtifactAction.java | 72 +----------- .../actions/BuildArtifactActionBase.java | 105 ++++++++++++++++++ .../compiler/actions/CleanArtifactAction.java | 63 +++++++++++ .../impl/artifacts/ArtifactImpl.java | 9 +- .../packaging/artifacts/Artifact.java | 3 + resources/src/idea/IdeaActions.xml | 1 + 6 files changed, 186 insertions(+), 67 deletions(-) create mode 100644 java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactActionBase.java create mode 100644 java/compiler/impl/src/com/intellij/compiler/actions/CleanArtifactAction.java 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 fd634a1fe651..9504ec5278df 100644 --- a/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java +++ b/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactAction.java @@ -15,88 +15,28 @@ */ package com.intellij.compiler.actions; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.PlatformDataKeys; -import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.compiler.CompilerManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.packaging.artifacts.Artifact; -import com.intellij.packaging.artifacts.ArtifactManager; import com.intellij.packaging.impl.compiler.ArtifactCompileScope; -import com.intellij.packaging.impl.compiler.ArtifactsWorkspaceSettings; -import com.intellij.packaging.impl.ui.ChooseArtifactsDialog; -import java.util.ArrayList; import java.util.List; /** * @author nik */ -public class BuildArtifactAction extends AnAction { +public class BuildArtifactAction extends BuildArtifactActionBase { public BuildArtifactAction() { - super("Build Artifact"); + super("Build"); } @Override - public void update(AnActionEvent e) { - final Project project = e.getData(PlatformDataKeys.PROJECT); - final Presentation presentation = e.getPresentation(); - presentation.setEnabled(false); - if (project == null) { - return; - } - final List artifacts = getArtifactWithOutputPaths(project); - if (artifacts.isEmpty()) { - return; - } - presentation.setEnabled(true); - if (artifacts.size() == 1) { - presentation.setText("Build '" + StringUtil.first(artifacts.get(0).getName(), 40, true) + "' artifact"); - } - else { - presentation.setText("Build Artifacts..."); - } + protected String getDescription() { + return "Selected artifacts will be built with all dependencies"; } - private static List getArtifactWithOutputPaths(Project project) { - final List result = new ArrayList(); - for (Artifact artifact : ArtifactManager.getInstance(project).getSortedArtifacts()) { - if (!StringUtil.isEmpty(artifact.getOutputPath())) { - result.add(artifact); - } - } - return result; - } - - public void actionPerformed(AnActionEvent e) { - final Project project = e.getData(PlatformDataKeys.PROJECT); - if (project == null) return; - - final List artifacts = getArtifactWithOutputPaths(project); - if (artifacts.isEmpty()) return; - - if (artifacts.size() == 1) { - buildArtifacts(project, artifacts); - return; - } - - final ChooseArtifactsDialog dialog = new ChooseArtifactsDialog(project, artifacts, "Choose Artifacts to Build", "Selected artifacts will be built with all dependencies"); - final List initialSelection = ArtifactsWorkspaceSettings.getInstance(project).getArtifactsToBuild(); - if (!initialSelection.isEmpty()) { - dialog.selectElements(initialSelection); - } - dialog.show(); - - if (dialog.isOK()) { - final List selected = dialog.getChosenElements(); - ArtifactsWorkspaceSettings.getInstance(project).setArtifactsToBuild(selected); - buildArtifacts(project, selected); - } - } - - private static void buildArtifacts(Project project, final List artifacts) { + @Override + protected void performAction(Project project, final List artifacts) { CompilerManager.getInstance(project).make(ArtifactCompileScope.createArtifactsScope(project, artifacts), null); } } diff --git a/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactActionBase.java b/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactActionBase.java new file mode 100644 index 000000000000..412641289f5c --- /dev/null +++ b/java/compiler/impl/src/com/intellij/compiler/actions/BuildArtifactActionBase.java @@ -0,0 +1,105 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.compiler.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.packaging.artifacts.Artifact; +import com.intellij.packaging.artifacts.ArtifactManager; +import com.intellij.packaging.impl.compiler.ArtifactsWorkspaceSettings; +import com.intellij.packaging.impl.ui.ChooseArtifactsDialog; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author nik + */ +public abstract class BuildArtifactActionBase extends AnAction { + private final String myActionName; + + public BuildArtifactActionBase(String actionName) { + super(actionName + " Artifact"); + myActionName = actionName; + } + + @Override + public void update(AnActionEvent e) { + final Project project = e.getData(PlatformDataKeys.PROJECT); + final Presentation presentation = e.getPresentation(); + presentation.setEnabled(false); + if (project == null) { + return; + } + final List artifacts = getArtifactWithOutputPaths(project); + if (artifacts.isEmpty()) { + return; + } + presentation.setEnabled(true); + if (artifacts.size() == 1) { + String first = StringUtil.first(artifacts.get(0).getName(), 40, true); + presentation.setText(myActionName + " '" + first + "' artifact"); + } + else { + presentation.setText(myActionName + " Artifacts..."); + } + } + + public void actionPerformed(AnActionEvent e) { + final Project project = e.getData(PlatformDataKeys.PROJECT); + if (project == null) return; + + final List artifacts = getArtifactWithOutputPaths(project); + if (artifacts.isEmpty()) return; + + if (artifacts.size() == 1) { + performAction(project, artifacts); + return; + } + + final ChooseArtifactsDialog dialog = new ChooseArtifactsDialog(project, artifacts, "Choose Artifacts to " + myActionName, + getDescription()); + final List initialSelection = ArtifactsWorkspaceSettings.getInstance(project).getArtifactsToBuild(); + if (!initialSelection.isEmpty()) { + dialog.selectElements(initialSelection); + } + dialog.show(); + + if (dialog.isOK()) { + final List selected = dialog.getChosenElements(); + ArtifactsWorkspaceSettings.getInstance(project).setArtifactsToBuild(selected); + performAction(project, selected); + } + } + + protected abstract String getDescription(); + + protected abstract void performAction(Project project, List artifacts); + + private static List getArtifactWithOutputPaths(Project project) { + final List result = new ArrayList(); + for (Artifact artifact : ArtifactManager.getInstance(project).getSortedArtifacts()) { + if (!StringUtil.isEmpty(artifact.getOutputPath())) { + result.add(artifact); + } + } + return result; + } +} diff --git a/java/compiler/impl/src/com/intellij/compiler/actions/CleanArtifactAction.java b/java/compiler/impl/src/com/intellij/compiler/actions/CleanArtifactAction.java new file mode 100644 index 000000000000..3be0a2e3604c --- /dev/null +++ b/java/compiler/impl/src/com/intellij/compiler/actions/CleanArtifactAction.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.compiler.actions; + +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.progress.Task; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.packaging.artifacts.Artifact; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * @author nik + */ +public class CleanArtifactAction extends BuildArtifactActionBase { + public CleanArtifactAction() { + super("Clean"); + } + + @Override + protected String getDescription() { + return "Output of the selected artifacts will be cleared."; + } + + @Override + protected void performAction(Project project, final List artifacts) { + final List files = new ArrayList(); + for (Artifact artifact : artifacts) { + String outputPath = artifact.getOutputFilePath(); + if (outputPath != null) { + files.add(new File(FileUtil.toSystemDependentName(outputPath))); + } + } + new Task.Backgroundable(project, "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(); + } +} diff --git a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactImpl.java b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactImpl.java index 29566661c0f2..e0c0ca223c8b 100644 --- a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactImpl.java +++ b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactImpl.java @@ -16,6 +16,7 @@ package com.intellij.packaging.impl.artifacts; import com.intellij.openapi.util.UserDataHolderBase; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.*; @@ -149,6 +150,12 @@ public class ArtifactImpl extends UserDataHolderBase implements ModifiableArtifa @Override public VirtualFile getOutputFile() { + String filePath = getOutputFilePath(); + return !StringUtil.isEmpty(filePath) ? LocalFileSystem.getInstance().findFileByPath(filePath) : null; + } + + @Override + public String getOutputFilePath() { String filePath; if (myRootElement instanceof ArchivePackagingElement) { filePath = myOutputPath + "/" + ((ArchivePackagingElement)myRootElement).getArchiveFileName(); @@ -156,7 +163,7 @@ public class ArtifactImpl extends UserDataHolderBase implements ModifiableArtifa else { filePath = myOutputPath; } - return LocalFileSystem.getInstance().findFileByPath(filePath); + return filePath; } public void copyFrom(ArtifactImpl modified) { diff --git a/java/compiler/openapi/src/com/intellij/packaging/artifacts/Artifact.java b/java/compiler/openapi/src/com/intellij/packaging/artifacts/Artifact.java index bfda83b02a3c..8155cf05af6e 100644 --- a/java/compiler/openapi/src/com/intellij/packaging/artifacts/Artifact.java +++ b/java/compiler/openapi/src/com/intellij/packaging/artifacts/Artifact.java @@ -46,4 +46,7 @@ public interface Artifact extends UserDataHolder { @Nullable VirtualFile getOutputFile(); + + @Nullable + String getOutputFilePath(); } diff --git a/resources/src/idea/IdeaActions.xml b/resources/src/idea/IdeaActions.xml index 5d07dcd316b7..840522251c3c 100644 --- a/resources/src/idea/IdeaActions.xml +++ b/resources/src/idea/IdeaActions.xml @@ -197,6 +197,7 @@ +