BuildManager.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // System.Web.Compilation.BuildManager
  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;
  33. using System.CodeDom.Compiler;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Web;
  39. using System.Web.Configuration;
  40. using System.Web.Hosting;
  41. namespace System.Web.Compilation {
  42. public sealed class BuildManager {
  43. private static List<Assembly> AppCode_Assemblies = new List<Assembly>();
  44. private static List<Assembly> TopLevel_Assemblies = new List<Assembly>();
  45. private static bool haveResources;
  46. internal BuildManager ()
  47. {
  48. }
  49. internal static void ThrowNoProviderException (string extension)
  50. {
  51. string msg = "No registered provider for extension '{0}'.";
  52. throw new HttpException (String.Format (msg, extension));
  53. }
  54. public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
  55. {
  56. // virtualPath + Exists done in GetCompiledType()
  57. if (requiredBaseType == null)
  58. throw new NullReferenceException (); // This is what MS does, but from somewhere else.
  59. // Get the Type.
  60. Type type = GetCompiledType (virtualPath);
  61. if (!requiredBaseType.IsAssignableFrom (type)) {
  62. string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
  63. type.FullName, requiredBaseType.FullName);
  64. throw new HttpException (500, msg);
  65. }
  66. return Activator.CreateInstance (type, null);
  67. }
  68. [MonoTODO ("Not implemented, always returns null")]
  69. public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
  70. {
  71. return null; // null is ok here until we store the dependency set in the Cache.
  72. }
  73. internal static BuildProvider GetBuildProviderForPath (string virtualPath, bool throwOnMissing)
  74. {
  75. string extension = VirtualPathUtility.GetExtension (virtualPath);
  76. CompilationSection c = WebConfigurationManager.GetSection ("system.web/compilation", virtualPath) as CompilationSection;
  77. if (c == null)
  78. ThrowNoProviderException (extension);
  79. BuildProviderCollection coll = c.BuildProviders;
  80. if (coll == null || coll.Count == 0)
  81. ThrowNoProviderException (extension);
  82. BuildProvider provider = coll.GetProviderForExtension (extension);
  83. if (provider == null && throwOnMissing)
  84. ThrowNoProviderException (extension);
  85. return provider;
  86. }
  87. public static Assembly GetCompiledAssembly (string virtualPath)
  88. {
  89. BuildProvider provider;
  90. CompilerParameters parameters;
  91. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  92. CompilerType ctype = provider.CodeCompilerType;
  93. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  94. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  95. return results.CompiledAssembly;
  96. }
  97. static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
  98. {
  99. if (virtualPath == null || virtualPath == "")
  100. throw new ArgumentNullException ("virtualPath");
  101. if (virtualPath [0] != '/')
  102. throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
  103. if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
  104. throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
  105. provider = GetBuildProviderForPath (virtualPath, true);
  106. CompilerType compiler_type = provider.CodeCompilerType;
  107. Type ctype = compiler_type.CodeDomProviderType;
  108. CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
  109. AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
  110. provider.SetVirtualPath (virtualPath);
  111. provider.GenerateCode (abuilder);
  112. return abuilder;
  113. }
  114. public static string GetCompiledCustomString (string virtualPath)
  115. {
  116. BuildProvider provider = GetBuildProviderForPath (virtualPath, false);
  117. if (provider == null)
  118. return null;
  119. // FIXME: where to get the CompilerResults from?
  120. return provider.GetCustomString (null);
  121. }
  122. static CompilerParameters PrepareParameters (TempFileCollection temp_files,
  123. string virtualPath,
  124. CompilerParameters base_params)
  125. {
  126. CompilerParameters res = new CompilerParameters ();
  127. res.TempFiles = temp_files;
  128. res.CompilerOptions = base_params.CompilerOptions;
  129. res.IncludeDebugInformation = base_params.IncludeDebugInformation;
  130. res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
  131. res.WarningLevel = base_params.WarningLevel;
  132. string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
  133. res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
  134. return res;
  135. }
  136. public static Type GetCompiledType (string virtualPath)
  137. {
  138. BuildProvider provider;
  139. CompilerParameters parameters;
  140. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  141. CompilerType ctype = provider.CodeCompilerType;
  142. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  143. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  144. return provider.GetGeneratedType (results);
  145. }
  146. // The 2 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
  147. public static Type GetType (string typeName, bool throwOnError)
  148. {
  149. return GetType (typeName, throwOnError, false);
  150. }
  151. public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
  152. {
  153. Type ret = null;
  154. try {
  155. foreach (Assembly asm in TopLevel_Assemblies) {
  156. ret = asm.GetType (typeName, throwOnError, ignoreCase);
  157. if (ret != null)
  158. break;
  159. }
  160. } catch (Exception ex) {
  161. throw new HttpException ("Failed to find the specified type.", ex);
  162. }
  163. return ret;
  164. }
  165. internal static ICollection GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
  166. {
  167. BuildProvider provider = bprovider;
  168. if (provider == null)
  169. provider = GetBuildProviderForPath (virtualPath, false);
  170. if (provider == null)
  171. return null;
  172. return provider.VirtualPathDependencies;
  173. }
  174. public static ICollection GetVirtualPathDependencies (string virtualPath)
  175. {
  176. return GetVirtualPathDependencies (virtualPath, null);
  177. }
  178. // Assemblies built from the App_Code directory
  179. public static IList CodeAssemblies {
  180. get { return AppCode_Assemblies; }
  181. }
  182. internal static IList TopLevelAssemblies {
  183. get { return TopLevel_Assemblies; }
  184. }
  185. internal static bool HaveResources {
  186. get { return haveResources; }
  187. set { haveResources = value; }
  188. }
  189. }
  190. }
  191. #endif