RepositoryGenerator.tt 8.7 KB

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