BuildManager.cs 7.4 KB

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