PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PowerTools/Utilities/EfTextTemplateHost.cs

#
C# | 192 lines | 158 code | 31 blank | 3 comment | 11 complexity | a303696157c35dfc4f4842acc695ad29 MD5 | raw file
  1. namespace Microsoft.DbContextPackage.Utilities
  2. {
  3. using System;
  4. using System.CodeDom.Compiler;
  5. using System.Collections.Generic;
  6. using System.Data.Metadata.Edm;
  7. using System.Data.Objects;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using Microsoft.DbContextPackage.Resources;
  13. using Microsoft.VisualStudio.Shell;
  14. using Microsoft.VisualStudio.Shell.Interop;
  15. using Microsoft.VisualStudio.TextTemplating;
  16. public class EfTextTemplateHost : ITextTemplatingEngineHost
  17. {
  18. public EntityType EntityType { get; set; }
  19. public EntityContainer EntityContainer { get; set; }
  20. public string Namespace { get; set; }
  21. public string ModelsNamespace { get; set; }
  22. public string MappingNamespace { get; set; }
  23. public Version EntityFrameworkVersion { get; set; }
  24. public EntitySet TableSet { get; set; }
  25. public Dictionary<EdmProperty, EdmProperty> PropertyToColumnMappings { get; set; }
  26. public Dictionary<AssociationType, Tuple<EntitySet, Dictionary<RelationshipEndMember, Dictionary<EdmMember, string>>>> ManyToManyMappings { get; set; }
  27. #region T4 plumbing
  28. public CompilerErrorCollection Errors { get; set; }
  29. public string FileExtension { get; set; }
  30. public Encoding OutputEncoding { get; set; }
  31. public string TemplateFile { get; set; }
  32. public virtual string ResolveAssemblyReference(string assemblyReference)
  33. {
  34. if (File.Exists(assemblyReference))
  35. {
  36. return assemblyReference;
  37. }
  38. try
  39. {
  40. // TODO: This is failing to resolve partial assembly names (e.g. "System.Xml")
  41. var assembly = Assembly.Load(assemblyReference);
  42. if (assembly != null)
  43. {
  44. return assembly.Location;
  45. }
  46. }
  47. catch (FileNotFoundException)
  48. {
  49. }
  50. catch (FileLoadException)
  51. {
  52. }
  53. catch (BadImageFormatException)
  54. {
  55. }
  56. return string.Empty;
  57. }
  58. IList<string> ITextTemplatingEngineHost.StandardAssemblyReferences
  59. {
  60. get
  61. {
  62. return new[]
  63. {
  64. Assembly.GetExecutingAssembly().Location,
  65. typeof(Uri).Assembly.Location,
  66. typeof(Enumerable).Assembly.Location,
  67. typeof(ObjectContext).Assembly.Location,
  68. // HACK: Because of the issue in ResolveAssemblyReference, these are not being
  69. // loaded but are required by the default templates
  70. typeof(System.Data.AcceptRejectRule).Assembly.Location,
  71. typeof(System.Data.Entity.Design.EdmToObjectNamespaceMap).Assembly.Location,
  72. typeof(System.Xml.ConformanceLevel).Assembly.Location,
  73. typeof(System.Xml.Linq.Extensions).Assembly.Location,
  74. typeof(EnvDTE._BuildEvents).Assembly.Location
  75. };
  76. }
  77. }
  78. IList<string> ITextTemplatingEngineHost.StandardImports
  79. {
  80. get
  81. {
  82. return new[]
  83. {
  84. "System",
  85. "Microsoft.DbContextPackage.Utilities"
  86. };
  87. }
  88. }
  89. object ITextTemplatingEngineHost.GetHostOption(string optionName)
  90. {
  91. if (optionName == "CacheAssemblies")
  92. {
  93. return 1;
  94. }
  95. return null;
  96. }
  97. bool ITextTemplatingEngineHost.LoadIncludeText(string requestFileName, out string content, out string location)
  98. {
  99. location = ((ITextTemplatingEngineHost)this).ResolvePath(requestFileName);
  100. if (File.Exists(location))
  101. {
  102. content = File.ReadAllText(location);
  103. return true;
  104. }
  105. using (var rootKey = VSRegistry.RegistryRoot(__VsLocalRegistryType.RegType_Configuration))
  106. using (var includeFoldersKey = rootKey.OpenSubKey(@"TextTemplating\IncludeFolders\.tt"))
  107. {
  108. foreach (var valueName in includeFoldersKey.GetValueNames())
  109. {
  110. var includeFolder = includeFoldersKey.GetValue(valueName) as string;
  111. if (includeFolder == null)
  112. {
  113. continue;
  114. }
  115. location = Path.Combine(includeFolder, requestFileName);
  116. if (File.Exists(location))
  117. {
  118. content = File.ReadAllText(location);
  119. return true;
  120. }
  121. }
  122. }
  123. location = string.Empty;
  124. content = string.Empty;
  125. return false;
  126. }
  127. void ITextTemplatingEngineHost.LogErrors(CompilerErrorCollection errors)
  128. {
  129. Errors = errors;
  130. }
  131. AppDomain ITextTemplatingEngineHost.ProvideTemplatingAppDomain(string content)
  132. {
  133. return AppDomain.CurrentDomain;
  134. }
  135. Type ITextTemplatingEngineHost.ResolveDirectiveProcessor(string processorName)
  136. {
  137. throw Error.UnknownDirectiveProcessor(processorName);
  138. }
  139. string ITextTemplatingEngineHost.ResolveParameterValue(string directiveId, string processorName, string parameterName)
  140. {
  141. return string.Empty;
  142. }
  143. string ITextTemplatingEngineHost.ResolvePath(string path)
  144. {
  145. if (!Path.IsPathRooted(path) && Path.IsPathRooted(TemplateFile))
  146. {
  147. return Path.Combine(Path.GetDirectoryName(TemplateFile), path);
  148. }
  149. return path;
  150. }
  151. void ITextTemplatingEngineHost.SetFileExtension(string extension)
  152. {
  153. FileExtension = extension;
  154. }
  155. void ITextTemplatingEngineHost.SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
  156. {
  157. OutputEncoding = encoding;
  158. }
  159. #endregion
  160. }
  161. }