Author: mhabersack
Date: 2006-11-08 07:08:05 -0500 (Wed, 08 Nov 2006)
New Revision: 67511

Added:
   trunk/mcs/class/System.Web/System.Web.Compilation/AppCodeCompiler.cs
Modified:
   trunk/mcs/class/System.Web/ChangeLog
   trunk/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs
   trunk/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs
   trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog
   trunk/mcs/class/System.Web/System.Web.dll.sources
   trunk/mcs/class/System.Web/System.Web/ChangeLog
   trunk/mcs/class/System.Web/System.Web/HttpApplicationFactory.cs
Log:
Implementation of the App_Code folder compiler for ASP.NET 2.0

Modified: trunk/mcs/class/System.Web/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/ChangeLog        2006-11-08 11:12:09 UTC (rev 
67510)
+++ trunk/mcs/class/System.Web/ChangeLog        2006-11-08 12:08:05 UTC (rev 
67511)
@@ -1,3 +1,7 @@
+2006-11-08  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * System.Web.dll.sources: added the App_Code compiler
+
 2006-11-05 Vladimir Krasnov <[EMAIL PROTECTED]>
 
        * System.Web.dll.sources: added System.Web.Profile/ProfileParser.cs

Modified: trunk/mcs/class/System.Web/System.Web/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web/ChangeLog     2006-11-08 11:12:09 UTC 
(rev 67510)
+++ trunk/mcs/class/System.Web/System.Web/ChangeLog     2006-11-08 12:08:05 UTC 
(rev 67511)
@@ -1,3 +1,7 @@
+2006-11-08  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * HttpApplicationFactory.cs: Add invocation of the App_Code compiler
+
 2006-11-07  Andrew Skiba  <[EMAIL PROTECTED]>
 
        * HttpRuntime.cs: TARGET_JVM ifdef

Modified: trunk/mcs/class/System.Web/System.Web/HttpApplicationFactory.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web/HttpApplicationFactory.cs     
2006-11-08 11:12:09 UTC (rev 67510)
+++ trunk/mcs/class/System.Web/System.Web/HttpApplicationFactory.cs     
2006-11-08 12:08:05 UTC (rev 67511)
@@ -393,6 +393,14 @@
                                cr = alrc.Compile ();
                                if (cr != null && cr.CompiledAssembly != null)
                                        
