mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Merge branch 'leneshka/template-projects'
This commit is contained in:
@@ -52,9 +52,6 @@ public class RemoteTemplatesFactory extends ProjectTemplatesFactory {
|
||||
|
||||
private static final String URL = "http://download.jetbrains.com/idea/project_templates/";
|
||||
|
||||
public static final String TEMPLATE = "template";
|
||||
public static final String INPUT_DEFAULT = "default";
|
||||
|
||||
private final ClearableLazyValue<MultiMap<String, ArchivedProjectTemplate>> myTemplates = new ClearableLazyValue<MultiMap<String, ArchivedProjectTemplate>>() {
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -111,7 +108,8 @@ public class RemoteTemplatesFactory extends ProjectTemplatesFactory {
|
||||
}
|
||||
|
||||
private static List<ArchivedProjectTemplate> createGroupTemplates(Element groupElement) {
|
||||
return ContainerUtil.mapNotNull(groupElement.getChildren(TEMPLATE), (NullableFunction<Element, ArchivedProjectTemplate>)element -> {
|
||||
List<Element> children = groupElement.getChildren(ArchivedProjectTemplate.TEMPLATE);
|
||||
return ContainerUtil.mapNotNull(children, (NullableFunction<Element, ArchivedProjectTemplate>)element -> {
|
||||
if (!checkRequiredPlugins(element)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
+6
-62
@@ -19,19 +19,20 @@ import com.intellij.codeInsight.CodeInsightUtil;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateUtil;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import gnu.trove.TIntObjectHashMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -45,7 +46,7 @@ public class FileHeaderChecker {
|
||||
static ProblemDescriptor checkFileHeader(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean onTheFly) {
|
||||
TIntObjectHashMap<String> offsetToProperty = new TIntObjectHashMap<String>();
|
||||
FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
|
||||
Pattern pattern = getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
|
||||
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
|
||||
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
|
||||
if (!matcher.matches()) {
|
||||
return null;
|
||||
@@ -61,15 +62,6 @@ public class FileHeaderChecker {
|
||||
return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
|
||||
}
|
||||
|
||||
public static Pattern getTemplatePattern(@NotNull FileTemplate template,
|
||||
@NotNull Project project,
|
||||
@NotNull TIntObjectHashMap<String> offsetToProperty) {
|
||||
String templateText = template.getText().trim();
|
||||
String regex = templateToRegex(templateText, offsetToProperty, project);
|
||||
regex = StringUtil.replace(regex, "with", "(?:with|by)");
|
||||
regex = ".*(" + regex + ").*";
|
||||
return Pattern.compile(regex, Pattern.DOTALL);
|
||||
}
|
||||
|
||||
private static Properties computeProperties(final Matcher matcher, final TIntObjectHashMap<String> offsetToProperty, Project project) {
|
||||
Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());
|
||||
@@ -123,52 +115,4 @@ public class FileHeaderChecker {
|
||||
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
|
||||
return template.isDefault() ? new LocalQuickFix[]{editFileTemplateFix} : new LocalQuickFix[]{replaceTemplateFix, editFileTemplateFix};
|
||||
}
|
||||
|
||||
private static String templateToRegex(String text, TIntObjectHashMap<String> offsetToProperty, Project project) {
|
||||
List<Object> properties = ContainerUtil.newArrayList(FileTemplateManager.getInstance(project).getDefaultProperties().keySet());
|
||||
properties.add("PACKAGE_NAME");
|
||||
|
||||
String regex = escapeRegexChars(text);
|
||||
// first group is a whole file header
|
||||
int groupNumber = 1;
|
||||
for (Object property : properties) {
|
||||
String name = property.toString();
|
||||
String escaped = escapeRegexChars("${" + name + "}");
|
||||
boolean first = true;
|
||||
for (int i = regex.indexOf(escaped); i != -1 && i < regex.length(); i = regex.indexOf(escaped, i + 1)) {
|
||||
String replacement = first ? "([^\\n]*)" : "\\" + groupNumber;
|
||||
int delta = escaped.length() - replacement.length();
|
||||
int[] offs = offsetToProperty.keys();
|
||||
for (int off : offs) {
|
||||
if (off > i) {
|
||||
String prop = offsetToProperty.remove(off);
|
||||
offsetToProperty.put(off - delta, prop);
|
||||
}
|
||||
}
|
||||
offsetToProperty.put(i, name);
|
||||
regex = regex.substring(0, i) + replacement + regex.substring(i + escaped.length());
|
||||
if (first) {
|
||||
groupNumber++;
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return regex;
|
||||
}
|
||||
|
||||
private static String escapeRegexChars(String regex) {
|
||||
regex = StringUtil.replace(regex, "|", "\\|");
|
||||
regex = StringUtil.replace(regex, ".", "\\.");
|
||||
regex = StringUtil.replace(regex, "*", "\\*");
|
||||
regex = StringUtil.replace(regex, "+", "\\+");
|
||||
regex = StringUtil.replace(regex, "?", "\\?");
|
||||
regex = StringUtil.replace(regex, "$", "\\$");
|
||||
regex = StringUtil.replace(regex, "(", "\\(");
|
||||
regex = StringUtil.replace(regex, ")", "\\)");
|
||||
regex = StringUtil.replace(regex, "[", "\\[");
|
||||
regex = StringUtil.replace(regex, "]", "\\]");
|
||||
regex = StringUtil.replace(regex, "{", "\\{");
|
||||
regex = StringUtil.replace(regex, "}", "\\}");
|
||||
return regex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import gnu.trove.TIntObjectHashMap;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.exception.VelocityException;
|
||||
import org.apache.velocity.runtime.parser.ParseException;
|
||||
@@ -55,6 +56,7 @@ import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author MYakovlev
|
||||
@@ -413,4 +415,62 @@ public class FileTemplateUtil{
|
||||
newTemplate.setText(content);
|
||||
return newTemplate;
|
||||
}
|
||||
|
||||
public static Pattern getTemplatePattern(@NotNull FileTemplate template,
|
||||
@NotNull Project project,
|
||||
@NotNull TIntObjectHashMap<String> offsetToProperty) {
|
||||
String templateText = template.getText().trim();
|
||||
String regex = templateToRegex(templateText, offsetToProperty, project);
|
||||
regex = StringUtil.replace(regex, "with", "(?:with|by)");
|
||||
regex = ".*(" + regex + ").*";
|
||||
return Pattern.compile(regex, Pattern.DOTALL);
|
||||
}
|
||||
|
||||
private static String templateToRegex(String text, TIntObjectHashMap<String> offsetToProperty, Project project) {
|
||||
List<Object> properties = ContainerUtil.newArrayList(FileTemplateManager.getInstance(project).getDefaultProperties().keySet());
|
||||
properties.add("PACKAGE_NAME");
|
||||
|
||||
String regex = escapeRegexChars(text);
|
||||
// first group is a whole file header
|
||||
int groupNumber = 1;
|
||||
for (Object property : properties) {
|
||||
String name = property.toString();
|
||||
String escaped = escapeRegexChars("${" + name + "}");
|
||||
boolean first = true;
|
||||
for (int i = regex.indexOf(escaped); i != -1 && i < regex.length(); i = regex.indexOf(escaped, i + 1)) {
|
||||
String replacement = first ? "([^\\n]*)" : "\\" + groupNumber;
|
||||
int delta = escaped.length() - replacement.length();
|
||||
int[] offs = offsetToProperty.keys();
|
||||
for (int off : offs) {
|
||||
if (off > i) {
|
||||
String prop = offsetToProperty.remove(off);
|
||||
offsetToProperty.put(off - delta, prop);
|
||||
}
|
||||
}
|
||||
offsetToProperty.put(i, name);
|
||||
regex = regex.substring(0, i) + replacement + regex.substring(i + escaped.length());
|
||||
if (first) {
|
||||
groupNumber++;
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return regex;
|
||||
}
|
||||
|
||||
private static String escapeRegexChars(String regex) {
|
||||
regex = StringUtil.replace(regex, "|", "\\|");
|
||||
regex = StringUtil.replace(regex, ".", "\\.");
|
||||
regex = StringUtil.replace(regex, "*", "\\*");
|
||||
regex = StringUtil.replace(regex, "+", "\\+");
|
||||
regex = StringUtil.replace(regex, "?", "\\?");
|
||||
regex = StringUtil.replace(regex, "$", "\\$");
|
||||
regex = StringUtil.replace(regex, "(", "\\(");
|
||||
regex = StringUtil.replace(regex, ")", "\\)");
|
||||
regex = StringUtil.replace(regex, "[", "\\[");
|
||||
regex = StringUtil.replace(regex, "]", "\\]");
|
||||
regex = StringUtil.replace(regex, "{", "\\{");
|
||||
regex = StringUtil.replace(regex, "}", "\\}");
|
||||
return regex;
|
||||
}
|
||||
}
|
||||
|
||||
+34
-8
@@ -38,6 +38,10 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.platform.CustomStepProjectGenerator;
|
||||
import com.intellij.platform.DirectoryProjectGenerator;
|
||||
import com.intellij.platform.PlatformProjectOpenProcessor;
|
||||
import com.intellij.platform.ProjectTemplate;
|
||||
import com.intellij.platform.templates.ArchivedTemplatesFactory;
|
||||
import com.intellij.platform.templates.LocalArchivedTemplate;
|
||||
import com.intellij.platform.templates.TemplateProjectDirectoryGenerator;
|
||||
import com.intellij.projectImport.ProjectOpenedCallback;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.NullableConsumer;
|
||||
@@ -48,6 +52,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.platform.ProjectTemplatesFactory.CUSTOM_GROUP;
|
||||
|
||||
|
||||
public class AbstractNewProjectStep extends DefaultActionGroup implements DumbAware {
|
||||
private static final Logger LOG = Logger.getInstance(AbstractNewProjectStep.class);
|
||||
@@ -63,6 +69,14 @@ public class AbstractNewProjectStep extends DefaultActionGroup implements DumbAw
|
||||
customization.setUpBasicAction(projectSpecificAction, generators);
|
||||
|
||||
addAll(customization.getActions(generators, callback));
|
||||
if (customization.showUserDefinedProjects()) {
|
||||
ArchivedTemplatesFactory factory = new ArchivedTemplatesFactory();
|
||||
ProjectTemplate[] templates = factory.createTemplates(CUSTOM_GROUP, null);
|
||||
for (ProjectTemplate template : templates) {
|
||||
TemplateProjectDirectoryGenerator generator = new TemplateProjectDirectoryGenerator((LocalArchivedTemplate)template);
|
||||
addAll(customization.getActions(generator, callback));
|
||||
}
|
||||
}
|
||||
addAll(customization.getExtraActions(callback));
|
||||
}
|
||||
|
||||
@@ -130,6 +144,10 @@ public class AbstractNewProjectStep extends DefaultActionGroup implements DumbAw
|
||||
|
||||
public void setUpBasicAction(@NotNull ProjectSpecificAction projectSpecificAction, @NotNull DirectoryProjectGenerator[] generators) {
|
||||
}
|
||||
|
||||
public boolean showUserDefinedProjects(){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected static abstract class AbstractCallback implements NullableConsumer<ProjectSettingsStepBase> {
|
||||
@@ -194,16 +212,24 @@ public class AbstractNewProjectStep extends DefaultActionGroup implements DumbAw
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
RecentProjectsManager.getInstance().setLastProjectCreationLocation(location.getParent());
|
||||
final Object finalSettings = settings;
|
||||
return PlatformProjectOpenProcessor.doOpenProject(baseDir, null, false, -1, new ProjectOpenedCallback() {
|
||||
@Override
|
||||
public void projectOpened(Project project, Module module) {
|
||||
if (generator != null) {
|
||||
generator.generateProject(project, baseDir, finalSettings, module);
|
||||
|
||||
ProjectOpenedCallback callback = null;
|
||||
if(generator instanceof TemplateProjectDirectoryGenerator){
|
||||
((TemplateProjectDirectoryGenerator)generator).generateProject(baseDir.getName(), locationString);
|
||||
} else {
|
||||
final Object finalSettings = settings;
|
||||
callback = new ProjectOpenedCallback() {
|
||||
@Override
|
||||
public void projectOpened(Project project, Module module) {
|
||||
if (generator != null) {
|
||||
generator.generateProject(project, baseDir, finalSettings, module);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
};
|
||||
}
|
||||
return PlatformProjectOpenProcessor.doOpenProject(baseDir, null, false, -1, callback, false);
|
||||
}
|
||||
|
||||
}
|
||||
+10
-3
@@ -30,6 +30,7 @@ import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.wm.impl.welcomeScreen.AbstractActionWithPanel;
|
||||
import com.intellij.platform.DirectoryProjectGenerator;
|
||||
import com.intellij.platform.WebProjectGenerator;
|
||||
import com.intellij.platform.templates.TemplateProjectDirectoryGenerator;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.ui.JBColor;
|
||||
import com.intellij.ui.components.JBScrollPane;
|
||||
@@ -219,10 +220,16 @@ public class ProjectSettingsStepBase extends AbstractActionWithPanel implements
|
||||
}
|
||||
|
||||
protected JPanel createAndFillContentPanel() {
|
||||
if (!(myProjectGenerator instanceof WebProjectTemplate)) return createContentPanelWithAdvancedSettingsPanel();
|
||||
|
||||
WebProjectSettingsStepWrapper settingsStep = new WebProjectSettingsStepWrapper();
|
||||
((WebProjectTemplate)myProjectGenerator).getPeer().buildUI(settingsStep);
|
||||
if (myProjectGenerator instanceof WebProjectTemplate) {
|
||||
((WebProjectTemplate)myProjectGenerator).getPeer().buildUI(settingsStep);
|
||||
}
|
||||
else if (myProjectGenerator instanceof TemplateProjectDirectoryGenerator) {
|
||||
((TemplateProjectDirectoryGenerator)myProjectGenerator).buildUI(settingsStep);
|
||||
}
|
||||
else {
|
||||
return createContentPanelWithAdvancedSettingsPanel();
|
||||
}
|
||||
|
||||
//back compatibility: some plugins can implement only GeneratorPeer#getComponent() method
|
||||
if (settingsStep.isEmpty()) return createContentPanelWithAdvancedSettingsPanel();
|
||||
|
||||
+3
-1
@@ -46,6 +46,8 @@ import java.util.zip.ZipInputStream;
|
||||
@Tag("template")
|
||||
public abstract class ArchivedProjectTemplate implements ProjectTemplate {
|
||||
public static final String INPUT_FIELD = "input-field";
|
||||
public static final String TEMPLATE = "template";
|
||||
public static final String INPUT_DEFAULT = "default";
|
||||
|
||||
protected final String myDisplayName;
|
||||
@Nullable private final String myCategory;
|
||||
@@ -132,7 +134,7 @@ public abstract class ArchivedProjectTemplate implements ProjectTemplate {
|
||||
@Override
|
||||
public WizardInputField fun(Element element) {
|
||||
ProjectTemplateParameterFactory factory = WizardInputField.getFactoryById(element.getText());
|
||||
return factory == null ? null : factory.createField(element.getAttributeValue(RemoteTemplatesFactory.INPUT_DEFAULT));
|
||||
return factory == null ? null : factory.createField(element.getAttributeValue(INPUT_DEFAULT));
|
||||
}
|
||||
});
|
||||
}
|
||||
+12
-71
@@ -20,15 +20,10 @@ import com.intellij.ide.fileTemplates.impl.UrlUtil;
|
||||
import com.intellij.ide.util.projectWizard.WizardContext;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.ClearableLazyValue;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.platform.ProjectTemplate;
|
||||
import com.intellij.platform.ProjectTemplatesFactory;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import gnu.trove.THashMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -38,8 +33,6 @@ import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
@@ -50,55 +43,6 @@ public class ArchivedTemplatesFactory extends ProjectTemplatesFactory {
|
||||
|
||||
static final String ZIP = ".zip";
|
||||
|
||||
private final ClearableLazyValue<MultiMap<String, Pair<URL, ClassLoader>>> myGroups = new ClearableLazyValue<MultiMap<String, Pair<URL, ClassLoader>>>() {
|
||||
@NotNull
|
||||
@Override
|
||||
protected MultiMap<String, Pair<URL, ClassLoader>> compute() {
|
||||
MultiMap<String, Pair<URL, ClassLoader>> map = MultiMap.createSmart();
|
||||
Map<URL, ClassLoader> urls = new THashMap<URL, ClassLoader>();
|
||||
//for (IdeaPluginDescriptor plugin : plugins) {
|
||||
// if (!plugin.isEnabled()) continue;
|
||||
// try {
|
||||
// ClassLoader loader = plugin.getPluginClassLoader();
|
||||
// Enumeration<URL> resources = loader.getResources("resources/projectTemplates");
|
||||
// ArrayList<URL> list = Collections.list(resources);
|
||||
// for (URL url : list) {
|
||||
// urls.put(url, loader);
|
||||
// }
|
||||
// }
|
||||
// catch (IOException e) {
|
||||
// LOG.error(e);
|
||||
// }
|
||||
//}
|
||||
|
||||
URL configURL = getCustomTemplatesURL();
|
||||
urls.put(configURL, ClassLoader.getSystemClassLoader());
|
||||
|
||||
for (Map.Entry<URL, ClassLoader> url : urls.entrySet()) {
|
||||
try {
|
||||
List<String> children = UrlUtil.getChildrenRelativePaths(url.getKey());
|
||||
if (configURL == url.getKey() && !children.isEmpty()) {
|
||||
map.putValue(CUSTOM_GROUP, Pair.create(url.getKey(), url.getValue()));
|
||||
continue;
|
||||
}
|
||||
|
||||
for (String child : children) {
|
||||
int index = child.indexOf('/');
|
||||
if (index != -1) {
|
||||
child = child.substring(0, index);
|
||||
}
|
||||
String name = child.replace('_', ' ');
|
||||
map.putValue(name, Pair.create(new URL(url.getKey().toExternalForm() + "/" + child), url.getValue()));
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private static URL getCustomTemplatesURL() {
|
||||
try {
|
||||
@@ -121,34 +65,31 @@ public class ArchivedTemplatesFactory extends ProjectTemplatesFactory {
|
||||
@NotNull
|
||||
@Override
|
||||
public String[] getGroups() {
|
||||
myGroups.drop();
|
||||
Set<String> groups = myGroups.getValue().keySet();
|
||||
return ArrayUtil.toStringArray(groups);
|
||||
return new String[]{CUSTOM_GROUP};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ProjectTemplate[] createTemplates(@Nullable String group, WizardContext context) {
|
||||
// myGroups contains only not-null keys
|
||||
if (group == null) {
|
||||
if (!CUSTOM_GROUP.equals(group)) {
|
||||
return ProjectTemplate.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
List<ProjectTemplate> templates = null;
|
||||
for (Pair<URL, ClassLoader> url : myGroups.getValue().get(group)) {
|
||||
try {
|
||||
for (String child : UrlUtil.getChildrenRelativePaths(url.first)) {
|
||||
if (child.endsWith(ZIP)) {
|
||||
if (templates == null) {
|
||||
templates = new SmartList<ProjectTemplate>();
|
||||
}
|
||||
templates.add(new LocalArchivedTemplate(new URL(url.first.toExternalForm() + '/' + child), url.second));
|
||||
URL url = getCustomTemplatesURL();
|
||||
try {
|
||||
for (String child : UrlUtil.getChildrenRelativePaths(url)) {
|
||||
if (child.endsWith(ZIP)) {
|
||||
if (templates == null) {
|
||||
templates = new SmartList<ProjectTemplate>();
|
||||
}
|
||||
templates.add(new LocalArchivedTemplate(new URL(url.toExternalForm() + '/' + child), ClassLoader.getSystemClassLoader()));
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
return ContainerUtil.isEmpty(templates) ? ProjectTemplate.EMPTY_ARRAY : templates.toArray(new ProjectTemplate[templates.size()]);
|
||||
}
|
||||
+7
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 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.
|
||||
@@ -16,9 +16,9 @@
|
||||
package com.intellij.platform.templates;
|
||||
|
||||
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.fileTemplates.FileTemplateUtil;
|
||||
import com.intellij.ide.impl.ProjectUtil;
|
||||
import com.intellij.ide.util.projectWizard.ProjectTemplateFileProcessor;
|
||||
import com.intellij.ide.util.projectWizard.ProjectTemplateParameterFactory;
|
||||
@@ -160,7 +160,8 @@ public class SaveProjectAsTemplateAction extends AnAction {
|
||||
!fileName.equals(PROJECT_TEMPLATE_XML) &&
|
||||
!fileName.equals("misc.xml") &&
|
||||
!fileName.equals("modules.xml") &&
|
||||
!fileName.equals("workspace.xml")) {
|
||||
!fileName.equals("workspace.xml") &&
|
||||
!fileName.endsWith(".iml")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -241,7 +242,7 @@ public class SaveProjectAsTemplateAction extends AnAction {
|
||||
String text = VfsUtilCore.loadText(virtualFile);
|
||||
final FileTemplate template = FileTemplateManager.getInstance(project).getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
|
||||
final String templateText = template.getText();
|
||||
final Pattern pattern = FileHeaderChecker.getTemplatePattern(template, project, new TIntObjectHashMap<String>());
|
||||
final Pattern pattern = FileTemplateUtil.getTemplatePattern(template, project, new TIntObjectHashMap<String>());
|
||||
String result = convertTemplates(text, pattern, templateText);
|
||||
result = ProjectTemplateFileProcessor.encodeFile(result, virtualFile, project);
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
||||
@@ -282,11 +283,11 @@ public class SaveProjectAsTemplateAction extends AnAction {
|
||||
}
|
||||
|
||||
private static String getInputFieldsText(Map<String, String> parameters) {
|
||||
Element element = new Element(RemoteTemplatesFactory.TEMPLATE);
|
||||
Element element = new Element(ArchivedProjectTemplate.TEMPLATE);
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
||||
Element field = new Element(ArchivedProjectTemplate.INPUT_FIELD);
|
||||
field.setText(entry.getValue());
|
||||
field.setAttribute(RemoteTemplatesFactory.INPUT_DEFAULT, entry.getKey());
|
||||
field.setAttribute(ArchivedProjectTemplate.INPUT_DEFAULT, entry.getKey());
|
||||
element.addContent(field);
|
||||
}
|
||||
return JDOMUtil.writeElement(element);
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.facet.ui.ValidationResult;
|
||||
import com.intellij.ide.util.projectWizard.ModuleBuilder;
|
||||
import com.intellij.ide.util.projectWizard.SettingsStep;
|
||||
import com.intellij.ide.util.projectWizard.WizardInputField;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.platform.DirectoryProjectGenerator;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class TemplateProjectDirectoryGenerator implements DirectoryProjectGenerator<Object> {
|
||||
private final LocalArchivedTemplate myTemplate;
|
||||
private final ModuleBuilder myModuleBuilder;
|
||||
|
||||
public TemplateProjectDirectoryGenerator(@NotNull LocalArchivedTemplate template) {
|
||||
myTemplate = template;
|
||||
myModuleBuilder = myTemplate.createModuleBuilder();
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return myTemplate.getName();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Icon getLogo() {
|
||||
return myTemplate.getIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateProject(@NotNull Project newProject,
|
||||
@NotNull VirtualFile baseDir,
|
||||
@Nullable Object settings,
|
||||
@NotNull Module module) {
|
||||
throw new IllegalStateException("Usn't supposed to be invoked, use generateProject(String, String) instead.");
|
||||
}
|
||||
|
||||
public void generateProject(String name, String path){
|
||||
try {
|
||||
myModuleBuilder.createProject(name, path);
|
||||
}
|
||||
finally {
|
||||
myModuleBuilder.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ValidationResult validate(@NotNull String baseDirPath) {
|
||||
String message = "Invalid settings";
|
||||
for (WizardInputField field : myTemplate.getInputFields()) {
|
||||
try {
|
||||
if (field.validate()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (ConfigurationException e) {
|
||||
message = e.getMessage();
|
||||
}
|
||||
return new ValidationResult(message);
|
||||
}
|
||||
|
||||
return ValidationResult.OK;
|
||||
}
|
||||
|
||||
public void buildUI(@NotNull SettingsStep settingsStep){
|
||||
for (WizardInputField field : myTemplate.getInputFields()) {
|
||||
field.addToSettings(settingsStep);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user