From 8e799bf3d4236c384be898057a695632c3a1bf65 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 30 Mar 2013 17:33:29 +0100 Subject: [PATCH] Allow running actions as part of file templates. --- .../Project/Src/Gui/Dialogs/NewFileDialog.cs | 15 ++-- .../Internal/Templates/File/FileTemplate.cs | 72 +++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs b/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs index f201e5abd4..aebb1ec5c5 100644 --- a/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs +++ b/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs @@ -159,9 +159,7 @@ namespace ICSharpCode.SharpDevelop.Gui Category cat = GetCategory(StringParser.Parse(titem.Template.Category), StringParser.Parse(titem.Template.Subcategory)); cat.Templates.Add(titem); - if (cat.Selected == false && template.WizardPath == null) { - cat.Selected = true; - } + cat.Selected = true; if (!cat.HasSelectedTemplate && titem.Template.FileDescriptionTemplates.Count == 1) { if (((FileDescriptionTemplate)titem.Template.FileDescriptionTemplates[0]).Name.StartsWith("Empty")) { titem.Selected = true; @@ -473,16 +471,24 @@ namespace ICSharpCode.SharpDevelop.Gui StringParserPropertyContainer.FileCreation["StandardNamespace"] = CustomToolsService.GetDefaultNamespace(project, fileName); } } + + FileTemplateOptions options = new FileTemplateOptions(); + options.ClassName = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false); + options.FileName = FileName.Create(fileName); + options.IsUntitled = allowUntitledFiles; + options.Namespace = StringParserPropertyContainer.FileCreation["StandardNamespace"]; + StringParserPropertyContainer.FileCreation["FullName"] = fileName; StringParserPropertyContainer.FileCreation["FileName"] = Path.GetFileName(fileName); StringParserPropertyContainer.FileCreation["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(fileName); StringParserPropertyContainer.FileCreation["Extension"] = Path.GetExtension(fileName); StringParserPropertyContainer.FileCreation["Path"] = Path.GetDirectoryName(fileName); - StringParserPropertyContainer.FileCreation["ClassName"] = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false); + StringParserPropertyContainer.FileCreation["ClassName"] = options.ClassName; // when adding a file to a project (but not when creating a standalone file while a project is open): if (ProjectService.CurrentProject != null && !this.allowUntitledFiles) { + options.Project = ProjectService.CurrentProject; // add required assembly references to the project bool changes = false; foreach (ReferenceProjectItem reference in item.Template.RequiredAssemblyReferences) { @@ -525,6 +531,7 @@ namespace ICSharpCode.SharpDevelop.Gui foreach (KeyValuePair entry in createdFiles) { FileService.FireFileCreated(entry.Key, false); } + item.Template.RunActions(options); } } diff --git a/src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs b/src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs index 838497486f..f5faecb38c 100644 --- a/src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs +++ b/src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs @@ -95,6 +95,37 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates } } + public class FileTemplateOptions + { + /// + /// Gets/Sets whether the file being created will be untitled. + /// + public bool IsUntitled { get; set; } + + /// + /// The parent project to which this file is added. + /// Can be null when creating a file outside of a project. + /// + public IProject Project { get; set; } + + /// + /// The name of the file + /// + public FileName FileName { get; set; } + + /// + /// The default namespace to use for the newly created file. + /// + public string Namespace { get; set; } + + /// + /// The class name (generated from the file name). + /// + public string ClassName { get; set; } + + //IDictionary properties; + } + /// /// This class defines and holds the new file templates. /// @@ -120,6 +151,7 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates List requiredAssemblyReferences = new List(); XmlElement fileoptions = null; + Action actions; int IComparable.CompareTo(object other) { @@ -165,6 +197,7 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates return description; } } + [Obsolete] public string WizardPath { get { return wizardpath; @@ -275,6 +308,14 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates } } + if (doc.DocumentElement["Actions"] != null) { + foreach (XmlElement el in doc.DocumentElement["Actions"]) { + Action action = ReadAction(el); + if (action != null) + actions += action; + } + } + fileoptions = doc.DocumentElement["AdditionalOptions"]; doc.DocumentElement.SetAttribute("fileName", filename); // used for template loading warnings @@ -289,6 +330,37 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates } } + static Action ReadAction(XmlElement el) + { + switch (el.Name) { + case "RunCommand": + if (el.HasAttribute("path")) { + try { + ICommand command = (ICommand)AddInTree.BuildItem(el.GetAttribute("path"), null); + return fileCreateInformation => { + command.Owner = fileCreateInformation; + command.Run(); + }; + } catch (TreePathNotFoundException ex) { + MessageService.ShowWarning(ex.Message + " - in " + el.OwnerDocument.DocumentElement.GetAttribute("fileName")); + return null; + } + } else { + ProjectTemplate.WarnAttributeMissing(el, "path"); + return null; + } + default: + ProjectTemplate.WarnObsoleteNode(el, "Unknown action element is ignored"); + return null; + } + } + + public void RunActions(FileTemplateOptions options) + { + if (actions != null) + actions(options); + } + public static void UpdateTemplates() { string dataTemplateDir = Path.Combine(PropertyService.DataDirectory, "templates", "file");