2 // System.Web.Compilation.BuildManager
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // (C) 2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.CodeDom.Compiler;
37 using System.Collections;
38 using System.Collections.Generic;
40 using System.Reflection;
42 using System.Web.Configuration;
43 using System.Web.Hosting;
45 namespace System.Web.Compilation {
46 public sealed class BuildManager {
47 private static List<Assembly> AppCode_Assemblies = new List<Assembly>();
48 private static List<Assembly> TopLevel_Assemblies = new List<Assembly>();
49 private static bool haveResources;
51 internal BuildManager ()
55 internal static void ThrowNoProviderException (string extension)
57 string msg = "No registered provider for extension '{0}'.";
58 throw new HttpException (String.Format (msg, extension));
61 public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
63 // virtualPath + Exists done in GetCompiledType()
64 if (requiredBaseType == null)
65 throw new NullReferenceException (); // This is what MS does, but from somewhere else.
68 Type type = GetCompiledType (virtualPath);
69 if (!requiredBaseType.IsAssignableFrom (type)) {
70 string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
71 type.FullName, requiredBaseType.FullName);
72 throw new HttpException (500, msg);
75 return Activator.CreateInstance (type, null);
78 [MonoTODO ("Not implemented, always returns null")]
79 public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
81 return null; // null is ok here until we store the dependency set in the Cache.
84 internal static BuildProvider GetBuildProviderForPath (string virtualPath, bool throwOnMissing)
86 string extension = VirtualPathUtility.GetExtension (virtualPath);
87 object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
88 BuildProviderCollection coll = o as BuildProviderCollection;
89 if (coll == null || coll.Count == 0)
90 ThrowNoProviderException (extension);
92 BuildProvider provider = coll.GetProviderForExtension (extension);
93 if (provider == null && throwOnMissing)
94 ThrowNoProviderException (extension);
98 public static Assembly GetCompiledAssembly (string virtualPath)
100 BuildProvider provider;
101 CompilerParameters parameters;
103 AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
104 CompilerType ctype = provider.CodeCompilerType;
105 parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
106 CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
107 return results.CompiledAssembly;
110 static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
112 if (virtualPath == null || virtualPath == "")
113 throw new ArgumentNullException ("virtualPath");
115 if (virtualPath [0] != '/')
116 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
118 if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
119 throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
120 provider = GetBuildProviderForPath (virtualPath, true);
121 CompilerType compiler_type = provider.CodeCompilerType;
122 Type ctype = compiler_type.CodeDomProviderType;
123 CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
125 AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
126 provider.SetVirtualPath (virtualPath);
127 provider.GenerateCode (abuilder);
131 public static string GetCompiledCustomString (string virtualPath)
133 BuildProvider provider = GetBuildProviderForPath (virtualPath, false);
134 if (provider == null)
136 // FIXME: where to get the CompilerResults from?
137 return provider.GetCustomString (null);
140 static CompilerParameters PrepareParameters (TempFileCollection temp_files,
142 CompilerParameters base_params)
144 CompilerParameters res = new CompilerParameters ();
145 res.TempFiles = temp_files;
146 res.CompilerOptions = base_params.CompilerOptions;
147 res.IncludeDebugInformation = base_params.IncludeDebugInformation;
148 res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
149 res.WarningLevel = base_params.WarningLevel;
150 string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
151 res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
155 public static Type GetCompiledType (string virtualPath)
157 BuildProvider provider;
158 CompilerParameters parameters;
160 AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
161 CompilerType ctype = provider.CodeCompilerType;
162 parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
163 CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
164 return provider.GetGeneratedType (results);
167 // The 2 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
168 public static Type GetType (string typeName, bool throwOnError)
170 return GetType (typeName, throwOnError, false);
173 public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
177 foreach (Assembly asm in TopLevel_Assemblies) {
178 ret = asm.GetType (typeName, throwOnError, ignoreCase);
182 } catch (Exception ex) {
183 throw new HttpException ("Failed to find the specified type.", ex);
188 internal static ICollection GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
190 BuildProvider provider = bprovider;
191 if (provider == null)
192 provider = GetBuildProviderForPath (virtualPath, false);
193 if (provider == null)
195 return provider.VirtualPathDependencies;
198 public static ICollection GetVirtualPathDependencies (string virtualPath)
200 return GetVirtualPathDependencies (virtualPath, null);
203 // Assemblies built from the App_Code directory
204 public static IList CodeAssemblies {
205 get { return AppCode_Assemblies; }
208 internal static IList TopLevelAssemblies {
209 get { return TopLevel_Assemblies; }
212 internal static bool HaveResources {
213 get { return haveResources; }
214 set { haveResources = value; }