2007-08-14 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AssemblyBuilder.cs
index 6c4f1462a1f00d37f6eeb96d3ffbbe70f3d9fbaa..fda3ffdeb015480997bcb93a37b569b87d9ffc99 100644 (file)
@@ -52,7 +52,6 @@ namespace System.Web.Compilation {
                List <string> referenced_assemblies;
                Dictionary <string, string> resource_files;
                TempFileCollection temp_files;
-               string virtual_path;
                //TODO: there should be a Compile () method here which is where all the compilation exceptions are thrown from.
 
                internal AssemblyBuilder (CodeDomProvider provider)
@@ -62,7 +61,6 @@ namespace System.Web.Compilation {
                internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
                {
                        this.provider = provider;
-                       this.virtual_path = virtualPath;
                        units = new List <CodeCompileUnit> ();
                        temp_files = new TempFileCollection ();
                        referenced_assemblies = new List <string> ();
@@ -109,10 +107,17 @@ namespace System.Web.Compilation {
                {
                        if (a == null)
                                throw new ArgumentNullException ("a");
-                       
+
                        referenced_assemblies.Add (a.Location);
                }
 
+               internal void AddCodeCompileUnit (CodeCompileUnit compileUnit)
+               {
+                       if (compileUnit == null)
+                               throw new ArgumentNullException ("compileUnit");
+                       units.Add (compileUnit);
+               }
+               
                public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
                {
                        if (buildProvider == null)
@@ -216,19 +221,25 @@ namespace System.Web.Compilation {
                                        }
                                }
                        }
-
                        Dictionary <string, string> resources = ResourceFiles;
                        foreach (KeyValuePair <string, string> de in resources)
                                options.EmbeddedResources.Add (de.Value);
                        foreach (string refasm in referenced_assemblies)
                                options.ReferencedAssemblies.Add (refasm);
                        
-                       results = provider.CompileAssemblyFromFile (options, files.ToArray ());
-                       
-                       // FIXME: generate the code and display it
-                       if (results.NativeCompilerReturnValue != 0)
-                               throw new CompilationException (virtualPath, results.Errors, "");
+                       results = provider.CompileAssemblyFromFile (options, files.ToArray ());                 
 
+                       if (results.NativeCompilerReturnValue != 0) {
+                               string fileText = null;
+                               try {
+                                       using (StreamReader sr = File.OpenText (results.Errors [0].FileName)) {
+                                               fileText = sr.ReadToEnd ();
+                                       }
+                               } catch (Exception) {}
+                               
+                               throw new CompilationException (virtualPath, results.Errors, fileText);
+                       }
+                       
                        Assembly assembly = results.CompiledAssembly;
                        if (assembly == null) {
                                if (!File.Exists (options.OutputAssembly)) {
@@ -237,7 +248,12 @@ namespace System.Web.Compilation {
                                                "No assembly returned after compilation!?");
                                }
 
-                               results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
+                               try {
+                                       results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
+                               } catch (Exception ex) {
+                                       results.TempFiles.Delete ();
+                                       throw new HttpException ("Unable to load compiled assembly", ex);
+                               }
                        }
 
                        if (!KeepFiles)