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 1012f63d39c2..e6cb2a3ed94e 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java +++ b/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java @@ -40,6 +40,7 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.util.ThrowableComputable; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.StreamUtil; import com.intellij.openapi.vfs.VfsUtil; @@ -176,7 +177,12 @@ public class SaveProjectAsTemplateAction extends AnAction { @Override public InputStream getContent(final File file) throws IOException { if (virtualFile.getFileType().isBinary() || PROJECT_TEMPLATE_XML.equals(virtualFile.getName())) return STANDARD.getContent(file); - String result = getEncodedContent(virtualFile, project, parameters); + String result = ApplicationManager.getApplication().runReadAction(new ThrowableComputable() { + @Override + public String compute() throws IOException { + return getEncodedContent(virtualFile, project, parameters); + } + }); return new ByteArrayInputStream(result.getBytes(TemplateModuleBuilder.UTF_8)); } }); diff --git a/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java b/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java index 8407c9372b3c..61bf78feed89 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java +++ b/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java @@ -214,9 +214,9 @@ public class TemplateModuleBuilder extends ModuleBuilder { }; ZipUtil.unzip(ProgressManager.getInstance().getProgressIndicator(), dir, zipInputStream, pathConvertor, new ZipUtil.ContentProcessor() { @Override - public byte[] processContent(byte[] content, String fileName) throws IOException { - FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(FileUtilRt.getExtension(fileName)); - return fileType.isBinary() ? content : processTemplates(projectName, new String(content)); + public byte[] processContent(byte[] content, File file) throws IOException { + FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(FileUtilRt.getExtension(file.getName())); + return fileType.isBinary() ? content : processTemplates(projectName, new String(content), file); } }); String iml = ContainerUtil.find(dir.list(), new Condition() { @@ -250,7 +250,14 @@ public class TemplateModuleBuilder extends ModuleBuilder { return "/" + value.replace('.', '/') + "/"; } - private byte[] processTemplates(@Nullable String projectName, String s) throws IOException { + @SuppressWarnings("UseOfPropertiesAsHashtable") + @Nullable + private byte[] processTemplates(@Nullable String projectName, String content, File file) throws IOException { + for (WizardInputField field : myAdditionalFields) { + if (!field.acceptFile(file)) { + return null; + } + } Properties properties = FileTemplateManager.getInstance().getDefaultProperties(); for (WizardInputField field : myAdditionalFields) { properties.putAll(field.getValues()); @@ -258,7 +265,7 @@ public class TemplateModuleBuilder extends ModuleBuilder { if (projectName != null) { properties.put(ProjectTemplateParameterFactory.IJ_PROJECT_NAME, projectName); } - String merged = FileTemplateUtil.mergeTemplate(properties, s, true); + String merged = FileTemplateUtil.mergeTemplate(properties, content, true); return merged.replace("\\$", "$").replace("\\#", "#").getBytes(UTF_8); } diff --git a/platform/lang-api/src/com/intellij/ide/util/projectWizard/WizardInputField.java b/platform/lang-api/src/com/intellij/ide/util/projectWizard/WizardInputField.java index b7df25646e8a..2101687fd358 100644 --- a/platform/lang-api/src/com/intellij/ide/util/projectWizard/WizardInputField.java +++ b/platform/lang-api/src/com/intellij/ide/util/projectWizard/WizardInputField.java @@ -16,8 +16,10 @@ package com.intellij.ide.util.projectWizard; import com.intellij.openapi.options.ConfigurationException; +import org.jetbrains.annotations.TestOnly; import javax.swing.*; +import java.io.File; import java.util.Collections; import java.util.Map; @@ -69,4 +71,13 @@ public abstract class WizardInputField { public void addToSettings(SettingsStep settingsStep) { settingsStep.addSettingsField(getLabel(), getComponent()); } + + public boolean acceptFile(File file) { + return true; + } + + @TestOnly + public void setValue(String value) { + throw new UnsupportedOperationException(); + } } diff --git a/platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java b/platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java index e773e40818ea..a1ea436f0959 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java +++ b/platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java @@ -42,4 +42,8 @@ public abstract class ProjectTemplateFileProcessor { } return content; } + + protected static String wrap(String param) { + return "${" + param + "}"; + } } 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 7bdcd91cc002..cc3acc9262d1 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 @@ -22,12 +22,15 @@ import java.util.zip.ZipInputStream; /** * @author Sergey Simonchik */ +@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") public class ZipUtil { private static final Logger LOG = Logger.getInstance(ZipUtil.class); public interface ContentProcessor { - byte[] processContent(byte[] content, String fileName) throws IOException; + /** Return null to skip the file */ + @Nullable + byte[] processContent(byte[] content, File file) throws IOException; } public static void unzipWithProgressSynchronously( @@ -109,17 +112,23 @@ public class ZipUtil { if (progress != null) { progress.setText("Extracting " + relativeExtractPath + " ..."); } - FileOutputStream fileOutputStream = new FileOutputStream(child); + FileOutputStream fileOutputStream = null; try { if (contentProcessor == null) { + fileOutputStream = new FileOutputStream(child); FileUtil.copy(stream, fileOutputStream); } else { - byte[] content = contentProcessor.processContent(FileUtil.loadBytes(stream), child.getName()); - fileOutputStream.write(content); + byte[] content = contentProcessor.processContent(FileUtil.loadBytes(stream), child); + if (content != null) { + fileOutputStream = new FileOutputStream(child); + fileOutputStream.write(content); + } } } finally { - fileOutputStream.close(); + if (fileOutputStream != null) { + fileOutputStream.close(); + } } LOG.info("Extract: " + relativeExtractPath); } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java index 9af5a3b9c28d..24e487b30d7d 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java @@ -51,11 +51,11 @@ public class MavenTemplateFileProcessor extends ProjectTemplateFileProcessor { String text = psiFile.getText(); XmlElement element = model.getName().getXmlElement(); if (element instanceof XmlTag) { - text = ((XmlTag)element).getValue().getTextRange().replace(text, "${" + ProjectTemplateParameterFactory.IJ_PROJECT_NAME + "}"); + text = ((XmlTag)element).getValue().getTextRange().replace(text, wrap(ProjectTemplateParameterFactory.IJ_PROJECT_NAME)); } element = model.getArtifactId().getXmlElement(); if (element instanceof XmlTag) { - text = ((XmlTag)element).getValue().getTextRange().replace(text, "${" + ProjectTemplateParameterFactory.IJ_PROJECT_NAME + "}"); + text = ((XmlTag)element).getValue().getTextRange().replace(text, wrap(ProjectTemplateParameterFactory.IJ_PROJECT_NAME)); } return text; }