BuildManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.IO;
  36. using System.Reflection;
  37. using System.Web;
  38. using System.Web.Configuration;
  39. using System.Web.Hosting;
  40. namespace System.Web.Compilation {
  41. public sealed class BuildManager {
  42. internal BuildManager ()
  43. {
  44. }
  45. internal static void ThrowNoProviderException (string extension)
  46. {
  47. string msg = "No registered provider for extension '{0}'.";
  48. throw new HttpException (String.Format (msg, extension));
  49. }
  50. public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
  51. {
  52. // virtualPath + Exists done in GetCompiledType()
  53. if (requiredBaseType == null)
  54. throw new NullReferenceException (); // This is what MS does, but from somewhere else.
  55. // Get the Type.
  56. Type type = GetCompiledType (virtualPath);
  57. if (!requiredBaseType.IsAssignableFrom (type)) {
  58. string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
  59. type.FullName, requiredBaseType.FullName);
  60. throw new HttpException (500, msg);
  61. }
  62. return Activator.CreateInstance (type, null);
  63. }
  64. [MonoTODO]
  65. public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
  66. {
  67. return null; // null is ok here until we store the dependency set in the Cache.
  68. }
  69. public static Assembly GetCompiledAssembly (string virtualPath)
  70. {
  71. if (virtualPath == null || virtualPath == "")
  72. throw new ArgumentNullException ("virtualPath");
  73. if (virtualPath [0] != '/')
  74. throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
  75. if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
  76. throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
  77. object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
  78. BuildProviderCollection coll = (BuildProviderCollection) o;
  79. string extension = VirtualPathUtility.GetExtension (virtualPath);
  80. if (coll == null || coll.Count == 0)
  81. ThrowNoProviderException (extension);
  82. BuildProvider build_provider = coll.GetProviderForExtension (extension);
  83. if (build_provider == null)
  84. ThrowNoProviderException (extension);
  85. CompilerType compiler_type = build_provider.CodeCompilerType;
  86. Type ctype = compiler_type.CodeDomProviderType;
  87. CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
  88. AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
  89. build_provider.SetVirtualPath (virtualPath);
  90. build_provider.GenerateCode (abuilder);
  91. CompilerResults results;
  92. CompilerParameters parameters;
  93. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, compiler_type.CompilerParameters);
  94. CodeCompileUnit [] units = abuilder.GetUnitsAsArray ();
  95. results = dom_provider.CompileAssemblyFromDom (parameters, units);
  96. // FIXME: generate the code and display it
  97. if (results.NativeCompilerReturnValue != 0)
  98. throw new CompilationException (virtualPath, results.Errors, "");
  99. Assembly assembly = results.CompiledAssembly;
  100. if (assembly == null) {
  101. if (!File.Exists (parameters.OutputAssembly)) {
  102. results.TempFiles.Delete ();
  103. throw new CompilationException (virtualPath, results.Errors,
  104. "No assembly returned after compilation!?");
  105. }
  106. assembly = Assembly.LoadFrom (parameters.OutputAssembly);
  107. }
  108. results.TempFiles.Delete ();
  109. return assembly;
  110. }
  111. [MonoTODO]
  112. public static string GetCompiledCustomString (string virtualPath)
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. static CompilerParameters PrepareParameters (TempFileCollection temp_files,
  117. string virtualPath,
  118. CompilerParameters base_params)
  119. {
  120. CompilerParameters res = new CompilerParameters ();
  121. res.TempFiles = temp_files;
  122. res.CompilerOptions = base_params.CompilerOptions;
  123. res.IncludeDebugInformation = base_params.IncludeDebugInformation;
  124. res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
  125. res.WarningLevel = base_params.WarningLevel;
  126. string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
  127. res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
  128. return res;
  129. }
  130. public static Type GetCompiledType (string virtualPath)
  131. {
  132. Assembly assembly = GetCompiledAssembly (virtualPath);
  133. Type [] types = assembly.GetTypes ();
  134. if (types.Length != 1)
  135. throw new CompilationException (virtualPath, null, "");
  136. return types [0];
  137. }
  138. // The 3 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
  139. [MonoTODO]
  140. public static Type GetType (string typeName, bool throwOnError)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. [MonoTODO]
  145. public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. [MonoTODO]
  150. public static ICollection GetVirtualPathDependencies (string virtualPath)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. // Assemblies built from the App_Code directory
  155. [MonoTODO]
  156. public static IList CodeAssemblies {
  157. get {
  158. throw new NotImplementedException ();
  159. }
  160. }
  161. }
  162. }
  163. #endif