From 3c8f18bc0443eff863435dd5ba5bcd9a93295e5e Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Wed, 10 Oct 2012 21:20:16 +0400 Subject: [PATCH] creating empty dirs --- .../templates/ArchivedProjectTemplate.java | 3 +- .../SaveProjectAsTemplateAction.java | 15 +++++++- .../platform/templates/github/ZipUtil.java | 37 ++++++------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/java/idea-ui/src/com/intellij/platform/templates/ArchivedProjectTemplate.java b/java/idea-ui/src/com/intellij/platform/templates/ArchivedProjectTemplate.java index da69165d4af2..019fe8a01d24 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/ArchivedProjectTemplate.java +++ b/java/idea-ui/src/com/intellij/platform/templates/ArchivedProjectTemplate.java @@ -51,6 +51,7 @@ import java.util.zip.ZipInputStream; */ public class ArchivedProjectTemplate implements ProjectTemplate { + static final String DESCRIPTION_PATH = ".idea/description.html"; private final String myDisplayName; private final URL myArchivePath; private final WizardContext myContext; @@ -76,7 +77,7 @@ public class ArchivedProjectTemplate implements ProjectTemplate { ZipInputStream stream = getStream(); ZipEntry entry; while ((entry = stream.getNextEntry()) != null) { - if (entry.getName().endsWith("/description.html")) { + if (entry.getName().endsWith(DESCRIPTION_PATH)) { return StreamUtil.readText(stream); } } diff --git a/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java b/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java index 2a84d13526b2..9b99d7a94c6e 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java +++ b/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java @@ -20,6 +20,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.StreamUtil; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; @@ -29,6 +30,7 @@ import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.IOException; +import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** @@ -41,7 +43,7 @@ public class SaveProjectAsTemplateAction extends AnAction { public void actionPerformed(AnActionEvent e) { Project project = getEventProject(e); assert project != null; - VirtualFile descriptionFile = VfsUtil.findRelativeFile(project.getBaseDir(), "description.html"); + VirtualFile descriptionFile = VfsUtil.findRelativeFile(project.getBaseDir(), ArchivedProjectTemplate.DESCRIPTION_PATH.split("/")); SaveProjectAsTemplateDialog dialog = new SaveProjectAsTemplateDialog(project, descriptionFile); if (dialog.showAndGet()) { File file = dialog.getTemplateFile(); @@ -51,6 +53,15 @@ public class SaveProjectAsTemplateAction extends AnAction { file.createNewFile(); stream = new ZipOutputStream(new FileOutputStream(file)); VirtualFile dir = project.getBaseDir(); + String description = dialog.getDescription(); + if (descriptionFile == null) { + stream.putNextEntry(new ZipEntry(dir.getName() + "/" + ArchivedProjectTemplate.DESCRIPTION_PATH)); + stream.write(description.getBytes()); + stream.closeEntry(); + } + else { + VfsUtil.saveText(descriptionFile, description); + } ZipUtil.addDirToZipRecursively(stream, null, new File(dir.getPath()), dir.getName(), new FileFilter() { @Override public boolean accept(File pathname) { @@ -59,6 +70,8 @@ public class SaveProjectAsTemplateAction extends AnAction { return true; } }, null); + Messages.showInfoMessage(FileUtil.getNameWithoutExtension(file) + " was successfully created.\n" + + "It's available now in Project Wizard", "Template Created"); } catch (IOException ex) { Messages.showErrorDialog(project, ex.getMessage(), "Error"); diff --git a/platform/lang-impl/src/com/intellij/platform/templates/github/ZipUtil.java b/platform/lang-impl/src/com/intellij/platform/templates/github/ZipUtil.java index 1163b5e073da..043889bcaf8d 100644 --- a/platform/lang-impl/src/com/intellij/platform/templates/github/ZipUtil.java +++ b/platform/lang-impl/src/com/intellij/platform/templates/github/ZipUtil.java @@ -99,17 +99,12 @@ public class ZipUtil { @NotNull final ZipEntry zipEntry, @NotNull final File extractToDir, ZipInputStream stream) throws IOException { - final char pathDelimiterChar = '/'; - String relativeExtractPath = createRelativeExtractPath(zipEntry, pathDelimiterChar, true); - int ind = relativeExtractPath.lastIndexOf(pathDelimiterChar); - final String relativeParentDir; - final String name; - if (ind != -1) { - relativeParentDir = relativeExtractPath.substring(0, ind); - name = relativeExtractPath.substring(ind + 1); - } else { - relativeParentDir = ""; - name = relativeExtractPath; + + String relativeExtractPath = createRelativeExtractPath(zipEntry); + File child = new File(extractToDir, relativeExtractPath); + File dir = zipEntry.isDirectory() ? child : child.getParentFile(); + if (!dir.exists() && !dir.mkdirs()) { + throw new IOException("Unable to create dir: '" + dir + "'!"); } if (zipEntry.isDirectory()) { return; @@ -117,14 +112,6 @@ public class ZipUtil { if (progress != null) { progress.setText("Extracting " + relativeExtractPath + " ..."); } - File parentDir = new File(extractToDir, relativeParentDir); - if (!parentDir.exists() || !parentDir.isDirectory()) { - boolean created = parentDir.mkdirs(); - if (!created) { - throw new RuntimeException("Unable to create dir: '" + parentDir + "'!"); - } - } - File child = new File(parentDir, name); FileOutputStream fileOutputStream = new FileOutputStream(child); try { FileUtil.copy(stream, fileOutputStream); @@ -134,15 +121,13 @@ public class ZipUtil { LOG.info("Extract: " + relativeExtractPath); } - private static String createRelativeExtractPath(ZipEntry zipEntry, char pathDelimiterChar, boolean singleTopLevelDir) { + private static String createRelativeExtractPath(ZipEntry zipEntry) { String name = zipEntry.getName(); - if (singleTopLevelDir) { - int ind = name.indexOf(pathDelimiterChar); - if (ind >= 0) { - name = name.substring(ind + 1); - } + int ind = name.indexOf('/'); + if (ind >= 0) { + name = name.substring(ind + 1); } - return StringUtil.trimEnd(name, String.valueOf(pathDelimiterChar)); + return StringUtil.trimEnd(name, "/"); } }