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 List <string> referenced_assemblies;
53 Dictionary <string, string> resource_files;
54 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 (CodeDomProvider provider)
58 : this (null, provider)
61 internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
63 this.provider = provider;
64 units = new List <CodeCompileUnit> ();
65 temp_files = new TempFileCollection ();
66 referenced_assemblies = new List <string> ();
67 CompilationSection section;
68 if (virtualPath != null)
69 section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath);
71 section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
72 string tempdir = section.TempDirectory;
73 if (tempdir == null || tempdir == "")
74 tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
76 temp_files = new TempFileCollection (tempdir, KeepFiles);
79 internal TempFileCollection TempFiles {
80 get { return temp_files; }
83 internal CodeCompileUnit [] GetUnitsAsArray ()
85 CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
86 units.CopyTo (result, 0);
90 List <string> SourceFiles {
92 if (source_files == null)
93 source_files = new List <string> ();
98 Dictionary <string, string> ResourceFiles {
100 if (resource_files == null)
101 resource_files = new Dictionary <string, string> ();
102 return resource_files;
106 public void AddAssemblyReference (Assembly a)
109 throw new ArgumentNullException ("a");
111 referenced_assemblies.Add (a.Location);
114 internal void AddCodeCompileUnit (CodeCompileUnit compileUnit)
116 if (compileUnit == null)
117 throw new ArgumentNullException ("compileUnit");
118 units.Add (compileUnit);
121 public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
123 if (buildProvider == null)
124 throw new ArgumentNullException ("buildProvider");
126 if (compileUnit == null)
127 throw new ArgumentNullException ("compileUnit");
129 units.Add (compileUnit);
132 public TextWriter CreateCodeFile (BuildProvider buildProvider)
134 if (buildProvider == null)
135 throw new ArgumentNullException ("buildProvider");
137 // Generate a file name with the correct source language extension
138 string filename = GetTempFilePhysicalPath (provider.FileExtension);
139 SourceFiles.Add (filename);
140 return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
143 internal void AddCodeFile (string path)
145 if (path == null || path.Length == 0)
147 string extension = Path.GetExtension (path);
148 if (extension == null || extension.Length == 0)
149 return; // maybe better to throw an exception here?
150 extension = extension.Substring (1);
151 string filename = GetTempFilePhysicalPath (extension);
152 File.Copy (path, filename, true);
153 SourceFiles.Add (filename);
156 public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
158 if (buildProvider == null)
159 throw new ArgumentNullException ("buildProvider");
161 if (name == null || name == "")
162 throw new ArgumentNullException ("name");
164 string filename = GetTempFilePhysicalPath ("resource");
165 Stream stream = File.OpenWrite (filename);
166 ResourceFiles [name] = filename;
170 [MonoTODO ("Not implemented, does nothing")]
171 public void GenerateTypeFactory (string typeName)
173 // Do nothing by now.
176 public string GetTempFilePhysicalPath (string extension)
178 if (extension == null)
179 throw new ArgumentNullException ("extension");
181 return temp_files.AddExtension (String.Format ("_{0}.{1}", temp_files.Count, extension), true);
184 public CodeDomProvider CodeDomProvider {
185 get { return provider; }
188 internal CompilerResults BuildAssembly (CompilerParameters options)
190 return BuildAssembly (null, options);
193 internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
196 throw new ArgumentNullException ("options");
198 CompilerResults results;
199 CodeCompileUnit [] units = GetUnitsAsArray ();
201 // Since we may have some source files and some code
202 // units, we generate code from all of them and then
203 // compile the assembly from the set of temporary source
204 // files. This also facilates possible debugging for the
205 // end user, since they get the code beforehand.
206 List <string> files = SourceFiles;
208 StreamWriter sw = null;
209 foreach (CodeCompileUnit unit in units) {
210 filename = GetTempFilePhysicalPath (provider.FileExtension);
212 sw = new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
213 provider.GenerateCodeFromCompileUnit (unit, sw, null);
214 files.Add (filename);
224 Dictionary <string, string> resources = ResourceFiles;
225 foreach (KeyValuePair <string, string> de in resources)
226 options.EmbeddedResources.Add (de.Value);
227 foreach (string refasm in referenced_assemblies)
228 options.ReferencedAssemblies.Add (refasm);
230 results = provider.CompileAssemblyFromFile (options, files.ToArray ());
232 if (results.NativeCompilerReturnValue != 0) {
233 string fileText = null;
235 using (StreamReader sr = File.OpenText (results.Errors [0].FileName)) {
236 fileText = sr.ReadToEnd ();
238 } catch (Exception) {}
240 throw new CompilationException (virtualPath, results.Errors, fileText);
243 Assembly assembly = results.CompiledAssembly;
244 if (assembly == null) {
245 if (!File.Exists (options.OutputAssembly)) {
246 results.TempFiles.Delete ();
247 throw new CompilationException (virtualPath, results.Errors,
248 "No assembly returned after compilation!?");
252 results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
253 } catch (Exception ex) {
254 results.TempFiles.Delete ();
255 throw new HttpException ("Unable to load compiled assembly");
260 results.TempFiles.Delete ();