merge -r 61110:61111
[mono.git] / mcs / class / System.Web / System.Web.Compilation / BuildManager.cs
1 //
2 // System.Web.Compilation.BuildManager
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 #if NET_2_0
33
34 using System;
35 using System.CodeDom;
36 using System.CodeDom.Compiler;
37 using System.Collections;
38 using System.IO;
39 using System.Reflection;
40 using System.Web;
41 using System.Web.Configuration;
42 using System.Web.Hosting;
43
44 namespace System.Web.Compilation {
45         public sealed class BuildManager {
46                 internal BuildManager ()
47                 {
48                 }
49
50                 internal static void ThrowNoProviderException (string extension)
51                 {
52                         string msg = "No registered provider for extension '{0}'.";
53                         throw new HttpException (String.Format (msg, extension));
54                 }
55
56                 public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
57                 {
58                         // virtualPath + Exists done in GetCompiledType()
59                         if (requiredBaseType == null)
60                                 throw new NullReferenceException (); // This is what MS does, but from somewhere else.
61
62                         // Get the Type.
63                         Type type = GetCompiledType (virtualPath);
64                         if (!requiredBaseType.IsAssignableFrom (type)) {
65                                 string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
66                                                                 type.FullName, requiredBaseType.FullName);
67                                 throw new HttpException (500, msg);
68                         }
69
70                         return Activator.CreateInstance (type, null);
71                 }
72
73                 [MonoTODO]
74                 public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
75                 {
76                         return null; // null is ok here until we store the dependency set in the Cache.
77                 }
78
79                 public static Assembly GetCompiledAssembly (string virtualPath)
80                 {
81                         BuildProvider provider;
82                         CompilerParameters parameters;
83
84                         AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
85                         CompilerType ctype = provider.CodeCompilerType;
86                         parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
87                         CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
88                         return results.CompiledAssembly;
89                 }
90
91                 static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
92                 {
93                         if (virtualPath == null || virtualPath == "")
94                                 throw new ArgumentNullException ("virtualPath");
95
96                         if (virtualPath [0] != '/')
97                                 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
98
99                         if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
100                                 throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
101
102                         object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
103                         BuildProviderCollection coll = (BuildProviderCollection) o;
104                         string extension = VirtualPathUtility.GetExtension (virtualPath);
105                         if (coll == null || coll.Count == 0)
106                                 ThrowNoProviderException (extension);
107
108                         provider = coll.GetProviderForExtension (extension);
109                         if (provider == null)
110                                 ThrowNoProviderException (extension);
111
112                         CompilerType compiler_type = provider.CodeCompilerType;
113                         Type ctype = compiler_type.CodeDomProviderType;
114                         CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
115
116                         AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
117                         provider.SetVirtualPath (virtualPath);
118                         provider.GenerateCode (abuilder);
119                         return abuilder;
120                 }
121
122                 [MonoTODO]
123                 public static string GetCompiledCustomString (string virtualPath)
124                 {
125                         throw new NotImplementedException ();
126                 }
127
128                 static CompilerParameters PrepareParameters (TempFileCollection temp_files,
129                                                                 string virtualPath,
130                                                                 CompilerParameters base_params)
131                 {
132                         CompilerParameters res = new CompilerParameters ();
133                         res.TempFiles = temp_files;
134                         res.CompilerOptions = base_params.CompilerOptions;
135                         res.IncludeDebugInformation = base_params.IncludeDebugInformation;
136                         res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
137                         res.WarningLevel = base_params.WarningLevel;
138                         string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
139                         res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
140                         return res;
141                 }
142
143                 public static Type GetCompiledType (string virtualPath)
144                 {
145                         BuildProvider provider;
146                         CompilerParameters parameters;
147
148                         AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
149                         CompilerType ctype = provider.CodeCompilerType;
150                         parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
151                         CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
152                         return provider.GetGeneratedType (results);
153                 }
154
155                 // The 3 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
156                 [MonoTODO]
157                 public static Type GetType (string typeName, bool throwOnError)
158                 {
159                         throw new NotImplementedException ();
160                 }
161
162                 [MonoTODO]
163                 public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
164                 {
165                         throw new NotImplementedException ();
166                 }
167
168                 [MonoTODO]
169                 public static ICollection GetVirtualPathDependencies (string virtualPath)
170                 {
171                         throw new NotImplementedException ();
172                 }
173
174                 // Assemblies built from the App_Code directory
175                 [MonoTODO]
176                 public static IList CodeAssemblies {
177                         get {
178                                 throw new NotImplementedException ();
179                         }
180                 }
181
182         }
183 }
184
185 #endif
186