2008-11-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AppCodeCompiler.cs
index 5d436e44aa5a6280dd99b6ebacc5c1d01484ed32..4820ba47412ef6a76b4bebf68124ddcd3421385b 100644 (file)
@@ -88,13 +88,13 @@ namespace System.Web.Compilation
        
        internal class AppCodeAssembly
        {
-               private List<string> files;
-               private List<CodeCompileUnit> units;
+               List<string> files;
+               List<CodeCompileUnit> units;
                
-               private string name;
-               private string path;
-               private bool validAssembly;
-               private string outputAssemblyName;
+               string name;
+               string path;
+               bool validAssembly;
+               string outputAssemblyName;
 
                public string OutputAssemblyName
                {
@@ -257,6 +257,9 @@ namespace System.Web.Compilation
                        foreach (Assembly a in BuildManager.TopLevelAssemblies)
                                parameters.ReferencedAssemblies.Add (a.Location);
                        CompilerResults results = abuilder.BuildAssembly (parameters);
+                       if (results == null)
+                               return;
+                       
                        if (results.NativeCompilerReturnValue == 0) {
                                BuildManager.CodeAssemblies.Add (results.CompiledAssembly);
                                BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
@@ -268,12 +271,12 @@ namespace System.Web.Compilation
                        }
                }
                
-               private string PhysicalToVirtual (string file)
+               VirtualPath PhysicalToVirtual (string file)
                {
-                       return file.Replace (HttpRuntime.AppDomainAppPath, "/").Replace (Path.DirectorySeparatorChar, '/');
+                       return new VirtualPath (file.Replace (HttpRuntime.AppDomainAppPath, "/").Replace (Path.DirectorySeparatorChar, '/'));
                }
                
-               private BuildProvider GetBuildProviderFor (string file, BuildProviderCollection buildProviders)
+               BuildProvider GetBuildProviderFor (string file, BuildProviderCollection buildProviders)
                {
                        if (file == null || file.Length == 0 || buildProviders == null || buildProviders.Count == 0)
                                return null;
@@ -287,7 +290,7 @@ namespace System.Web.Compilation
                        return null;
                }
 
-               private bool IsCorrectBuilderType (BuildProvider bp)
+               bool IsCorrectBuilderType (BuildProvider bp)
                {
                        if (bp == null)
                                return false;
@@ -320,7 +323,7 @@ namespace System.Web.Compilation
        
        internal class AppCodeCompiler
        {
-               static private bool _alreadyCompiled;
+               static bool _alreadyCompiled;
                internal static string DefaultAppCodeAssemblyName;
                
                // A dictionary that contains an entry per an assembly that will
@@ -345,7 +348,7 @@ namespace System.Web.Compilation
                // Files for which exist BuildProviders but which have no
                // unambiguous language assigned to them (e.g. .wsdl files), are
                // built using the default website compiler.
-               private List<AppCodeAssembly> assemblies;
+               List<AppCodeAssembly> assemblies;
                string providerTypeName = null;
                
                public AppCodeCompiler ()
@@ -580,12 +583,20 @@ namespace System.Web.Compilation
                        }
                        
                        string baseType = ps.Inherits;
-                       bool baseIsGlobal = false;
-                       if (String.IsNullOrEmpty (baseType)) {
+                       if (String.IsNullOrEmpty (baseType))
                                baseType = "System.Web.Profile.ProfileBase";
-                               baseIsGlobal = true;
+                       else {
+                               string[] parts = baseType.Split (new char[] {','});
+                               if (parts.Length > 1)
+                                       baseType = parts [0].Trim ();
                        }
                        
+                       bool baseIsGlobal;
+                       if (baseType.IndexOf ('.') != -1)
+                               baseIsGlobal = true;
+                       else
+                               baseIsGlobal = false;
+                       
                        BuildProfileClass (ps, "ProfileCommon", props, ns, baseType, baseIsGlobal, groupProperties);
                        return true;
                }
@@ -634,7 +645,7 @@ namespace System.Web.Compilation
 
                        if (!haveCode)
                                return;
-
+                       
                        HttpRuntime.EnableAssemblyMapping (true);
                        string[] binAssemblies = HttpApplication.BinDirectoryAssemblies;
                        
@@ -642,6 +653,8 @@ namespace System.Web.Compilation
                                aca.Build (binAssemblies);
                        _alreadyCompiled = true;
                        DefaultAppCodeAssemblyName = Path.GetFileNameWithoutExtension (defasm.OutputAssemblyName);
+
+                       RunAppInitialize ();
                        
                        if (haveCustomProfile && providerTypeName != null) {
                                if (Type.GetType (providerTypeName, false) == null) {
@@ -661,7 +674,43 @@ namespace System.Web.Compilation
                        }
                }
 
-               private bool CollectFiles (string dir, AppCodeAssembly aca)
+               // Documented (sort of...) briefly in:
+               //
+               //   http://quickstarts.asp.net/QuickStartv20/aspnet/doc/extensibility.aspx
+               //   http://msdn2.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx
+               void RunAppInitialize ()
+               {
+                       MethodInfo mi = null, tmi;
+                       Type[] types;
+                       
+                       foreach (Assembly asm in BuildManager.CodeAssemblies) {
+                               types = asm.GetExportedTypes ();
+                               if (types == null || types.Length == 0)
+                                       continue;
+
+                               foreach (Type type in types) {
+                                       tmi = type.GetMethod ("AppInitialize",
+                                                             BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase,
+                                                             null,
+                                                             Type.EmptyTypes,
+                                                             null);
+                                       if (tmi == null)
+                                               continue;
+
+                                       if (mi != null)
+                                               throw new HttpException ("The static AppInitialize method found in more than one type in the App_Code directory.");
+
+                                       mi = tmi;
+                               }
+                       }
+
+                       if (mi == null)
+                               return;
+
+                       mi.Invoke (null, null);
+               }
+               
+               bool CollectFiles (string dir, AppCodeAssembly aca)
                {
                        bool haveFiles = false;