'package file' action executed in a separate thread and some logging added

This commit is contained in:
nik
2010-06-25 10:44:01 +04:00
parent 5f4c8cd18b
commit de69e72bcd
2 changed files with 45 additions and 14 deletions
@@ -3,17 +3,24 @@
*/
package com.intellij.packaging.impl.ui.actions;
import com.intellij.CommonBundle;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.compiler.CompilerBundle;
import com.intellij.openapi.compiler.CompilerManager;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
@@ -76,23 +83,39 @@ public class PackageFileAction extends AnAction {
FileDocumentManager.getInstance().saveAllDocuments();
final List<VirtualFile> files = getFilesToPackage(event, project);
try {
for (VirtualFile file : files) {
PackageFileWorker.packageFile(file, project);
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Packaging Files") {
@Override
public void run(@NotNull ProgressIndicator indicator) {
for (final VirtualFile file : files) {
indicator.checkCanceled();
new ReadAction() {
protected void run(final Result result) {
try {
PackageFileWorker.packageFile(file, project);
}
catch (IOException e) {
Notifications.Bus.notify(
new Notification("Package File", "Cannot package file", CompilerBundle.message("message.tect.package.file.io.error", e.toString()),
NotificationType.ERROR));
}
}
}.execute();
}
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
setStatusText(project, files);
}
});
}
setStatusText(project, files);
}
catch (IOException e) {
Messages.showErrorDialog(CompilerBundle.message("message.tect.package.file.io.error", e), CommonBundle.getErrorTitle());
}
});
}
private static void setStatusText(Project project, List<VirtualFile> files) {
if (!files.isEmpty()) {
String fileNames = "";
StringBuilder fileNames = new StringBuilder();
for (VirtualFile file : files) {
if (fileNames.length() != 0) fileNames += ", ";
fileNames+="'"+file.getName()+"'";
if (fileNames.length() != 0) fileNames.append(", ");
fileNames.append("'").append(file.getName()).append("'");
}
String time = DateFormat.getTimeInstance().format(new Date());
final String statusText = CompilerBundle.message("status.text.file.has.been.packaged", files.size(), fileNames, time);
@@ -16,6 +16,7 @@
package com.intellij.packaging.impl.ui.actions;
import com.intellij.openapi.deployment.DeploymentUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Trinity;
import com.intellij.openapi.util.io.FileUtil;
@@ -44,6 +45,7 @@ import java.util.List;
* @author nik
*/
public class PackageFileWorker {
private static final Logger LOG = Logger.getInstance("#com.intellij.packaging.impl.ui.actions.PackageFileWorker");
private final File myFile;
private final String myRelativeOutputPath;
@@ -53,6 +55,7 @@ public class PackageFileWorker {
}
public static void packageFile(@NotNull VirtualFile file, @NotNull Project project) throws IOException {
LOG.debug("Start packaging file: " + file.getPath());
final Collection<Trinity<Artifact, PackagingElementPath, String>> items = ArtifactUtil.findContainingArtifactsWithOutputPaths(file, project);
File ioFile = VfsUtil.virtualToIoFile(file);
for (Trinity<Artifact, PackagingElementPath, String> item : items) {
@@ -60,6 +63,7 @@ public class PackageFileWorker {
final String outputPath = artifact.getOutputPath();
if (!StringUtil.isEmpty(outputPath)) {
PackageFileWorker worker = new PackageFileWorker(ioFile, item.getThird());
LOG.debug(" package to " + outputPath);
worker.packageFile(outputPath, item.getSecond().getParents());
}
}
@@ -76,7 +80,9 @@ public class PackageFileWorker {
private void copyFile(String outputPath, List<CompositePackagingElement<?>> parents) throws IOException {
if (parents.isEmpty()) {
FileUtil.copy(myFile, new File(FileUtil.toSystemDependentName(DeploymentUtil.appendToPath(outputPath, myRelativeOutputPath))));
final String fullOutputPath = DeploymentUtil.appendToPath(outputPath, myRelativeOutputPath);
LOG.debug(" copying to " + fullOutputPath);
FileUtil.copy(myFile, new File(FileUtil.toSystemDependentName(fullOutputPath)));
return;
}
@@ -94,6 +100,7 @@ public class PackageFileWorker {
private void packFile(String archivePath, String pathInArchive, List<CompositePackagingElement<?>> parents) throws IOException {
final File archiveFile = new File(FileUtil.toSystemDependentName(archivePath));
if (parents.isEmpty()) {
LOG.debug(" adding to archive " + archivePath);
JBZipFile file = getOrCreateZipFile(archiveFile);
try {
final String fullPathInArchive = DeploymentUtil.trimForwardSlashes(DeploymentUtil.appendToPath(pathInArchive, myRelativeOutputPath));
@@ -113,6 +120,7 @@ public class PackageFileWorker {
JBZipFile zipFile = getOrCreateZipFile(archiveFile);
try {
final JBZipEntry entry = zipFile.getOrCreateEntry(nextPathInArchive);
LOG.debug(" extracting to temp file: " + nextPathInArchive + " from " + archivePath);
final File tempFile = FileUtil.createTempFile("packageFile" + FileUtil.sanitizeFileName(nextPathInArchive), FileUtil.getExtension(PathUtil.getFileName(nextPathInArchive)));
if (entry.getSize() != -1) {
FileUtil.writeToFile(tempFile, entry.getData());