2
0

AssemblyBuilder.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. internal void AddCodeCompileUnit (CodeCompileUnit compileUnit)
  101. {
  102. if (compileUnit == null)
  103. throw new ArgumentNullException ("compileUnit");
  104. units.Add (compileUnit);
  105. }
  106. public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
  107. {
  108. if (buildProvider == null)
  109. throw new ArgumentNullException ("buildProvider");
  110. if (compileUnit == null)
  111. throw new ArgumentNullException ("compileUnit");
  112. units.Add (compileUnit);
  113. }
  114. public TextWriter CreateCodeFile (BuildProvider buildProvider)
  115. {
  116. if (buildProvider == null)
  117. throw new ArgumentNullException ("buildProvider");
  118. // Generate a file name with the correct source language extension
  119. string filename = GetTempFilePhysicalPath (provider.FileExtension);
  120. SourceFiles.Add (filename);
  121. return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  122. }
  123. internal void AddCodeFile (string path)
  124. {
  125. if (path == null || path.Length == 0)
  126. return;
  127. string extension = Path.GetExtension (path);
  128. if (extension == null || extension.Length == 0)
  129. return; // maybe better to throw an exception here?
  130. extension = extension.Substring (1);
  131. string filename = GetTempFilePhysicalPath (extension);
  132. File.Copy (path, filename, true);
  133. SourceFiles.Add (filename);
  134. }
  135. public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
  136. {
  137. if (buildProvider == null)
  138. throw new ArgumentNullException ("buildProvider");
  139. if (name == null || name == "")
  140. throw new ArgumentNullException ("name");
  141. string filename = GetTempFilePhysicalPath ("resource");
  142. Stream stream = File.OpenWrite (filename);
  143. ResourceFiles [name] = filename;
  144. return stream;
  145. }
  146. [MonoTODO ("Not implemented, does nothing")]
  147. public void GenerateTypeFactory (string typeName)
  148. {
  149. // Do nothing by now.
  150. }
  151. public string GetTempFilePhysicalPath (string extension)
  152. {
  153. if (extension == null)
  154. throw new ArgumentNullException ("extension");
  155. return temp_files.AddExtension (String.Format ("_{0}.{1}", temp_files.Count, extension), true);
  156. }
  157. public CodeDomProvider CodeDomProvider {
  158. get { return provider; }
  159. }
  160. internal CompilerResults BuildAssembly (CompilerParameters options)
  161. {
  162. return BuildAssembly (null, options);
  163. }
  164. internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
  165. {
  166. if (options == null)
  167. throw new ArgumentNullException ("options");
  168. CompilerResults results;
  169. CodeCompileUnit [] units = GetUnitsAsArray ();
  170. // Since we may have some source files and some code
  171. // units, we generate code from all of them and then
  172. // compile the assembly from the set of temporary source
  173. // files. This also facilates possible debugging for the
  174. // end user, since they get the code beforehand.
  175. List <string> files = SourceFiles;
  176. string filename;
  177. StreamWriter sw = null;
  178. foreach (CodeCompileUnit unit in units) {
  179. filename = GetTempFilePhysicalPath (provider.FileExtension);
  180. try {
  181. sw = new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  182. provider.GenerateCodeFromCompileUnit (unit, sw, null);
  183. files.Add (filename);
  184. } catch {
  185. throw;
  186. } finally {
  187. if (sw != null) {
  188. sw.Flush ();
  189. sw.Close ();
  190. }
  191. }
  192. }
  193. Dictionary <string, string> resources = ResourceFiles;
  194. foreach (KeyValuePair <string, string> de in resources)
  195. options.EmbeddedResources.Add (de.Value);
  196. foreach (string refasm in referenced_assemblies)
  197. options.ReferencedAssemblies.Add (refasm);
  198. results = provider.CompileAssemblyFromFile (options, files.ToArray ());
  199. // FIXME: generate the code and display it
  200. if (results.NativeCompilerReturnValue != 0)
  201. throw new CompilationException (virtualPath, results.Errors, "");
  202. Assembly assembly = results.CompiledAssembly;
  203. if (assembly == null) {
  204. if (!File.Exists (options.OutputAssembly)) {
  205. results.TempFiles.Delete ();
  206. throw new CompilationException (virtualPath, results.Errors,
  207. "No assembly returned after compilation!?");
  208. }
  209. results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
  210. }
  211. if (!KeepFiles)
  212. results.TempFiles.Delete ();
  213. return results;
  214. }
  215. }
  216. }
  217. #endif