Browse Source

Allow running actions as part of file templates.

pull/39/merge
Daniel Grunwald 13 years ago
parent
commit
8e799bf3d4
  1. 15
      src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs
  2. 72
      src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs

15
src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs

@ -159,9 +159,7 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -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 @@ -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 @@ -525,6 +531,7 @@ namespace ICSharpCode.SharpDevelop.Gui
foreach (KeyValuePair<string, FileDescriptionTemplate> entry in createdFiles) {
FileService.FireFileCreated(entry.Key, false);
}
item.Template.RunActions(options);
}
}

72
src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs

@ -95,6 +95,37 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates @@ -95,6 +95,37 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates
}
}
public class FileTemplateOptions
{
/// <summary>
/// Gets/Sets whether the file being created will be untitled.
/// </summary>
public bool IsUntitled { get; set; }
/// <summary>
/// The parent project to which this file is added.
/// Can be null when creating a file outside of a project.
/// </summary>
public IProject Project { get; set; }
/// <summary>
/// The name of the file
/// </summary>
public FileName FileName { get; set; }
/// <summary>
/// The default namespace to use for the newly created file.
/// </summary>
public string Namespace { get; set; }
/// <summary>
/// The class name (generated from the file name).
/// </summary>
public string ClassName { get; set; }
//IDictionary<string, string> properties;
}
/// <summary>
/// This class defines and holds the new file templates.
/// </summary>
@ -120,6 +151,7 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates @@ -120,6 +151,7 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates
List<ReferenceProjectItem> requiredAssemblyReferences = new List<ReferenceProjectItem>();
XmlElement fileoptions = null;
Action<FileTemplateOptions> actions;
int IComparable.CompareTo(object other)
{
@ -165,6 +197,7 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates @@ -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 @@ -275,6 +308,14 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates
}
}
if (doc.DocumentElement["Actions"] != null) {
foreach (XmlElement el in doc.DocumentElement["Actions"]) {
Action<FileTemplateOptions> 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 @@ -289,6 +330,37 @@ namespace ICSharpCode.SharpDevelop.Internal.Templates
}
}
static Action<FileTemplateOptions> 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");

Loading…
Cancel
Save