| usingSystem; |
| usingSystem.Windows.Forms.Design; |
| usingMicrosoft.Practices.RecipeFramework; |
| usingSystem.Collections.Generic; |
|
| publicclassCustomTemplateAction:Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.T4Action |
| { |
| #regionInputProperties |
| //Placeinputpropertieshere |
| #endregion |
|
| |
|
| ///<summary> |
| ///Generatesatextfileinthespecifiedproject. |
| ///</summary> |
| ///<paramname="fileName">Nameofthefiletocreate.</param> |
| ///<paramname="content">Contentofthetextfiletobecreated.</param> |
| privatevoidGenerateTextFile(stringfileName,stringcontent) |
| { |
| using(Microsoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringActionmyAction=newMicrosoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringAction()) |
| { |
| base.Container.Add(myAction); |
|
| //setparametersforaction |
| myAction.Content=content; |
| myAction.TargetFileName=fileName; |
| myAction.Project=outputProject; |
|
| myAction.Execute(); |
|
| } |
| } |
| ///<summary> |
| ///Rendersatemplatesandsavestoafile. |
| ///</summary> |
| ///<paramname="templateName">Nameoftexttemplatefile.</param> |
| ///<paramname="fileOutput">Nameoftheoutputfiletosavethetemplateto.</param> |
| privatevoidGenerateTemplate(stringtemplateName,stringfileOutput) |
| { |
| |
| //GetpathtoTemplateFile |
| stringnewpath=System.IO.Path.Combine(base.GetTemplateBasePath(),templateName); |
|
| //ReadalltextinTemplateFile |
| StringtemplateContent=System.IO.File.ReadAllText(newpath); |
|
| //Rendertemplate |
| try |
| { |
| stringret=base.Render(templateContent,newpath); |
| GenerateTextFile(fileOutput,ret); |
|
| } |
|
| catch(Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TemplateExceptionte) |
| { |
| //Templateerror |
| } |
| catch(Exceptione) |
| { |
| //Otherexception |
| } |
| } |
|
| publicoverridevoidExecute() |
| { |
|
| //Thisarugmentwillbeavailabletoeverytexttemplate |
| base.additionalArguments.Add("Argument1",SomeArugment); |
|
|
| foreach(StringcinListOfClassesObject) |
| { |
| base.additionalArguments.Add("MyClass",c); |
| |
| GenerateTemplate(TemplateName,c+".txt"); |
| |
| //RemoveMyClasssonextiteartion of the loop gets |
| //anewMyClassparameter |
| base.additionalArguments.Remove("MyClass"); |
| } |
| |
| } |
|
| publicoverridevoidUndo() |
| { |
|
| } |
| } |
|
| } |