ClientBuildManager.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // System.Web.Compilation.ClientBuildManager
  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;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Web;
  38. using System.Web.Hosting;
  39. namespace System.Web.Compilation {
  40. public sealed class ClientBuildManager : MarshalByRefObject, IDisposable {
  41. string virt_dir;
  42. string phys_src_dir;
  43. string phys_target_dir;
  44. ClientBuildManagerParameter build_params;
  45. BareApplicationHost host;
  46. ApplicationManager manager;
  47. string app_id;
  48. string cache_path;
  49. public ClientBuildManager (string appVirtualDir, string appPhysicalSourceDir)
  50. {
  51. if (appVirtualDir == null || appVirtualDir == "")
  52. throw new ArgumentNullException ("appVirtualDir");
  53. if (appPhysicalSourceDir == null || appPhysicalSourceDir == "")
  54. throw new ArgumentNullException ("appPhysicalSourceDir");
  55. virt_dir = appVirtualDir; // TODO: adjust vpath (it allows 'blah' that turns into '/blah', '////blah', '\\blah'...
  56. phys_src_dir = appPhysicalSourceDir;
  57. manager = ApplicationManager.GetApplicationManager ();
  58. }
  59. public ClientBuildManager (string appVirtualDir, string appPhysicalSourceDir,
  60. string appPhysicalTargetDir)
  61. : this (appVirtualDir, appPhysicalSourceDir)
  62. {
  63. if (appPhysicalTargetDir == null || appPhysicalTargetDir == "")
  64. throw new ArgumentNullException ("appPhysicalTargetDir");
  65. phys_target_dir = appPhysicalTargetDir;
  66. }
  67. public ClientBuildManager (string appVirtualDir, string appPhysicalSourceDir,
  68. string appPhysicalTargetDir, ClientBuildManagerParameter parameter)
  69. : this (appVirtualDir, appPhysicalSourceDir, appPhysicalTargetDir)
  70. {
  71. build_params = parameter;
  72. }
  73. public event BuildManagerHostUnloadEventHandler AppDomainShutdown;
  74. public event EventHandler AppDomainStarted;
  75. public event BuildManagerHostUnloadEventHandler AppDomainUnloaded;
  76. BareApplicationHost Host {
  77. get {
  78. if (host != null)
  79. return host;
  80. int hashcode = virt_dir.GetHashCode ();
  81. if (app_id != null)
  82. hashcode ^= Int32.Parse (app_id);
  83. app_id = hashcode.ToString (CultureInfo.InvariantCulture);
  84. host = manager.CreateHostWithCheck (app_id, virt_dir, phys_src_dir);
  85. cache_path = "";
  86. //cache_path = Path.Combine (Path.GetTempPath (),
  87. //String.Format ("{0}-temp-aspnet-{1:x}", Environment.UserName, i));
  88. int hash = virt_dir.GetHashCode () << 5 + phys_src_dir.GetHashCode ();
  89. cache_path = Path.Combine (cache_path, hash.ToString (CultureInfo.InvariantCulture));
  90. Directory.CreateDirectory (cache_path);
  91. OnAppDomainStarted ();
  92. return host;
  93. }
  94. }
  95. void OnAppDomainStarted ()
  96. {
  97. if (AppDomainStarted != null)
  98. AppDomainStarted (this, EventArgs.Empty);
  99. }
  100. void OnAppDomainShutdown (ApplicationShutdownReason reason)
  101. {
  102. if (AppDomainShutdown != null) {
  103. BuildManagerHostUnloadEventArgs args = new BuildManagerHostUnloadEventArgs (reason);
  104. AppDomainShutdown (this, args);
  105. }
  106. }
  107. void OnDomainUnload (object sender, EventArgs args)
  108. {
  109. OnAppDomainUnloaded (0); // FIXME: set a reason?
  110. }
  111. void OnAppDomainUnloaded (ApplicationShutdownReason reason)
  112. {
  113. if (AppDomainUnloaded != null) {
  114. BuildManagerHostUnloadEventArgs args = new BuildManagerHostUnloadEventArgs (reason);
  115. AppDomainUnloaded (this, args);
  116. }
  117. }
  118. [MonoTODO]
  119. public void CompileApplicationDependencies ()
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. public void CompileFile (string virtualPath)
  124. {
  125. CompileFile (virtualPath, null);
  126. }
  127. [MonoTODO]
  128. public void CompileFile (string virtualPath, ClientBuildManagerCallback callback)
  129. {
  130. // 1. Creates the Host
  131. // This creates a .compiled file + an assembly
  132. // App_Code reported to be built *before* anything else (with progress callback)
  133. throw new NotImplementedException ();
  134. }
  135. public IRegisteredObject CreateObject (Type type, bool failIfExists)
  136. {
  137. return manager.CreateObject (app_id, type, virt_dir, phys_src_dir, failIfExists);
  138. }
  139. public string GenerateCode (string virtualPath, string virtualFileString, out IDictionary linePragmasTable)
  140. {
  141. // This thing generates a .ccu (CodeCompileUnit?) file (reported as TrueType font data by 'file'!)
  142. // resultType=7 vs. resultType=3 for assemblies reported in the .compiled file
  143. // The virtual path is just added to the dependencies list, but is checked to be an absolute path.
  144. // IsHostCreated returns true after calling this method.
  145. //
  146. // A null file string causes virtualPath to be mapped and read to generate the code
  147. //
  148. if (virtualPath == null || virtualPath == "")
  149. throw new ArgumentNullException ("virtualPath");
  150. CodeCompileUnit unit = null;
  151. Type cprovider_type;
  152. CompilerParameters parameters;
  153. unit = GenerateCodeCompileUnit (virtualPath, virtualFileString, out cprovider_type,
  154. out parameters, out linePragmasTable);
  155. return null;
  156. }
  157. [MonoTODO]
  158. public CodeCompileUnit GenerateCodeCompileUnit (string virtualPath,
  159. string virtualFileString,
  160. out Type codeDomProviderType,
  161. out CompilerParameters compilerParameters,
  162. out IDictionary linePragmasTable)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. public CodeCompileUnit GenerateCodeCompileUnit (string virtualPath,
  167. out Type codeDomProviderType,
  168. out CompilerParameters compilerParameters,
  169. out IDictionary linePragmasTable)
  170. {
  171. return GenerateCodeCompileUnit (virtualPath, out codeDomProviderType,
  172. out compilerParameters, out linePragmasTable);
  173. }
  174. static string [] shutdown_directories = new string [] {
  175. "bin", "App_GlobalResources", "App_Code",
  176. "App_WebReferences", "App_Browsers" };
  177. public string [] GetAppDomainShutdownDirectories ()
  178. {
  179. return shutdown_directories;
  180. }
  181. [MonoTODO]
  182. public IDictionary GetBrowserDefinitions ()
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. [MonoTODO]
  187. public void GetCodeDirectoryInformation (string virtualCodeDir,
  188. out Type codeDomProviderType,
  189. out CompilerParameters compilerParameters,
  190. out string generatedFilesDir)
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. [MonoTODO]
  195. public Type GetCompiledType (string virtualPath)
  196. {
  197. // CompileFile + get the type based on .compiled file information
  198. // Throws if virtualPath is a special virtual directory (App_Code et al)
  199. throw new NotImplementedException ();
  200. }
  201. [MonoTODO]
  202. public void GetCompilerParameters (string virtualPath,
  203. out Type codeDomProviderType,
  204. out CompilerParameters compilerParameters)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. [MonoTODO]
  209. public string GetGeneratedFileVirtualPath (string filePath)
  210. {
  211. // returns empty string for any vpath. Test with real paths.
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. public string GetGeneratedSourceFile (string virtualPath)
  216. {
  217. // This one takes a directory name /xxx and /xxx/App_Code throw either
  218. // a KeyNotFoundException or an InvalidOperationException
  219. throw new NotImplementedException ();
  220. }
  221. [MonoTODO]
  222. public string [] GetTopLevelAssemblyReferences (string virtualPath)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. public string [] GetVirtualCodeDirectories ()
  227. {
  228. // Host is created here when needed. (Unload()+GetVirtualCodeDirectories()+IsHostCreated -> true)
  229. //return Host.
  230. throw new NotImplementedException ();
  231. }
  232. public override object InitializeLifetimeService ()
  233. {
  234. return null;
  235. }
  236. [MonoTODO]
  237. public bool IsCodeAssembly (string assemblyName)
  238. {
  239. // Trying all the assemblies loaded by FullName and GetName().Name yield false here :-?
  240. throw new NotImplementedException ();
  241. }
  242. [MonoTODO]
  243. public void PrecompileApplication ()
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. [MonoTODO]
  248. public void PrecompileApplication (ClientBuildManagerCallback callback)
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. [MonoTODO]
  253. public void PrecompileApplication (ClientBuildManagerCallback callback, bool forceCleanBuild)
  254. {
  255. throw new NotImplementedException ();
  256. }
  257. public bool Unload ()
  258. {
  259. if (host != null) {
  260. host.Shutdown ();
  261. OnAppDomainShutdown (0);
  262. host = null;
  263. }
  264. return true; // FIXME: may be we should do this synch. + timeout? Test!
  265. }
  266. public string CodeGenDir {
  267. get { return Host.GetCodeGenDir (); }
  268. }
  269. public bool IsHostCreated {
  270. get { return host != null; }
  271. }
  272. void IDisposable.Dispose ()
  273. {
  274. Unload ();
  275. }
  276. }
  277. }
  278. #endif