BuildProvider.cs 6.1 KB

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