2
0

BuildProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. [MonoTODO]
  59. public virtual string GetCustomString (CompilerResults results)
  60. {
  61. throw new NotImplementedException ();
  62. }
  63. protected CompilerType GetDefaultCompilerType ()
  64. {
  65. CompilationSection config;
  66. config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  67. return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage);
  68. }
  69. void SetCommonParameters (CompilationSection config, CompilerParameters p)
  70. {
  71. p.IncludeDebugInformation = config.Debug;
  72. foreach (AssemblyInfo info in config.Assemblies) {
  73. if (info.Assembly != "*") {
  74. p.ReferencedAssemblies.Add (info.Assembly);
  75. ref_assemblies.Add (info.Assembly);
  76. } else {
  77. AddAssembliesInBin (p.ReferencedAssemblies);
  78. }
  79. }
  80. // explicit, strict?
  81. // embedded? linked?/resources
  82. }
  83. static void InitPath ()
  84. {
  85. lock (locker) {
  86. if (private_bin_path != null)
  87. return;
  88. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  89. private_bin_path = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
  90. }
  91. }
  92. void AddAssembliesInBin (StringCollection coll)
  93. {
  94. InitPath ();
  95. if (!Directory.Exists (private_bin_path))
  96. return;
  97. string [] binDlls = Directory.GetFiles (private_bin_path, "*.dll");
  98. foreach (string s in binDlls) {
  99. coll.Add (s);
  100. ref_assemblies.Add (s);
  101. }
  102. }
  103. protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
  104. {
  105. // MS throws when accesing a Hashtable, we do here.
  106. if (language == null || language == "")
  107. throw new ArgumentNullException ("language");
  108. CompilationSection config;
  109. config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  110. Compiler compiler = config.Compilers.Get (language);
  111. CompilerParameters p;
  112. if (compiler != null) {
  113. Type type = Type.GetType (compiler.Type, true);
  114. p = new CompilerParameters ();
  115. p.CompilerOptions = compiler.CompilerOptions;
  116. p.WarningLevel = compiler.WarningLevel;
  117. SetCommonParameters (config, p);
  118. return new CompilerType (type, p);
  119. }
  120. if (!CodeDomProvider.IsDefinedLanguage (language))
  121. throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
  122. CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
  123. CompilerParameters par = info.CreateDefaultCompilerParameters ();
  124. SetCommonParameters (config, par);
  125. return new CompilerType (info.CodeDomProviderType, par);
  126. }
  127. public virtual Type GetGeneratedType (CompilerResults results)
  128. {
  129. return null;
  130. }
  131. public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
  132. {
  133. return BuildProviderResultFlags.Default;
  134. }
  135. protected TextReader OpenReader ()
  136. {
  137. return OpenReader (VirtualPath);
  138. }
  139. protected TextReader OpenReader (string virtualPath)
  140. {
  141. Stream st = OpenStream (virtualPath);
  142. return new StreamReader (st, WebEncoding.FileEncoding);
  143. }
  144. protected Stream OpenStream ()
  145. {
  146. return OpenStream (VirtualPath);
  147. }
  148. protected Stream OpenStream (string virtualPath)
  149. {
  150. // MS also throws a NullReferenceException here when not hosted.
  151. return VirtualPathProvider.OpenFile (virtualPath);
  152. }
  153. public virtual CompilerType CodeCompilerType {
  154. get { return null; } // Documented to return null
  155. }
  156. protected ICollection ReferencedAssemblies {
  157. get { return ref_assemblies; }
  158. }
  159. protected internal string VirtualPath {
  160. get { return virtual_path; }
  161. }
  162. public virtual ICollection VirtualPathDependencies {
  163. get {
  164. if (vpath_deps == null)
  165. vpath_deps = new OneNullCollection ();
  166. return vpath_deps;
  167. }
  168. }
  169. }
  170. class OneNullCollection : ICollection {
  171. public int Count {
  172. get { return 1; }
  173. }
  174. public bool IsSynchronized {
  175. get { return false; }
  176. }
  177. public object SyncRoot {
  178. get { return this; }
  179. }
  180. public void CopyTo (Array array, int index)
  181. {
  182. if (array == null)
  183. throw new ArgumentNullException ();
  184. if (index < 0)
  185. throw new ArgumentOutOfRangeException ();
  186. if (array.Rank > 1)
  187. throw new ArgumentException ();
  188. int length = array.Length;
  189. if (index >= length || index > length - 1)
  190. throw new ArgumentException ();
  191. array.SetValue (null, index);
  192. }
  193. public IEnumerator GetEnumerator ()
  194. {
  195. yield return null;
  196. }
  197. }
  198. }
  199. #endif