BuildManagerDirectoryBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // System.Web.Compilation.BuildManagerDirectoryBuilder
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2008-2009 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using System.Web;
  33. using System.Web.Configuration;
  34. using System.Web.Hosting;
  35. using System.Web.Util;
  36. namespace System.Web.Compilation
  37. {
  38. sealed class BuildManagerDirectoryBuilder
  39. {
  40. sealed class BuildProviderItem
  41. {
  42. public BuildProvider Provider;
  43. public int ListIndex;
  44. public int ParentIndex;
  45. public BuildProviderItem (BuildProvider bp, int listIndex, int parentIndex)
  46. {
  47. this.Provider = bp;
  48. this.ListIndex = listIndex;
  49. this.ParentIndex = parentIndex;
  50. }
  51. }
  52. readonly VirtualPath virtualPath;
  53. readonly string virtualPathDirectory;
  54. CompilationSection compilationSection;
  55. Dictionary <string, BuildProvider> buildProviders;
  56. VirtualPathProvider vpp;
  57. CompilationSection CompilationSection {
  58. get {
  59. if (compilationSection == null)
  60. compilationSection = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  61. return compilationSection;
  62. }
  63. }
  64. public BuildManagerDirectoryBuilder (VirtualPath virtualPath)
  65. {
  66. if (virtualPath == null)
  67. throw new ArgumentNullException ("virtualPath");
  68. this.vpp = HostingEnvironment.VirtualPathProvider;
  69. this.virtualPath = virtualPath;
  70. this.virtualPathDirectory = VirtualPathUtility.GetDirectory (virtualPath.Absolute);
  71. }
  72. public List <BuildProviderGroup> Build (bool single)
  73. {
  74. if (StrUtils.StartsWith (virtualPath.AppRelative, "~/App_Themes/")) {
  75. var themebp = new ThemeDirectoryBuildProvider ();
  76. themebp.SetVirtualPath (virtualPath);
  77. return GetSingleBuildProviderGroup (themebp);
  78. }
  79. CompilationSection section = CompilationSection;
  80. BuildProviderCollection bpcoll = section != null ? section.BuildProviders : null;
  81. if (bpcoll == null || bpcoll.Count == 0)
  82. return null;
  83. if (virtualPath.IsFake) {
  84. BuildProvider bp = GetBuildProvider (virtualPath, bpcoll);
  85. if (bp == null)
  86. return null;
  87. return GetSingleBuildProviderGroup (bp);
  88. }
  89. if (single) {
  90. AddVirtualFile (GetVirtualFile (virtualPath.Absolute), bpcoll);
  91. } else {
  92. var cache = new Dictionary <string, bool> (RuntimeHelpers.StringEqualityComparer);
  93. AddVirtualDir (GetVirtualDirectory (virtualPath.Absolute), bpcoll, cache);
  94. cache = null;
  95. if (buildProviders == null || buildProviders.Count == 0)
  96. AddVirtualFile (GetVirtualFile (virtualPath.Absolute), bpcoll);
  97. }
  98. if (buildProviders == null || buildProviders.Count == 0)
  99. return null;
  100. var buildProviderGroups = new List <BuildProviderGroup> ();
  101. foreach (BuildProvider bp in buildProviders.Values)
  102. AssignToGroup (bp, buildProviderGroups);
  103. if (buildProviderGroups == null || buildProviderGroups.Count == 0) {
  104. buildProviderGroups = null;
  105. return null;
  106. }
  107. // We need to reverse the order, so that the build happens from the least
  108. // dependant assemblies to the most dependant ones, more or less.
  109. buildProviderGroups.Reverse ();
  110. return buildProviderGroups;
  111. }
  112. bool AddBuildProvider (BuildProvider buildProvider)
  113. {
  114. if (buildProviders == null)
  115. buildProviders = new Dictionary <string, BuildProvider> (RuntimeHelpers.StringEqualityComparer);
  116. string bpPath = buildProvider.VirtualPath;
  117. if (buildProviders.ContainsKey (bpPath))
  118. return false;
  119. buildProviders.Add (bpPath, buildProvider);
  120. return true;
  121. }
  122. void AddVirtualDir (VirtualDirectory vdir, BuildProviderCollection bpcoll, Dictionary <string, bool> cache)
  123. {
  124. if (vdir == null)
  125. return;
  126. BuildProvider bp;
  127. IDictionary <string, bool> deps;
  128. var dirs = new List <string> ();
  129. string fileVirtualPath;
  130. foreach (VirtualFile file in vdir.Files) {
  131. fileVirtualPath = file.VirtualPath;
  132. if (BuildManager.IgnoreVirtualPath (fileVirtualPath))
  133. continue;
  134. bp = GetBuildProvider (fileVirtualPath, bpcoll);
  135. if (bp == null)
  136. continue;
  137. if (!AddBuildProvider (bp))
  138. continue;
  139. deps = bp.ExtractDependencies ();
  140. if (deps == null)
  141. continue;
  142. string depDir, s;
  143. dirs.Clear ();
  144. foreach (var dep in deps) {
  145. s = dep.Key;
  146. depDir = VirtualPathUtility.GetDirectory (s); // dependencies are assumed to contain absolute paths
  147. if (cache.ContainsKey (depDir))
  148. continue;
  149. cache.Add (depDir, true);
  150. AddVirtualDir (GetVirtualDirectory (s), bpcoll, cache);
  151. }
  152. }
  153. }
  154. void AddVirtualFile (VirtualFile file, BuildProviderCollection bpcoll)
  155. {
  156. if (file == null || BuildManager.IgnoreVirtualPath (file.VirtualPath))
  157. return;
  158. BuildProvider bp = GetBuildProvider (file.VirtualPath, bpcoll);
  159. if (bp == null)
  160. return;
  161. AddBuildProvider (bp);
  162. }
  163. List <BuildProviderGroup> GetSingleBuildProviderGroup (BuildProvider bp)
  164. {
  165. var ret = new List <BuildProviderGroup> ();
  166. var group = new BuildProviderGroup ();
  167. group.AddProvider (bp);
  168. ret.Add (group);
  169. return ret;
  170. }
  171. VirtualDirectory GetVirtualDirectory (string virtualPath)
  172. {
  173. if (!vpp.DirectoryExists (VirtualPathUtility.GetDirectory (virtualPath)))
  174. return null;
  175. return vpp.GetDirectory (virtualPath);
  176. }
  177. VirtualFile GetVirtualFile (string virtualPath)
  178. {
  179. if (!vpp.FileExists (virtualPath))
  180. return null;
  181. return vpp.GetFile (virtualPath);
  182. }
  183. Type GetBuildProviderCodeDomType (BuildProvider bp)
  184. {
  185. CompilerType ct = bp.CodeCompilerType;
  186. if (ct == null) {
  187. string language = bp.LanguageName;
  188. if (String.IsNullOrEmpty (language))
  189. language = CompilationSection.DefaultLanguage;
  190. ct = BuildManager.GetDefaultCompilerTypeForLanguage (language, CompilationSection, false);
  191. }
  192. Type ret = ct != null ? ct.CodeDomProviderType : null;
  193. if (ret == null)
  194. throw new HttpException ("Unable to determine code compilation language provider for virtual path '" + bp.VirtualPath + "'.");
  195. return ret;
  196. }
  197. void AssignToGroup (BuildProvider buildProvider, List <BuildProviderGroup> groups)
  198. {
  199. if (IsDependencyCycle (buildProvider))
  200. throw new HttpException ("Dependency cycles are not suppported: " + buildProvider.VirtualPath);
  201. BuildProviderGroup myGroup = null;
  202. string bpVirtualPath = buildProvider.VirtualPath;
  203. string bpPath = VirtualPathUtility.GetDirectory (bpVirtualPath);
  204. bool canAdd;
  205. if (BuildManager.HasCachedItemNoLock (buildProvider.VirtualPath))
  206. return;
  207. StringComparison stringComparison = RuntimeHelpers.StringComparison;
  208. if (buildProvider is ApplicationFileBuildProvider || buildProvider is ThemeDirectoryBuildProvider) {
  209. // global.asax and theme directory go into their own assemblies
  210. myGroup = new BuildProviderGroup ();
  211. myGroup.Standalone = true;
  212. InsertGroup (myGroup, groups);
  213. } else {
  214. Type bpCodeDomType = GetBuildProviderCodeDomType (buildProvider);
  215. foreach (BuildProviderGroup group in groups) {
  216. if (group.Standalone)
  217. continue;
  218. if (group.Count == 0) {
  219. myGroup = group;
  220. break;
  221. }
  222. canAdd = true;
  223. foreach (BuildProvider bp in group) {
  224. if (IsDependency (buildProvider, bp)) {
  225. canAdd = false;
  226. break;
  227. }
  228. // There should be one assembly per virtual dir
  229. if (String.Compare (bpPath, VirtualPathUtility.GetDirectory (bp.VirtualPath), stringComparison) != 0) {
  230. canAdd = false;
  231. break;
  232. }
  233. // Different languages go to different assemblies
  234. if (bpCodeDomType != null) {
  235. Type type = GetBuildProviderCodeDomType (bp);
  236. if (type != null) {
  237. if (type != bpCodeDomType) {
  238. canAdd = false;
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. if (!canAdd)
  245. continue;
  246. myGroup = group;
  247. break;
  248. }
  249. if (myGroup == null) {
  250. myGroup = new BuildProviderGroup ();
  251. InsertGroup (myGroup, groups);
  252. }
  253. }
  254. myGroup.AddProvider (buildProvider);
  255. if (String.Compare (bpPath, virtualPathDirectory, stringComparison) == 0)
  256. myGroup.Master = true;
  257. }
  258. void InsertGroup (BuildProviderGroup group, List <BuildProviderGroup> groups)
  259. {
  260. if (group.Application) {
  261. groups.Insert (groups.Count - 1, group);
  262. return;
  263. }
  264. int index;
  265. if (group.Standalone)
  266. index = groups.FindLastIndex (SkipApplicationGroup);
  267. else
  268. index = groups.FindLastIndex (SkipStandaloneGroups);
  269. if (index == -1)
  270. groups.Add (group);
  271. else
  272. groups.Insert (index == 0 ? 0 : index - 1, group);
  273. }
  274. static bool SkipStandaloneGroups (BuildProviderGroup group)
  275. {
  276. if (group == null)
  277. return false;
  278. return group.Standalone;
  279. }
  280. static bool SkipApplicationGroup (BuildProviderGroup group)
  281. {
  282. if (group == null)
  283. return false;
  284. return group.Application;
  285. }
  286. bool IsDependency (BuildProvider bp1, BuildProvider bp2)
  287. {
  288. IDictionary <string, bool> deps = bp1.ExtractDependencies ();
  289. if (deps == null)
  290. return false;
  291. if (deps.ContainsKey (bp2.VirtualPath))
  292. return true;
  293. BuildProvider bp;
  294. // It won't loop forever as by the time this method is called, we are sure there are no cycles
  295. foreach (var dep in deps) {
  296. if (!buildProviders.TryGetValue (dep.Key, out bp))
  297. continue;
  298. if (IsDependency (bp, bp2))
  299. return true;
  300. }
  301. return false;
  302. }
  303. bool IsDependencyCycle (BuildProvider buildProvider)
  304. {
  305. var cache = new Dictionary <BuildProvider, bool> ();
  306. cache.Add (buildProvider, true);
  307. return IsDependencyCycle (cache, buildProvider.ExtractDependencies ());
  308. }
  309. bool IsDependencyCycle (Dictionary <BuildProvider, bool> cache, IDictionary <string, bool> deps)
  310. {
  311. if (deps == null)
  312. return false;
  313. BuildProvider bp;
  314. foreach (var d in deps) {
  315. if (!buildProviders.TryGetValue (d.Key, out bp))
  316. continue;
  317. if (cache.ContainsKey (bp))
  318. return true;
  319. cache.Add (bp, true);
  320. if (IsDependencyCycle (cache, bp.ExtractDependencies ()))
  321. return true;
  322. cache.Remove (bp);
  323. }
  324. return false;
  325. }
  326. public static BuildProvider GetBuildProvider (string virtualPath, BuildProviderCollection coll)
  327. {
  328. return GetBuildProvider (new VirtualPath (virtualPath), coll);
  329. }
  330. public static BuildProvider GetBuildProvider (VirtualPath virtualPath, BuildProviderCollection coll)
  331. {
  332. if (virtualPath == null || String.IsNullOrEmpty (virtualPath.Original) || coll == null)
  333. return null;
  334. string extension = virtualPath.Extension;
  335. BuildProvider bp = coll.GetProviderInstanceForExtension (extension);
  336. if (bp == null) {
  337. if (String.Compare (extension, ".asax", StringComparison.OrdinalIgnoreCase) == 0)
  338. bp = new ApplicationFileBuildProvider ();
  339. else if (StrUtils.StartsWith (virtualPath.AppRelative, "~/App_Themes/"))
  340. bp = new ThemeDirectoryBuildProvider ();
  341. if (bp != null)
  342. bp.SetVirtualPath (virtualPath);
  343. return bp;
  344. }
  345. object[] attrs = bp.GetType ().GetCustomAttributes (typeof (BuildProviderAppliesToAttribute), true);
  346. if (attrs == null || attrs.Length == 0)
  347. return bp;
  348. BuildProviderAppliesTo appliesTo = ((BuildProviderAppliesToAttribute)attrs [0]).AppliesTo;
  349. if ((appliesTo & BuildProviderAppliesTo.Web) == 0)
  350. return null;
  351. bp.SetVirtualPath (virtualPath);
  352. return bp;
  353. }
  354. }
  355. }