WebConfigurationManager.ExtraAssemblies.Add (cr.PathToAssembly);
+                               
+                               // Todo: Process App_WebResources here
+                               
+                               // Todo: Generate profile properties assembly 
from Web.config here
+                               
+                               // Todo: Compile code from App_Code here
+                               AppCodeCompiler acc = new AppCodeCompiler ();
+                               acc.Compile ();
 #endif
 
                                if (File.Exists (app_file)) {

Added: trunk/mcs/class/System.Web/System.Web.Compilation/AppCodeCompiler.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Compilation/AppCodeCompiler.cs        
2006-11-08 11:12:09 UTC (rev 67510)
+++ trunk/mcs/class/System.Web/System.Web.Compilation/AppCodeCompiler.cs        
2006-11-08 12:08:05 UTC (rev 67511)
@@ -0,0 +1,235 @@
+//
+// System.Web.Compilation.AppCodeCompiler: A compiler for the App_Code folder
+//
+// Authors:
+//   Marek Habersack ([EMAIL PROTECTED])
+//
+// (C) 2006 Marek Habersack
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+#if NET_2_0
+using System;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.Collections;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Web.Configuration;
+
+namespace System.Web.Compilation
+{
+       internal class AppCodeAssembly
+       {
+               private static Random rnd = new Random ();
+               
+               private List<string> files;
+               private string name;
+               private string path;
+               private bool validAssembly;
+               
+               public bool IsValid
+               {
+                       get { return validAssembly; }
+               }
+
+               public string SourcePath
+               {
+                       get { return path; }
+               }
+
+// temporary
+               public string Name
+               {
+                       get { return name; }
+               }
+               
+               public List<string> Files
+               {
+                       get { return files; }
+               }
+// temporary
+               
+               public AppCodeAssembly (string name, string path)
+               {
+                       this.files = new List<string>();
+                       this.validAssembly = true;
+                       this.name = name;
+                       this.path = path;
+               }
+
+               public void AddFile (string path)
+               {
+                       files.Add (path);
+               }
+               
+               // Build and add the assembly to the BuildManager's
+               // CodeAssemblies collection
+               public void Build ()
+               {
+                       Type compilerProvider = null;
+                       CompilerInfo compilerInfo = null, cit;
+                       string extension, language, cpfile = null;
+                       List<string> knownfiles = new List<string>();
+                       List<string> unknownfiles = new List<string>();
+                       
+                       // First make sure all the files are in the same
+                       // language
+                       bool known;
+                       foreach (string f in files) {
+                               known = true;
+                               language = null;
+                               
+                               extension = Path.GetExtension (f);
+                               if (!CodeDomProvider.IsDefinedExtension 
(extension))
+                                       known = false;
+                               if (known) {
+                                       language = 
CodeDomProvider.GetLanguageFromExtension(extension);
+                                       if (!CodeDomProvider.IsDefinedLanguage 
(language))
+                                               known = false;
+                               }
+                               if (!known || language == null) {
+                                       unknownfiles.Add (f);
+                                       continue;
+                               }
+                               
+                               cit = CodeDomProvider.GetCompilerInfo 
(language);
+                               if (cit == null || 
!cit.IsCodeDomProviderTypeValid)
+                                       continue;
+                               if (compilerProvider == null) {
+                                       cpfile = f;
+                                       compilerProvider = 
cit.CodeDomProviderType;
+                                       compilerInfo = cit;
+                               } else if (compilerProvider != 
cit.CodeDomProviderType)
+                                       throw new HttpException (
+                                               String.Format (
+                                                       "Files {0} and {1} are 
in different languages - they cannot be compiled into the same assembly",
+                                                       Path.GetFileName 
(cpfile),
+                                                       Path.GetFileName (f)));
+                               knownfiles.Add (f);
+                       }
+
+                       CodeDomProvider provider = null;
+                       // First the files in the language we are aware of
+                       if (compilerInfo != null) {
+                               provider = compilerInfo.CreateProvider ();
+                               if (provider == null)
+                                       throw new HttpException ("Unable to 
create a compiler for App_Code contents");
+
+                               CompilerParameters parameters = 
compilerInfo.CreateDefaultCompilerParameters ();
+                               int num = rnd.Next () + 1;
+                               
+                               string assemblyName = Path.Combine (
+                                       
AppDomain.CurrentDomain.SetupInformation.DynamicBase,
+                                       String.Format ("{0}.{1}.dll", name, 
num.ToString ("x")));
+                               parameters.OutputAssembly = assemblyName;
+                               CompilerResults results = 
provider.CompileAssemblyFromFile (parameters, knownfiles.ToArray ());
+                               if (results.Errors.Count == 0) {
+                                       IList assemblies = 
BuildManager.CodeAssemblies;
+                                       if (assemblies == null)
+                                               throw new HttpException 
("Internal error compiling code from the App_Code folder.");
+                                       assemblies.Add (results.PathToAssembly);
+                               } else {
+                                       if 
(HttpContext.Current.IsCustomErrorEnabled)
+                                               throw new HttpException ("An 
error occurred while initializing application.");
+                                       throw new CompilationException (null, 
results.Errors, null);
+                               }
+                       }
+               }
+       }
+       
+       internal class AppCodeCompiler
+       {
+               // A dictionary that contains an entry per an assembly that will
+               // be produced by compiling App_Code. There's one main assembly
+               // and an optional number of assemblies as defined by the
+               // codeSubDirectories sub-element of the compilation element in
+               // the system.web section of the app's config file.
+               // Each entry's value is an AppCodeAssembly instance.
+               //
+               // Assemblies are named as follows:
+               //
+               //  1. main assembly: App_Code.{HASH}
+               //  2. subdir assemblies: App_SubCode_{DirName}.{HASH}
+               //
+               // If any of the assemblies contains files that would be
+               // compiled with different compilers, a System.Web.HttpException
+               // is thrown.
+               //
+               // Files for which there is no explicit builder are ignored
+               // silently
+               //
+               // 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;
+               
+               public AppCodeCompiler ()
+               {
+                       assemblies = new List<AppCodeAssembly>();
+               }
+
+               public void Compile ()
+               {
+                       string appCode = Path.Combine 
(HttpRuntime.AppDomainAppPath, "App_Code");
+                       if (!Directory.Exists (appCode))
+                               return;
+                       
+                       // First process the codeSubDirectories
+                       CompilationSection cs = (CompilationSection) 
WebConfigurationManager.GetSection ("system.web/compilation");
+                       
+                       if (cs != null) {
+                               string aname;
+                               for (int i = 0; i < 
cs.CodeSubDirectories.Count; i++) {
+                                       aname = String.Format 
("App_SubCode_{0}", cs.CodeSubDirectories[i].DirectoryName);
+                                       assemblies.Add (new AppCodeAssembly (
+                                                               aname,
+                                                               Path.Combine 
(appCode, cs.CodeSubDirectories[i].DirectoryName)));
+                               }
+                       }
+                       AppCodeAssembly defasm = new AppCodeAssembly 
("App_Code", appCode);
+                       assemblies.Add (defasm);
+                       CollectFiles (appCode, defasm);
+
+                       foreach (AppCodeAssembly aca in assemblies)
+                               aca.Build ();
+               }
+
+               private void CollectFiles (string dir, AppCodeAssembly aca)
+               {
+                       AppCodeAssembly curaca = aca;
+                       foreach (string f in Directory.GetFiles (dir))
+                               aca.AddFile (f);
+                       foreach (string d in Directory.GetDirectories (dir)) {
+                               foreach (AppCodeAssembly a in assemblies)
+                                       if (a.SourcePath == d) {
+                                               curaca = a;
+                                               break;
+                                       }
+                               CollectFiles (d, curaca);
+                               curaca = aca;
+                       }
+               }
+       }
+}
+#endif

Modified: trunk/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs   
2006-11-08 11:12:09 UTC (rev 67510)
+++ trunk/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs   
2006-11-08 12:08:05 UTC (rev 67511)
@@ -113,12 +113,17 @@
 #if NET_2_0
                        ArrayList al = WebConfigurationManager.ExtraAssemblies;
                        if (al != null && al.Count > 0) {
-                               foreach (object o in al) {
-                                       if (o is string) {
+                               foreach (object o in al)
+                                       if (o is string)
                                                unit.ReferencedAssemblies.Add 
((string) o);
-                                       }
-                               }
                        }
+
+                       IList list = BuildManager.CodeAssemblies;
+                       if (list != null && list.Count > 0) {
+                               foreach (object o in list)
+                                       if (o is string)
+                                               unit.ReferencedAssemblies.Add 
((string) o);
+                       }
 #endif
                        // Late-bound generators specifics (as for 
MonoBASIC/VB.NET)
                        unit.UserData["RequireVariableDeclaration"] = 
parser.ExplicitOn;

Modified: trunk/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs   
2006-11-08 11:12:09 UTC (rev 67510)
+++ trunk/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs   
2006-11-08 12:08:05 UTC (rev 67511)
@@ -35,6 +35,7 @@
 using System.CodeDom;
 using System.CodeDom.Compiler;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 using System.Web;
@@ -43,6 +44,8 @@
 
 namespace System.Web.Compilation {
        public sealed class BuildManager {
+               private static List<string> AppCodeAssemblies = new 
List<string>();
+               
                internal BuildManager ()
                {
                }
@@ -172,10 +175,9 @@
                }
 
                // Assemblies built from the App_Code directory
-               [MonoTODO]
                public static IList CodeAssemblies {
                        get {
-                               throw new NotImplementedException ();
+                               return AppCodeAssemblies;
                        }
                }
 

Modified: trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog 2006-11-08 
11:12:09 UTC (rev 67510)
+++ trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog 2006-11-08 
12:08:05 UTC (rev 67511)
@@ -1,3 +1,11 @@
+2006-11-08  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * AppCodeCompiler.cs: The App_Code compiler classes
+
+       * BuildManager.cs: Implement the CodeAssemblies property
+
+       * BaseCompiler.cs: Reference the assemblies from App_Code, if any
+
 2006-10-18  Marek Habersack  <[EMAIL PROTECTED]>
 
        * TemplateControlCompiler.cs: add support for resource

Modified: trunk/mcs/class/System.Web/System.Web.dll.sources
===================================================================
--- trunk/mcs/class/System.Web/System.Web.dll.sources   2006-11-08 11:12:09 UTC 
(rev 67510)
+++ trunk/mcs/class/System.Web/System.Web.dll.sources   2006-11-08 12:08:05 UTC 
(rev 67511)
@@ -16,6 +16,7 @@
 System.Web/BrowserCapabilities.cs
 System.Web.Compilation/AppSettingsExpressionBuilder.cs
 System.Web.Compilation/AspComponentFoundry.cs
+System.Web.Compilation/AppCodeCompiler.cs
 System.Web.Compilation/AppResourcesCompiler.cs
 System.Web.Compilation/AppResourceFilesCompiler.cs
 System.Web.Compilation/AspGenerator.cs

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to