AssemblyBuilder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // System.Web.Compilation.AssemblyBuilder
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.CodeDom;
  33. using System.CodeDom.Compiler;
  34. using System.Collections.Generic;
  35. using System.Collections.Specialized;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Web.Configuration;
  39. using System.Web.Util;
  40. namespace System.Web.Compilation {
  41. public class AssemblyBuilder {
  42. static bool KeepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  43. CodeDomProvider provider;
  44. List <CodeCompileUnit> units;
  45. List <string> source_files;
  46. List <string> referenced_assemblies;
  47. Dictionary <string, string> resource_files;
  48. TempFileCollection temp_files;
  49. string virtual_path;
  50. //TODO: there should be a Compile () method here which is where all the compilation exceptions are thrown from.
  51. internal AssemblyBuilder (CodeDomProvider provider)
  52. : this (null, provider)
  53. {}
  54. internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
  55. {
  56. this.provider = provider;
  57. this.virtual_path = virtualPath;
  58. units = new List <CodeCompileUnit> ();
  59. temp_files = new TempFileCollection ();
  60. referenced_assemblies = new List <string> ();
  61. CompilationSection section;
  62. if (virtualPath != null)
  63. section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath);
  64. else
  65. section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  66. string tempdir = section.TempDirectory;
  67. if (tempdir == null || tempdir == "")
  68. tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  69. temp_files = new TempFileCollection (tempdir, KeepFiles);
  70. }
  71. internal TempFileCollection TempFiles {
  72. get { return temp_files; }
  73. }
  74. internal CodeCompileUnit [] GetUnitsAsArray ()
  75. {
  76. CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
  77. units.CopyTo (result, 0);
  78. return result;
  79. }
  80. List <string> SourceFiles {
  81. get {
  82. if (source_files == null)
  83. source_files = new List <string> ();
  84. return source_files;
  85. }
  86. }
  87. Dictionary <string, string> ResourceFiles {
  88. get {
  89. if (resource_files == null)
  90. resource_files = new Dictionary <string, string> ();
  91. return resource_files;
  92. }
  93. }
  94. public void AddAssemblyReference (Assembly a)
  95. {
  96. if (a == null)
  97. throw new ArgumentNullException ("a");
  98. referenced_assemblies.Add (a.Location);
  99. }
  100. public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
  101. {
  102. if (buildProvider == null)
  103. throw new ArgumentNullException ("buildProvider");
  104. if (compileUnit == null)
  105. throw new ArgumentNullException ("compileUnit");
  106. units.Add (compileUnit);
  107. }
  108. public TextWriter CreateCodeFile (BuildProvider buildProvider)
  109. {
  110. if (buildProvider == null)
  111. throw new ArgumentNullException ("buildProvider");
  112. // Generate a file name with the correct source language extension
  113. string filename = GetTempFilePhysicalPath (provider.FileExtension);
  114. SourceFiles.Add (filename);
  115. return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  116. }
  117. internal void AddCodeFile (string path)
  118. {
  119. if (path == null || path.Length == 0)
  120. return;
  121. string extension = Path.GetExtension (path);
  122. if (extension == null || extension.Length == 0)
  123. return; // maybe better to throw an exception here?
  124. extension = extension.Substring (1);
  125. string filename = GetTempFilePhysicalPath (extension);
  126. File.Copy (path, filename, true);
  127. SourceFiles.Add (filename);
  128. }
  129. public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
  130. {
  131. if (buildProvider == null)
  132. throw new ArgumentNullException ("buildProvider");
  133. if (name == null || name == "")
  134. throw new ArgumentNullException ("name");
  135. string filename = GetTempFilePhysicalPath ("resource");
  136. Stream stream = File.OpenWrite (filename);
  137. ResourceFiles [name] = filename;
  138. return stream;
  139. }
  140. [MonoTODO ("Not implemented, does nothing")]
  141. public void GenerateTypeFactory (string typeName)
  142. {
  143. // Do nothing by now.
  144. }
  145. public string GetTempFilePhysicalPath (string extension)
  146. {
  147. if (extension == null)
  148. throw new ArgumentNullException ("extension");
  149. return temp_files.AddExtension (String.Format ("_{0}.{1}", temp_files.Count, extension), true);
  150. }
  151. public CodeDomProvider CodeDomProvider {
  152. get { return provider; }
  153. }
  154. internal CompilerResults BuildAssembly (CompilerParameters options)
  155. {
  156. return BuildAssembly (null, options);
  157. }
  158. internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
  159. {
  160. if (options == null)
  161. throw new ArgumentNullException ("options");
  162. CompilerResults results;
  163. CodeCompileUnit [] units = GetUnitsAsArray ();
  164. // Since we may have some source files and some code
  165. // units, we generate code from all of them and then
  166. // compile the assembly from the set of temporary source
  167. // files. This also facilates possible debugging for the
  168. // end user, since they get the code beforehand.
  169. List <string> files = SourceFiles;
  170. string filename;
  171. StreamWriter sw = null;
  172. foreach (CodeCompileUnit unit in units) {
  173. filename = GetTempFilePhysicalPath (provider.FileExtension);
  174. try {
  175. sw = new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  176. provider.GenerateCodeFromCompileUnit (unit, sw, null);
  177. files.Add (filename);
  178. } catch {
  179. throw;
  180. } finally {
  181. if (sw != null) {
  182. sw.Flush ();
  183. sw.Close ();
  184. }
  185. }
  186. }
  187. Dictionary <string, string> resources = ResourceFiles;
  188. foreach (KeyValuePair <string, string> de in resources)
  189. options.EmbeddedResources.Add (de.Value);
  190. foreach (string refasm in referenced_assemblies)
  191. options.ReferencedAssemblies.Add (refasm);
  192. results = provider.CompileAssemblyFromFile (options, files.ToArray ());
  193. // FIXME: generate the code and display it
  194. if (results.NativeCompilerReturnValue != 0)
  195. throw new CompilationException (virtualPath, results.Errors, "");
  196. Assembly assembly = results.CompiledAssembly;
  197. if (assembly == null) {
  198. if (!File.Exists (options.OutputAssembly)) {
  199. results.TempFiles.Delete ();
  200. throw new CompilationException (virtualPath, results.Errors,
  201. "No assembly returned after compilation!?");
  202. }
  203. results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
  204. }
  205. if (!KeepFiles)
  206. results.TempFiles.Delete ();
  207. return results;
  208. }
  209. }
  210. }
  211. #endif