2006-02-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AssemblyBuilder.cs
1 //
2 // System.Web.Compilation.AssemblyBuilder
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.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.Generic;
38 using System.Collections.Specialized;
39 using System.IO;
40 using System.Reflection;
41 using System.Web.Configuration;
42 using System.Web.Util;
43
44 namespace System.Web.Compilation {
45
46         public class AssemblyBuilder {
47                 static bool KeepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
48
49                 CodeDomProvider provider;
50                 List <CodeCompileUnit> units;
51                 List <string> source_files;
52                 Dictionary <string, string> resource_files;
53                 TempFileCollection temp_files;
54                 string virtual_path;
55                 //TODO: there should be a Compile () method here which is where all the compilation exceptions are thrown from.
56                 
57                 internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
58                 {
59                         this.provider = provider;
60                         this.virtual_path = virtualPath;
61                         units = new List <CodeCompileUnit> ();
62                         units.Add (new CodeCompileUnit ());
63                         temp_files = new TempFileCollection ();
64                         CompilationSection section;
65                         section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath);
66                         string tempdir = section.TempDirectory;
67                         if (tempdir == null || tempdir == "")
68                                 tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
69                                 
70                         temp_files = new TempFileCollection (tempdir, KeepFiles);
71                 }
72
73                 internal TempFileCollection TempFiles {
74                         get { return temp_files; }
75                 }
76
77                 internal CodeCompileUnit [] GetUnitsAsArray ()
78                 {
79                         CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
80                         units.CopyTo (result, 0);
81                         return result;
82                 }
83
84                 List <string> SourceFiles {
85                         get {
86                                 if (source_files == null)
87                                         source_files = new List <string> ();
88                                 return source_files;
89                         }
90                 }
91
92                 Dictionary <string, string> ResourceFiles {
93                         get {
94                                 if (resource_files == null)
95                                         resource_files = new Dictionary <string, string> ();
96                                 return resource_files;
97                         }
98                 }
99
100                 public void AddAssemblyReference (Assembly a)
101                 {
102                         if (a == null)
103                                 throw new ArgumentNullException ("a");
104
105                         StringCollection coll = units [units.Count - 1].ReferencedAssemblies;
106                         string location = a.Location;
107                         if (coll.IndexOf (location) == -1)
108                                 coll.Add (location);
109                 }
110
111                 [MonoTODO ("Do something with the buildProvider argument")]
112                 public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
113                 {
114                         if (buildProvider == null)
115                                 throw new ArgumentNullException ("buildProvider");
116
117                         if (compileUnit == null)
118                                 throw new ArgumentNullException ("compileUnit");
119
120                         units.Add (compileUnit);
121                 }
122
123                 [MonoTODO ("Anything to do with the buildProvider argument?")]
124                 public TextWriter CreateCodeFile (BuildProvider buildProvider)
125                 {
126                         if (buildProvider == null)
127                                 throw new ArgumentNullException ("buildProvider");
128
129                         string filename = temp_files.AddExtension ("temp", true);
130                         SourceFiles.Add (filename);
131                         return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
132                 }
133
134                 [MonoTODO ("Anything to do with the buildProvider argument?")]
135                 public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
136                 {
137                         if (buildProvider == null)
138                                 throw new ArgumentNullException ("buildProvider");
139
140                         if (name == null || name == "")
141                                 throw new ArgumentNullException ("name");
142
143                         string filename = temp_files.AddExtension ("resource", true);
144                         Stream stream = File.OpenWrite (filename);
145                         ResourceFiles [name] = filename;
146                         return stream;
147                 }
148
149                 [MonoTODO]
150                 public void GenerateTypeFactory (string typeName)
151                 {
152                         // Do nothing by now.
153                 }
154
155                 public string GetTempFilePhysicalPath (string extension)
156                 {
157                         if (extension == null)
158                                 throw new ArgumentNullException ("extension");
159
160                         return temp_files.AddExtension (extension, true);
161                 }
162
163                 public CodeDomProvider CodeDomProvider {
164                         get { return provider; }
165                 }
166
167                 internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
168                 {
169                         CompilerResults results;
170                         CodeCompileUnit [] units = GetUnitsAsArray ();
171                         results = provider.CompileAssemblyFromDom (options, units);
172                         // FIXME: generate the code and display it
173                         if (results.NativeCompilerReturnValue != 0)
174                                 throw new CompilationException (virtualPath, results.Errors, "");
175
176                         Assembly assembly = results.CompiledAssembly;
177                         if (assembly == null) {
178                                 if (!File.Exists (options.OutputAssembly)) {
179                                         results.TempFiles.Delete ();
180                                         throw new CompilationException (virtualPath, results.Errors,
181                                                 "No assembly returned after compilation!?");
182                                 }
183
184                                 results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
185                         }
186
187                         results.TempFiles.Delete ();
188                         return results;
189                 }
190         }
191 }
192 #endif
193