RepositoryGenerator.tt 8.6 KB

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