BuildManager.cs 7.6 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. 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. object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
  77. BuildProviderCollection coll = o as BuildProviderCollection;
  78. if (coll == null || coll.Count == 0)
  79. ThrowNoProviderException (extension);
  80. BuildProvider provider = coll.GetProviderForExtension (extension);
  81. if (provider == null && throwOnMissing)
  82. ThrowNoProviderException (extension);
  83. return provider;
  84. }
  85. public static Assembly GetCompiledAssembly (string virtualPath)
  86. {
  87. BuildProvider provider;
  88. CompilerParameters parameters;
  89. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  90. CompilerType ctype = provider.CodeCompilerType;
  91. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  92. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  93. return results.CompiledAssembly;
  94. }
  95. static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
  96. {
  97. if (virtualPath == null || virtualPath == "")
  98. throw new ArgumentNullException ("virtualPath");
  99. if (virtualPath [0] != '/')
  100. throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
  101. if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
  102. throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
  103. provider = GetBuildProviderForPath (virtualPath, true);
  104. CompilerType compiler_type = provider.CodeCompilerType;
  105. Type ctype = compiler_type.CodeDomProviderType;
  106. CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
  107. AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
  108. provider.SetVirtualPath (virtualPath);
  109. provider.GenerateCode (abuilder);
  110. return abuilder;
  111. }
  112. public static string GetCompiledCustomString (string virtualPath)
  113. {
  114. BuildProvider provider = GetBuildProviderForPath (virtualPath, false);
  115. if (provider == null)
  116. return null;
  117. // FIXME: where to get the CompilerResults from?
  118. return provider.GetCustomString (null);
  119. }
  120. static CompilerParameters PrepareParameters (TempFileCollection temp_files,
  121. string virtualPath,
  122. CompilerParameters base_params)
  123. {
  124. CompilerParameters res = new CompilerParameters ();
  125. res.TempFiles = temp_files;
  126. res.CompilerOptions = base_params.CompilerOptions;
  127. res.IncludeDebugInformation = base_params.IncludeDebugInformation;
  128. res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
  129. res.WarningLevel = base_params.WarningLevel;
  130. string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
  131. res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
  132. return res;
  133. }
  134. public static Type GetCompiledType (string virtualPath)
  135. {
  136. BuildProvider provider;
  137. CompilerParameters parameters;
  138. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  139. CompilerType ctype = provider.CodeCompilerType;
  140. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  141. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  142. return provider.GetGeneratedType (results);
  143. }
  144. // The 2 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
  145. public static Type GetType (string typeName, bool throwOnError)
  146. {
  147. return GetType (typeName, throwOnError, false);
  148. }
  149. public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
  150. {
  151. Type ret = null;
  152. try {
  153. foreach (Assembly asm in TopLevel_Assemblies) {
  154. ret = asm.GetType (typeName, throwOnError, ignoreCase);
  155. if (ret != null)
  156. break;
  157. }
  158. } catch (Exception ex) {
  159. throw new HttpException ("Failed to find the specified type.", ex);
  160. }
  161. return ret;
  162. }
  163. internal static ICollection GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
  164. {
  165. BuildProvider provider = bprovider;
  166. if (provider == null)
  167. provider = GetBuildProviderForPath (virtualPath, false);
  168. if (provider == null)
  169. return null;
  170. return provider.VirtualPathDependencies;
  171. }
  172. public static ICollection GetVirtualPathDependencies (string virtualPath)
  173. {
  174. return GetVirtualPathDependencies (virtualPath, null);
  175. }
  176. // Assemblies built from the App_Code directory
  177. public static IList CodeAssemblies {
  178. get { return AppCode_Assemblies; }
  179. }
  180. internal static IList TopLevelAssemblies {
  181. get { return TopLevel_Assemblies; }
  182. }
  183. internal static bool HaveResources {
  184. get { return haveResources; }
  185. set { haveResources = value; }
  186. }
  187. }
  188. }
  189. #endif