2 // System.Web.Compilation.AssemblyBuilder
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2006 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
36 using System.CodeDom.Compiler;
37 using System.Collections.Generic;
38 using System.Collections.Specialized;
40 using System.Reflection;
41 using System.Web.Configuration;
42 using System.Web.Util;
44 namespace System.Web.Compilation {
46 public class AssemblyBuilder {
47 static bool KeepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
49 CodeDomProvider provider;
50 List <CodeCompileUnit> units;
51 List <string> source_files;
52 Dictionary <string, string> resource_files;
53 TempFileCollection temp_files;
55 //TODO: there should be a Compile () method here which is where all the compilation exceptions are thrown from.
57 internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
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;
70 temp_files = new TempFileCollection (tempdir, KeepFiles);
73 internal TempFileCollection TempFiles {
74 get { return temp_files; }
77 internal CodeCompileUnit [] GetUnitsAsArray ()
79 CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
80 units.CopyTo (result, 0);
84 List <string> SourceFiles {
86 if (source_files == null)
87 source_files = new List <string> ();
92 Dictionary <string, string> ResourceFiles {
94 if (resource_files == null)
95 resource_files = new Dictionary <string, string> ();
96 return resource_files;
100 public void AddAssemblyReference (Assembly a)
103 throw new ArgumentNullException ("a");
105 StringCollection coll = units [units.Count - 1].ReferencedAssemblies;
106 string location = a.Location;
107 if (coll.IndexOf (location) == -1)
111 [MonoTODO ("Do something with the buildProvider argument")]
112 public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
114 if (buildProvider == null)
115 throw new ArgumentNullException ("buildProvider");
117 if (compileUnit == null)
118 throw new ArgumentNullException ("compileUnit");
120 units.Add (compileUnit);
123 [MonoTODO ("Anything to do with the buildProvider argument?")]
124 public TextWriter CreateCodeFile (BuildProvider buildProvider)
126 if (buildProvider == null)
127 throw new ArgumentNullException ("buildProvider");
129 string filename = temp_files.AddExtension ("temp", true);
130 SourceFiles.Add (filename);
131 return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
134 [MonoTODO ("Anything to do with the buildProvider argument?")]
135 public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
137 if (buildProvider == null)
138 throw new ArgumentNullException ("buildProvider");
140 if (name == null || name == "")
141 throw new ArgumentNullException ("name");
143 string filename = temp_files.AddExtension ("resource", true);
144 Stream stream = File.OpenWrite (filename);
145 ResourceFiles [name] = filename;
150 public void GenerateTypeFactory (string typeName)
152 // Do nothing by now.
155 public string GetTempFilePhysicalPath (string extension)
157 if (extension == null)
158 throw new ArgumentNullException ("extension");
160 return temp_files.AddExtension (extension, true);
163 public CodeDomProvider CodeDomProvider {
164 get { return provider; }
167 internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
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, "");
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!?");
184 results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
187 results.TempFiles.Delete ();