RepositoryGenerator.tt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <#@ template debug="true" hostspecific="true" language="C#" #>
  2. <#@ assembly name="System.Core"#>
  3. <#@ assembly name="System.Data.Linq"#>
  4. <#@ assembly name="EnvDTE"#>
  5. <#@ assembly name="System.Xml"#>
  6. <#@ assembly name="System.Xml.Linq"#>
  7. <#@ import namespace="System.Diagnostics" #>
  8. <#@ import namespace="System"#>
  9. <#@ import namespace="System.CodeDom"#>
  10. <#@ import namespace="System.CodeDom.Compiler"#>
  11. <#@ import namespace="System.Collections.Generic"#>
  12. <#@ import namespace="System.Data.Linq"#>
  13. <#@ import namespace="System.Data.Linq.Mapping"#>
  14. <#@ import namespace="System.IO"#>
  15. <#@ import namespace="System.Linq"#>
  16. <#@ import namespace="System.Reflection"#>
  17. <#@ import namespace="System.Text"#>
  18. <#@ import namespace="System.Xml.Linq"#>
  19. <#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
  20. <#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="$(SolutionDir)\packages\EntityFramework.6.1.3\lib\net40\EntityFramework.dll" #>
  21. <#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="$(SolutionDir)\EMIS.Entities\bin\Debug\EMIS.Entities.dll" #>
  22. <#
  23. var manager = Manager.Create(Host, GenerationEnvironment);
  24. var blAssembly = Assembly.GetAssembly(typeof(EMIS.Entities.EMISNewContext));
  25. var types = blAssembly.GetTypes().Where(x =>
  26. x.FullName.StartsWith("EMIS.Entities") &&
  27. !x.Name.EndsWith("Mapping") && !x.Name.EndsWith("Context"));
  28. foreach(var type in types)
  29. {
  30. int startIndex = type.Name.IndexOf("_") + 1;
  31. int length = type.Name.Length - startIndex;
  32. string classShortName = type.Name.Substring(startIndex, length);
  33. string className = classShortName + "Repository";
  34. if (!classShortName.StartsWith("SS_"))
  35. {
  36. continue;
  37. }
  38. manager.StartNewFile(className + ".cs");
  39. #>
  40. using System;
  41. using System.Collections.Generic;
  42. using System.Linq;
  43. using System.Text;
  44. using EMIS.Entities;
  45. namespace EMIS.SSDataLogic.Repositories
  46. {
  47. public class <#=className#> : Repository<<#=type.Name#>>
  48. {
  49. public <#=className#>(UnitOfWork unitOfWork)
  50. : base(unitOfWork)
  51. { }
  52. }
  53. }
  54. <#
  55. manager.EndBlock();
  56. }
  57. manager.Process(true);
  58. #>
  59. <#+
  60. // Manager class records the various blocks so it can split them up
  61. class Manager {
  62. private class Block {
  63. public String Name;
  64. public int Start, Length;
  65. }
  66. private Block currentBlock;
  67. private List<Block> files = new List<Block>();
  68. private Block footer = new Block();
  69. private Block header = new Block();
  70. private ITextTemplatingEngineHost host;
  71. private StringBuilder template;
  72. protected List<String> generatedFileNames = new List<String>();
  73. public static Manager Create(ITextTemplatingEngineHost host, StringBuilder template) {
  74. return (host is IServiceProvider) ? new VSManager(host, template) : new Manager(host, template);
  75. }
  76. public void StartNewFile(String name) {
  77. if (name == null)
  78. throw new ArgumentNullException("name");
  79. CurrentBlock = new Block { Name = name };
  80. }
  81. public void StartFooter() {
  82. CurrentBlock = footer;
  83. }
  84. public void StartHeader() {
  85. CurrentBlock = header;
  86. }
  87. public void EndBlock() {
  88. if (CurrentBlock == null)
  89. return;
  90. CurrentBlock.Length = template.Length - CurrentBlock.Start;
  91. if (CurrentBlock != header && CurrentBlock != footer)
  92. files.Add(CurrentBlock);
  93. currentBlock = null;
  94. }
  95. public virtual void Process(bool split) {
  96. if (split) {
  97. EndBlock();
  98. String headerText = template.ToString(header.Start, header.Length);
  99. String footerText = template.ToString(footer.Start, footer.Length);
  100. String outputPath = Path.GetDirectoryName(host.TemplateFile);
  101. files.Reverse();
  102. foreach(Block block in files) {
  103. String fileName = Path.Combine(outputPath, block.Name);
  104. String content = headerText + template.ToString(block.Start, block.Length) + footerText;
  105. generatedFileNames.Add(fileName);
  106. CreateFile(fileName, content);
  107. template.Remove(block.Start, block.Length);
  108. }
  109. }
  110. }
  111. protected virtual void CreateFile(String fileName, String content) {
  112. if (IsFileContentDifferent(fileName, content))
  113. File.WriteAllText(fileName, content);
  114. }
  115. public virtual String GetCustomToolNamespace(String fileName) {
  116. return null;
  117. }
  118. public virtual String DefaultProjectNamespace {
  119. get { return null; }
  120. }
  121. protected bool IsFileContentDifferent(String fileName, String newContent) {
  122. return !(File.Exists(fileName) && File.ReadAllText(fileName) == newContent);
  123. }
  124. private Manager(ITextTemplatingEngineHost host, StringBuilder template) {
  125. this.host = host;
  126. this.template = template;
  127. }
  128. private Block CurrentBlock {
  129. get { return currentBlock; }
  130. set {
  131. if (CurrentBlock != null)
  132. EndBlock();
  133. if (value != null)
  134. value.Start = template.Length;
  135. currentBlock = value;
  136. }
  137. }
  138. private class VSManager: Manager {
  139. private EnvDTE.ProjectItem templateProjectItem;
  140. private EnvDTE.DTE dte;
  141. private Action<String> checkOutAction;
  142. private Action<IEnumerable<String>> projectSyncAction;
  143. public override String DefaultProjectNamespace {
  144. get {
  145. return templateProjectItem.ContainingProject.Properties.Item("DefaultNamespace").Value.ToString();
  146. }
  147. }
  148. public override String GetCustomToolNamespace(string fileName) {
  149. return dte.Solution.FindProjectItem(fileName).Properties.Item("CustomToolNamespace").Value.ToString();
  150. }
  151. public override void Process(bool split) {
  152. if (templateProjectItem.ProjectItems == null)
  153. return;
  154. base.Process(split);
  155. projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(generatedFileNames, null, null));
  156. }
  157. protected override void CreateFile(String fileName, String content) {
  158. if (IsFileContentDifferent(fileName, content)) {
  159. CheckoutFileIfRequired(fileName);
  160. File.WriteAllText(fileName, content);
  161. }
  162. }
  163. internal VSManager(ITextTemplatingEngineHost host, StringBuilder template)
  164. : base(host, template) {
  165. var hostServiceProvider = (IServiceProvider) host;
  166. if (hostServiceProvider == null)
  167. throw new ArgumentNullException("Could not obtain IServiceProvider");
  168. dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));
  169. if (dte == null)
  170. throw new ArgumentNullException("Could not obtain DTE from host");
  171. templateProjectItem = dte.Solution.FindProjectItem(host.TemplateFile);
  172. checkOutAction = (String fileName) => dte.SourceControl.CheckOutItem(fileName);
  173. projectSyncAction = (IEnumerable<String> keepFileNames) => ProjectSync(templateProjectItem, keepFileNames);
  174. }
  175. private static void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable<String> keepFileNames) {
  176. var keepFileNameSet = new HashSet<String>(keepFileNames);
  177. var projectFiles = new Dictionary<String, EnvDTE.ProjectItem>();
  178. var originalFilePrefix = Path.GetFileNameWithoutExtension(templateProjectItem.get_FileNames(0)) + ".";
  179. foreach(EnvDTE.ProjectItem projectItem in templateProjectItem.ProjectItems)
  180. projectFiles.Add(projectItem.get_FileNames(0), projectItem);
  181. // Remove unused items from the project
  182. foreach(var pair in projectFiles)
  183. if (!keepFileNames.Contains(pair.Key) && !(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalFilePrefix))
  184. pair.Value.Delete();
  185. // Add missing files to the project
  186. foreach(String fileName in keepFileNameSet)
  187. if (!projectFiles.ContainsKey(fileName))
  188. templateProjectItem.ProjectItems.AddFromFile(fileName);
  189. }
  190. private void CheckoutFileIfRequired(String fileName) {
  191. var sc = dte.SourceControl;
  192. if (sc != null && sc.IsItemUnderSCC(fileName) && !sc.IsItemCheckedOut(fileName))
  193. checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null));
  194. }
  195. }
  196. } #>