BuildProvider.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // System.Web.Compilation.BuildProvider
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.CodeDom.Compiler;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Web.Configuration;
  38. using System.Web.Hosting;
  39. using System.Web.Util;
  40. namespace System.Web.Compilation {
  41. public abstract class BuildProvider {
  42. static object locker = new object ();
  43. static string private_bin_path;
  44. string virtual_path;
  45. ArrayList ref_assemblies;
  46. ICollection vpath_deps;
  47. protected BuildProvider()
  48. {
  49. ref_assemblies = new ArrayList ();
  50. }
  51. internal void SetVirtualPath (string path)
  52. {
  53. virtual_path = path;
  54. }
  55. public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
  56. {
  57. }
  58. public virtual string GetCustomString (CompilerResults results)
  59. {
  60. return null;
  61. }
  62. protected CompilerType GetDefaultCompilerType ()
  63. {
  64. CompilationSection config;
  65. config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  66. return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
  67. }
  68. void SetCommonParameters (CompilationSection config, CompilerParameters p)
  69. {
  70. p.IncludeDebugInformation = config.Debug;
  71. foreach (AssemblyInfo info in config.Assemblies) {
  72. if (info.Assembly != "*") {
  73. p.ReferencedAssemblies.Add (info.Assembly);
  74. ref_assemblies.Add (info.Assembly);
  75. } else {
  76. AddAssembliesInBin (p.ReferencedAssemblies);
  77. }
  78. }
  79. // explicit, strict?
  80. // embedded? linked?/resources
  81. }
  82. static void InitPath ()
  83. {
  84. lock (locker) {
  85. if (private_bin_path != null)
  86. return;
  87. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  88. private_bin_path = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
  89. }
  90. }
  91. void AddAssembliesInBin (StringCollection coll)
  92. {
  93. InitPath ();
  94. if (!Directory.Exists (private_bin_path))
  95. return;
  96. string [] binDlls = Directory.GetFiles (private_bin_path, "*.dll");
  97. foreach (string s in binDlls) {
  98. coll.Add (s);
  99. ref_assemblies.Add (s);
  100. }
  101. }
  102. internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
  103. {
  104. // MS throws when accesing a Hashtable, we do here.
  105. if (language == null || language == "")
  106. throw new ArgumentNullException ("language");
  107. CompilationSection config;
  108. if (configSection == null)
  109. config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  110. else
  111. config = configSection;
  112. Compiler compiler = config.Compilers.Get (language);
  113. CompilerParameters p;
  114. if (compiler != null) {
  115. Type type = Type.GetType (compiler.Type, true);
  116. p = new CompilerParameters ();
  117. p.CompilerOptions = compiler.CompilerOptions;
  118. p.WarningLevel = compiler.WarningLevel;
  119. SetCommonParameters (config, p);
  120. return new CompilerType (type, p);
  121. }
  122. if (!CodeDomProvider.IsDefinedLanguage (language))
  123. throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
  124. CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
  125. CompilerParameters par = info.CreateDefaultCompilerParameters ();
  126. SetCommonParameters (config, par);
  127. return new CompilerType (info.CodeDomProviderType, par);
  128. }
  129. protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
  130. {
  131. return GetDefaultCompilerTypeForLanguage (language, null);
  132. }
  133. public virtual Type GetGeneratedType (CompilerResults results)
  134. {
  135. return null;
  136. }
  137. public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
  138. {
  139. return BuildProviderResultFlags.Default;
  140. }
  141. protected TextReader OpenReader ()
  142. {
  143. return OpenReader (VirtualPath);
  144. }
  145. protected TextReader OpenReader (string virtualPath)
  146. {
  147. Stream st = OpenStream (virtualPath);
  148. return new StreamReader (st, WebEncoding.FileEncoding);
  149. }
  150. protected Stream OpenStream ()
  151. {
  152. return OpenStream (VirtualPath);
  153. }
  154. protected Stream OpenStream (string virtualPath)
  155. {
  156. // MS also throws a NullReferenceException here when not hosted.
  157. return VirtualPathProvider.OpenFile (virtualPath);
  158. }
  159. public virtual CompilerType CodeCompilerType {
  160. get { return null; } // Documented to return null
  161. }
  162. protected ICollection ReferencedAssemblies {
  163. get { return ref_assemblies; }
  164. }
  165. protected internal string VirtualPath {
  166. get { return virtual_path; }
  167. }
  168. public virtual ICollection VirtualPathDependencies {
  169. get {
  170. if (vpath_deps == null)
  171. vpath_deps = new OneNullCollection ();
  172. return vpath_deps;
  173. }
  174. }
  175. }
  176. class OneNullCollection : ICollection {
  177. public int Count {
  178. get { return 1; }
  179. }
  180. public bool IsSynchronized {
  181. get { return false; }
  182. }
  183. public object SyncRoot {
  184. get { return this; }
  185. }
  186. public void CopyTo (Array array, int index)
  187. {
  188. if (array == null)
  189. throw new ArgumentNullException ();
  190. if (index < 0)
  191. throw new ArgumentOutOfRangeException ();
  192. if (array.Rank > 1)
  193. throw new ArgumentException ();
  194. int length = array.Length;
  195. if (index >= length || index > length - 1)
  196. throw new ArgumentException ();
  197. array.SetValue (null, index);
  198. }
  199. public IEnumerator GetEnumerator ()
  200. {
  201. yield return null;
  202. }
  203. }
  204. }
  205. #endif