From f13a6480580149a8d12c829e8fcd43cdc0b7f243 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Mon, 18 Feb 2013 14:03:58 +0400 Subject: [PATCH] save project as template: custom file processing --- .../SaveProjectAsTemplateAction.java | 76 +++++++++++-------- .../templates/SystemFileProcessor.java | 76 +++++++++++++++++++ .../templates/TemplateModuleBuilder.java | 11 +-- .../ProjectTemplateFileProcessor.java | 46 +++++++++++ .../impl/PsiAwareFileEditorManagerImpl.java | 13 +++- .../ProjectTemplateComponent.java | 25 ++++++ .../impl/FileEditorManagerImpl.java | 9 ++- .../src/META-INF/LangExtensionPoints.xml | 3 + .../utils/MavenTemplateFileProcessor.java | 66 ++++++++++++++++ .../src/main/resources/META-INF/plugin.xml | 1 + .../utils/MavenTemplateFileProcessorTest.java | 76 +++++++++++++++++++ resources/src/idea/RichPlatformPlugin.xml | 1 + 12 files changed, 361 insertions(+), 42 deletions(-) create mode 100644 java/idea-ui/src/com/intellij/platform/templates/SystemFileProcessor.java create mode 100644 platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java create mode 100644 platform/platform-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateComponent.java create mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java create mode 100644 plugins/maven/src/test/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessorTest.java 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 c873488d406e..c0ec401a9b04 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java +++ b/java/idea-ui/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java @@ -19,6 +19,7 @@ import com.intellij.CommonBundle; import com.intellij.codeInspection.defaultFileTemplateUsage.FileHeaderChecker; import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; +import com.intellij.ide.util.projectWizard.ProjectTemplateFileProcessor; import com.intellij.ide.util.projectWizard.ProjectTemplateParameterFactory; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; @@ -118,20 +119,7 @@ public class SaveProjectAsTemplateAction extends AnAction { boolean replaceParameters, final ProgressIndicator indicator) { - final Map parameters = new HashMap(); - if (replaceParameters) { - ApplicationManager.getApplication().runReadAction(new Runnable() { - public void run() { - ProjectTemplateParameterFactory[] extensions = Extensions.getExtensions(ProjectTemplateParameterFactory.EP_NAME); - for (ProjectTemplateParameterFactory extension : extensions) { - String value = extension.detectParameterValue(project); - if (value != null) { - parameters.put(value, extension.getParameterId()); - } - } - } - }); - } + final Map parameters = computeParameters(project, replaceParameters); indicator.setText("Saving project..."); UIUtil.invokeAndWaitIfNeeded(new Runnable() { @Override @@ -180,41 +168,32 @@ public class SaveProjectAsTemplateAction extends AnAction { : ModuleRootManager.getInstance(moduleToSave).getFileIndex(); final ZipOutputStream finalStream = stream; - final FileTemplate template = FileTemplateManager.getInstance().getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME); - final String templateText = template.getText(); - final Pattern pattern = FileHeaderChecker.getTemplatePattern(template, project, new TIntObjectHashMap()); - index.iterateContent(new ContentIterator() { @Override public boolean processFile(final VirtualFile virtualFile) { if (!virtualFile.isDirectory()) { - String name = virtualFile.getName(); - indicator.setText2(name); + final String fileName = virtualFile.getName(); + indicator.setText2(fileName); try { String relativePath = VfsUtilCore.getRelativePath(virtualFile, dir, '/'); if (relativePath == null) { throw new RuntimeException("Can't find relative path for " + virtualFile); } - boolean system = ".idea".equals(virtualFile.getParent().getName()); + final boolean system = ".idea".equals(virtualFile.getParent().getName()); if (system) { - if (!name.equals("description.html") && - !name.equals("misc.xml") && - !name.equals("modules.xml") && - !name.equals("workspace.xml")) { + if (!fileName.equals("description.html") && + !fileName.equals("misc.xml") && + !fileName.equals("modules.xml") && + !fileName.equals("workspace.xml")) { return true; } } ZipUtil.addFileToZip(finalStream, new File(virtualFile.getPath()), dir.getName() + "/" + relativePath, null, null, new ZipUtil.FileContentProcessor() { @Override - public InputStream getContent(File file) throws IOException { + public InputStream getContent(final File file) throws IOException { if (virtualFile.getFileType().isBinary()) return STANDARD.getContent(file); - - String s = VfsUtilCore.loadText(virtualFile); - String result = convertTemplates(s, pattern, templateText); - for (Map.Entry entry : parameters.entrySet()) { - result = result.replace(entry.getKey(), "${" + entry.getValue() + "}"); - } + String result = getEncodedContent(virtualFile, project, parameters); return new ByteArrayInputStream(result.getBytes(TemplateModuleBuilder.UTF_8)); } }); @@ -241,6 +220,39 @@ public class SaveProjectAsTemplateAction extends AnAction { } } + public static Map computeParameters(final Project project, boolean replaceParameters) { + final Map parameters = new HashMap(); + if (replaceParameters) { + ApplicationManager.getApplication().runReadAction(new Runnable() { + public void run() { + ProjectTemplateParameterFactory[] extensions = Extensions.getExtensions(ProjectTemplateParameterFactory.EP_NAME); + for (ProjectTemplateParameterFactory extension : extensions) { + String value = extension.detectParameterValue(project); + if (value != null) { + parameters.put(value, extension.getParameterId()); + } + } + } + }); + } + return parameters; + } + + public static String getEncodedContent(VirtualFile virtualFile, + Project project, + Map parameters) throws IOException { + final FileTemplate template = FileTemplateManager.getInstance().getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME); + final String templateText = template.getText(); + final Pattern pattern = FileHeaderChecker.getTemplatePattern(template, project, new TIntObjectHashMap()); + String text = VfsUtilCore.loadText(virtualFile); + String result = convertTemplates(text, pattern, templateText); + result = ProjectTemplateFileProcessor.encodeFile(result, virtualFile, project); + for (Map.Entry entry : parameters.entrySet()) { + result = result.replace(entry.getKey(), "${" + entry.getValue() + "}"); + } + return result; + } + private static VirtualFile getDirectoryToSave(Project project, @Nullable Module module) { if (module == null) { return project.getBaseDir(); diff --git a/java/idea-ui/src/com/intellij/platform/templates/SystemFileProcessor.java b/java/idea-ui/src/com/intellij/platform/templates/SystemFileProcessor.java new file mode 100644 index 000000000000..827997ac73a0 --- /dev/null +++ b/java/idea-ui/src/com/intellij/platform/templates/SystemFileProcessor.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2013 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.platform.templates; + +import com.intellij.ide.util.projectWizard.ProjectTemplateComponent; +import com.intellij.ide.util.projectWizard.ProjectTemplateFileProcessor; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.JDOMExternalizable; +import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.util.WriteExternalException; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.ui.UIUtil; +import org.jdom.Element; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.util.List; + +/** + * @author Dmitry Avdeev + * Date: 2/15/13 + */ +public class SystemFileProcessor extends ProjectTemplateFileProcessor { + @Nullable + @Override + protected String encodeFileText(String content, VirtualFile file, Project project) throws IOException { + final String fileName = file.getName(); + if (file.getParent().getName().equals(".idea") && fileName.equals("workspace.xml")) { + ProjectTemplateComponent[] components = project.getComponents(ProjectTemplateComponent.class); + List componentList = ContainerUtil.filter(components, new Condition() { + @Override + public boolean value(ProjectTemplateComponent component) { + return fileName.equals(component.getStorageFile()); + } + }); + if (!componentList.isEmpty()) { + Element root = new Element("project"); + for (final ProjectTemplateComponent component : componentList) { + final Element element = new Element("component"); + root.addContent(element); + UIUtil.invokeAndWaitIfNeeded(new Runnable() { + @Override + public void run() { + try { + ((JDOMExternalizable)component).writeExternal(element); + } + catch (WriteExternalException ignore) { + LOG.error(ignore); + } + } + }); + } + return JDOMUtil.writeElement(root); + } + } + return null; + } + + private static final Logger LOG = Logger.getInstance(SystemFileProcessor.class); +} 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 76db679edddf..1feb83814aa6 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java +++ b/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java @@ -154,7 +154,7 @@ public class TemplateModuleBuilder extends ModuleBuilder { public Module createModule(@NotNull ModifiableModuleModel moduleModel) throws InvalidDataException, IOException, ModuleWithNameAlreadyExists, JDOMException, ConfigurationException { final String path = getContentEntryPath(); - unzip(path, true); + unzip(null, path, true); Module module = ImportImlMode.setUpLoader(getModuleFilePath()).createModule(moduleModel); if (myProjectMode) { moduleModel.renameModule(module, module.getProject().getName()); @@ -181,7 +181,7 @@ public class TemplateModuleBuilder extends ModuleBuilder { return null; } - private void unzip(String path, final boolean moduleMode) { + private void unzip(final String name, String path, final boolean moduleMode) { File dir = new File(path); ZipInputStream zipInputStream = null; final WizardInputField basePackage = getBasePackageField(); @@ -202,7 +202,7 @@ public class TemplateModuleBuilder extends ModuleBuilder { @Override public byte[] processContent(byte[] content, String fileName) throws IOException { FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(FileUtilRt.getExtension(fileName)); - return fileType.isBinary() ? content : processTemplates(new String(content)); + return fileType.isBinary() ? content : processTemplates(name, new String(content)); } }); String iml = ContainerUtil.find(dir.list(), new Condition() { @@ -236,11 +236,12 @@ public class TemplateModuleBuilder extends ModuleBuilder { return "/" + value.replace('.', '/') + "/"; } - private byte[] processTemplates(String s) throws IOException { + private byte[] processTemplates(String projectName, String s) throws IOException { Properties properties = FileTemplateManager.getInstance().getDefaultProperties(); for (WizardInputField field : myAdditionalFields) { properties.putAll(field.getValues()); } + properties.put(ProjectTemplateParameterFactory.IJ_PROJECT_NAME, projectName); String merged = FileTemplateUtil.mergeTemplate(properties, s, true); return merged.replace("\\$", "$").replace("\\#", "#").getBytes(UTF_8); } @@ -249,7 +250,7 @@ public class TemplateModuleBuilder extends ModuleBuilder { @Override public Project createProject(String name, final String path) { myProjectMode = true; - unzip(path, false); + unzip(name, path, false); return ApplicationManager.getApplication().runWriteAction(new NullableComputable() { @Nullable @Override 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 new file mode 100644 index 000000000000..6d06db0daf20 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateFileProcessor.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2013 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.ide.util.projectWizard; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; + +/** + * @author Dmitry Avdeev + * Date: 2/15/13 + */ +public abstract class ProjectTemplateFileProcessor { + + public static final ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.projectTemplateFileProcessor"); + + /** Return null if it can't be processed */ + @Nullable + protected abstract String encodeFileText(String content, VirtualFile file, Project project) throws IOException; + + public static String encodeFile(String content, VirtualFile file, Project project) throws IOException { + ProjectTemplateFileProcessor[] processors = EP_NAME.getExtensions(); + for (ProjectTemplateFileProcessor processor : processors) { + String text = processor.encodeFileText(content, file, project); + if (text != null) return text; + } + return VfsUtilCore.loadText(file); + } +} diff --git a/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/PsiAwareFileEditorManagerImpl.java b/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/PsiAwareFileEditorManagerImpl.java index b1e03fe9a3f5..2be64b748421 100644 --- a/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/PsiAwareFileEditorManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/PsiAwareFileEditorManagerImpl.java @@ -18,16 +18,15 @@ package com.intellij.openapi.fileEditor.impl; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider; import com.intellij.openapi.module.Module; -import com.intellij.openapi.module.ModuleUtil; import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.io.FileUtil; - import com.intellij.openapi.vfs.VirtualFile; import com.intellij.problems.WolfTheProblemSolver; import com.intellij.psi.*; @@ -152,8 +151,14 @@ public class PsiAwareFileEditorManagerImpl extends FileEditorManagerImpl { private void doChange(final PsiTreeChangeEvent event) { final PsiFile psiFile = event.getFile(); + if (psiFile == null) return; + VirtualFile file = psiFile.getVirtualFile(); + if (file == null) return; + FileEditor[] editors = getAllEditors(file); + if (editors.length == 0) return; + final VirtualFile currentFile = getCurrentFile(); - if (currentFile != null && psiFile != null && Comparing.equal(psiFile.getVirtualFile(), currentFile)) { + if (currentFile != null && Comparing.equal(psiFile.getVirtualFile(), currentFile)) { updateFileIcon(currentFile); } } diff --git a/platform/platform-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateComponent.java b/platform/platform-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateComponent.java new file mode 100644 index 000000000000..9efdfa72b335 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ide/util/projectWizard/ProjectTemplateComponent.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2013 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.ide.util.projectWizard; + +/** + * @author Dmitry Avdeev + * Date: 2/15/13 + */ +public interface ProjectTemplateComponent { + + String getStorageFile(); +} diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java index c805e3e94ee9..5017b9e4b2a3 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java @@ -20,6 +20,7 @@ import com.intellij.ide.IdeBundle; import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.ui.UISettings; import com.intellij.ide.ui.UISettingsListener; +import com.intellij.ide.util.projectWizard.ProjectTemplateComponent; import com.intellij.injected.editor.VirtualFileWindow; import com.intellij.openapi.Disposable; import com.intellij.openapi.application.ApplicationManager; @@ -93,7 +94,7 @@ import java.util.List; * @author Eugene Belyaev * @author Vladimir Kondratyev */ -public class FileEditorManagerImpl extends FileEditorManagerEx implements ProjectComponent, JDOMExternalizable { +public class FileEditorManagerImpl extends FileEditorManagerEx implements ProjectComponent, JDOMExternalizable, ProjectTemplateComponent { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl"); private static final Key WATCH_REQUEST_KEY = Key.create("WATCH_REQUEST_KEY"); private static final Key DUMB_AWARE = Key.create("DUMB_AWARE"); @@ -251,6 +252,11 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Projec } } + @Override + public String getStorageFile() { + return "workspace.xml"; + } + private static class MyBorder implements Border { @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { @@ -1175,6 +1181,7 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Projec @Override public FileEditor[] getAllEditors(@NotNull VirtualFile file) { List editorComposites = getEditorComposites(file); + if (editorComposites.isEmpty()) return EMPTY_EDITOR_ARRAY; List editors = new ArrayList(); for (EditorWithProviderComposite composite : editorComposites) { ContainerUtil.addAll(editors, composite.getEditors()); diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index f286872f6f26..8373ebfefcf3 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -605,6 +605,9 @@ + + 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 new file mode 100644 index 000000000000..9af5a3b9c28d --- /dev/null +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessor.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2013 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 org.jetbrains.idea.maven.utils; + +import com.intellij.ide.highlighter.XmlFileType; +import com.intellij.ide.util.projectWizard.ProjectTemplateFileProcessor; +import com.intellij.ide.util.projectWizard.ProjectTemplateParameterFactory; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.ThrowableComputable; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiFileFactory; +import com.intellij.psi.xml.XmlElement; +import com.intellij.psi.xml.XmlTag; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.idea.maven.dom.MavenDomUtil; +import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel; + +import java.io.IOException; + +/** + * @author Dmitry Avdeev + * Date: 2/15/13 + */ +public class MavenTemplateFileProcessor extends ProjectTemplateFileProcessor { + + @Nullable + @Override + protected String encodeFileText(final String content, final VirtualFile file, final Project project) throws IOException { + if ("pom.xml".equals(file.getName())) { + return ApplicationManager.getApplication().runReadAction(new ThrowableComputable() { + @Override + public String compute() throws IOException { + PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("pom.xml", XmlFileType.INSTANCE, content); + final MavenDomProjectModel model = MavenDomUtil.getMavenDomModel(psiFile, MavenDomProjectModel.class); + if (model == null) return null; + String text = psiFile.getText(); + XmlElement element = model.getName().getXmlElement(); + if (element instanceof XmlTag) { + text = ((XmlTag)element).getValue().getTextRange().replace(text, "${" + ProjectTemplateParameterFactory.IJ_PROJECT_NAME + "}"); + } + element = model.getArtifactId().getXmlElement(); + if (element instanceof XmlTag) { + text = ((XmlTag)element).getValue().getTextRange().replace(text, "${" + ProjectTemplateParameterFactory.IJ_PROJECT_NAME + "}"); + } + return text; + } + }); + } + return null; + } +} diff --git a/plugins/maven/src/main/resources/META-INF/plugin.xml b/plugins/maven/src/main/resources/META-INF/plugin.xml index 2dbdbf8d81b0..90ac5621bc5e 100644 --- a/plugins/maven/src/main/resources/META-INF/plugin.xml +++ b/plugins/maven/src/main/resources/META-INF/plugin.xml @@ -65,6 +65,7 @@ + diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessorTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessorTest.java new file mode 100644 index 000000000000..ccbdf4c4f959 --- /dev/null +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/utils/MavenTemplateFileProcessorTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2013 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 org.jetbrains.idea.maven.utils; + +import com.intellij.ide.util.projectWizard.ProjectTemplateParameterFactory; +import com.intellij.platform.templates.SaveProjectAsTemplateAction; +import com.intellij.psi.PsiFile; +import com.intellij.testFramework.IdeaTestCase; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; + +import java.util.Map; + +/** + * @author Dmitry Avdeev + * Date: 2/15/13 + */ +public class MavenTemplateFileProcessorTest extends LightPlatformCodeInsightFixtureTestCase { + + private static final String TEXT = + "\n" + + " 4.0.0\n" + + " com.springapp\n" + + " springapp\n" + + " jar\n" + + " 1.0-SNAPSHOT\n" + + " SpringApp\n" + + ""; + + public MavenTemplateFileProcessorTest() { + IdeaTestCase.initPlatformPrefix(); + } + + public void testProcessor() throws Exception { + PsiFile file = myFixture.configureByText("pom.xml", TEXT); + String s = new MavenTemplateFileProcessor().encodeFileText(TEXT, file.getVirtualFile(), getProject()); + assertEquals("\n" + + " 4.0.0\n" + + " com.springapp\n" + + " ${IJ_PROJECT_NAME}\n" + + " jar\n" + + " 1.0-SNAPSHOT\n" + + " ${IJ_PROJECT_NAME}\n" + + "", s); + } + + public void testSaveAsTemplate() throws Exception { + PsiFile file = myFixture.configureByText("pom.xml", TEXT); + Map map = SaveProjectAsTemplateAction.computeParameters(getProject(), true); + map.put("com.springapp", ProjectTemplateParameterFactory.IJ_BASE_PACKAGE); + String content = SaveProjectAsTemplateAction.getEncodedContent(file.getVirtualFile(), getProject(), map); + assertEquals("\n" + + " 4.0.0\n" + + " ${IJ_BASE_PACKAGE}\n" + + " ${IJ_PROJECT_NAME}\n" + + " jar\n" + + " 1.0-SNAPSHOT\n" + + " ${IJ_PROJECT_NAME}\n" + + "", content); + } +} diff --git a/resources/src/idea/RichPlatformPlugin.xml b/resources/src/idea/RichPlatformPlugin.xml index d8d26ecf56e1..1d37d6ffb460 100644 --- a/resources/src/idea/RichPlatformPlugin.xml +++ b/resources/src/idea/RichPlatformPlugin.xml @@ -302,6 +302,7 @@ --> +