PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NHibernate.Tool.hbm2net/src/NHibernate.Tool.hbm2net/CustomHost.cs

https://bitbucket.org/dabide/nhcontrib
C# | 203 lines | 173 code | 23 blank | 7 comment | 9 complexity | c8578399e9350b722e7919f3230cecf5 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TextTemplating;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.CodeDom.Compiler;
  9. using System.Collections;
  10. using System.Xml;
  11. using log4net;
  12. namespace NHibernate.Tool.hbm2net.T4
  13. {
  14. ///<author>
  15. /// Felice Pollano (felice@felicepollano.com)
  16. ///</author>
  17. public class CustomHost : ITextTemplatingEngineHost
  18. {
  19. static Dictionary<string, System.Type> directiveProcessors = new Dictionary<string, System.Type>();
  20. public bool HasError { get; internal set; }
  21. public IList<CompilerError> Errors { get; internal set; }
  22. public ILog Logger { get; set; }
  23. public CustomHost()
  24. {
  25. Errors = new List<CompilerError>();
  26. }
  27. static CustomHost()
  28. {
  29. InitializeProcessors();
  30. }
  31. private static void InitializeProcessors()
  32. {
  33. foreach (System.Type t in Assembly.GetExecutingAssembly().GetTypes())
  34. {
  35. if (typeof(DirectiveProcessor).IsAssignableFrom(t))
  36. {
  37. directiveProcessors[t.Name] = t;
  38. }
  39. }
  40. }
  41. #region ITextTemplatingEngineHost Members
  42. public object GetHostOption(string optionName)
  43. {
  44. switch (optionName)
  45. {
  46. case "CacheAssemblies":
  47. return true;
  48. }
  49. return null;
  50. }
  51. public bool LoadIncludeText(string requestFileName, out string content, out string location)
  52. {
  53. try
  54. {
  55. content = GetTemplateCode(requestFileName);
  56. location = requestFileName;
  57. return true;
  58. }
  59. catch
  60. {
  61. content = location = null;
  62. return false;
  63. }
  64. }
  65. public void LogErrors(CompilerErrorCollection errors)
  66. {
  67. if (errors.HasErrors)
  68. {
  69. HasError = true;
  70. foreach (CompilerError ce in errors)
  71. {
  72. Errors.Add(ce);
  73. if (ce.IsWarning)
  74. Logger.Warn(ce.ErrorText);
  75. else
  76. Logger.Error(ce.ErrorText);
  77. }
  78. }
  79. }
  80. public AppDomain ProvideTemplatingAppDomain(string content)
  81. {
  82. HasError = false;
  83. // this can cause memory leaks. But since hbm2net is generally used
  84. // by a one-stot application, this should be acceptable.
  85. // creating a new appdomain would be the desired solution, but at present
  86. // ClassMapping is not serializable, and I dont want to change the hbm2net internals...
  87. return AppDomain.CurrentDomain;
  88. }
  89. public string ResolveAssemblyReference(string assemblyReference)
  90. {
  91. if (File.Exists(assemblyReference))
  92. {
  93. return assemblyReference;
  94. }
  95. string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), assemblyReference);
  96. if (File.Exists(candidate))
  97. {
  98. return candidate;
  99. }
  100. return "";
  101. }
  102. public System.Type ResolveDirectiveProcessor(string processorName)
  103. {
  104. if (directiveProcessors.ContainsKey(processorName))
  105. return directiveProcessors[processorName];
  106. throw new Exception(string.Format("Directive {0} not found", processorName));
  107. }
  108. public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public string ResolvePath(string path)
  113. {
  114. return path;
  115. }
  116. public void SetFileExtension(string extension)
  117. {
  118. }
  119. public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
  120. {
  121. }
  122. public IList<string> StandardAssemblyReferences
  123. {
  124. get
  125. {
  126. return new string[]
  127. {
  128. typeof(System.Uri).Assembly.Location
  129. ,GetType().Assembly.Location
  130. ,typeof(ClassMapping).Assembly.Location
  131. ,typeof(XmlElement).Assembly.Location
  132. ,typeof(IQueryable).Assembly.Location
  133. };
  134. }
  135. }
  136. public IList<string> StandardImports
  137. {
  138. get
  139. {
  140. string[] imports = new string[]
  141. {
  142. "System"
  143. ,"System.IO"
  144. ,"System.Collections"
  145. ,"System.Collections.Generic"
  146. ,"System.Linq"
  147. ,"System.Text"
  148. ,"System.Xml"
  149. ,"NHibernate.Tool.hbm2net"
  150. };
  151. return imports;
  152. }
  153. }
  154. public string TemplateFile
  155. {
  156. get;
  157. set;
  158. }
  159. #endregion
  160. public static string GetTemplateCode(string template)
  161. {
  162. Stream templateStream;
  163. if (template.StartsWith("res://"))
  164. {
  165. templateStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(template.Substring(6).Trim());
  166. }
  167. else
  168. {
  169. if (template.StartsWith("~"))
  170. {
  171. template = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), template.Substring(1).Trim('\\'));
  172. }
  173. templateStream = new FileStream(template, FileMode.Open);
  174. }
  175. string code = new StreamReader(templateStream).ReadToEnd();
  176. templateStream.Close();
  177. return code;
  178. }
  179. }
  180. }