PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Microsoft.Build/Microsoft.Build/Microsoft/Build/Evaluation/Evaluator!4.cs

#
C# | 1192 lines | 1152 code | 40 blank | 0 comment | 203 complexity | fe8675671206865b6c4e11eb12d33ea8 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. namespace Microsoft.Build.Evaluation
  2. {
  3. using Microsoft.Build.BackEnd;
  4. using Microsoft.Build.BackEnd.Logging;
  5. using Microsoft.Build.Collections;
  6. using Microsoft.Build.Construction;
  7. using Microsoft.Build.Debugging;
  8. using Microsoft.Build.Exceptions;
  9. using Microsoft.Build.Execution;
  10. using Microsoft.Build.Framework;
  11. using Microsoft.Build.Internal;
  12. using Microsoft.Build.Shared;
  13. using Microsoft.Internal.Performance;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Globalization;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Runtime;
  21. using System.Runtime.CompilerServices;
  22. internal class Evaluator<P, I, M, D> where P: class, IProperty, IEquatable<P>, IValued where I: class, IItem<M>, IMetadataTable where M: class, IMetadatum where D: class, IItemDefinition<M>
  23. {
  24. private BuildEventContext buildEventContext;
  25. private readonly IEvaluatorData<P, I, M, D> data;
  26. private static readonly bool debugEvaluation;
  27. private readonly PropertyDictionary<ProjectPropertyInstance> environmentProperties;
  28. private readonly Expander<P, I> expander;
  29. private IDictionary<ProjectRootElement, ProjectRootElement> importRelationships;
  30. private readonly Dictionary<string, ProjectImportElement> importsSeen;
  31. private IDictionary<string, object> initialLocals;
  32. private static IList<DebuggerLocalType> initialLocalsTypes;
  33. private readonly List<string> initialTargetsList;
  34. private readonly IList<ProjectItemDefinitionGroupElement> itemDefinitionGroupElements;
  35. private IDictionary<string, object> itemDefinitionPassLocals;
  36. private static IList<DebuggerLocalType> itemDefinitionPassLocalsTypes;
  37. private readonly IItemFactory<I, I> itemFactory;
  38. private readonly IList<ProjectItemGroupElement> itemGroupElements;
  39. private IDictionary<string, object> itemPassLocals;
  40. private static IList<DebuggerLocalType> itemPassLocalsTypes;
  41. private readonly ProjectLoadSettings loadSettings;
  42. private readonly ILoggingService loggingService;
  43. private readonly int maxNodeCount;
  44. private readonly ProjectInstance projectInstanceIfAnyForDebuggerOnly;
  45. private IDictionary<string, object> projectLevelLocalsForBuild;
  46. private readonly ProjectRootElement projectRootElement;
  47. private readonly ProjectRootElementCache projectRootElementCache;
  48. private readonly Dictionary<ProjectRootElement, NGen<bool>> projectSupportsReturnsAttribute;
  49. private IDictionary<string, object> propertyPassLocals;
  50. private static IList<DebuggerLocalType> propertyPassLocalsTypes;
  51. private static readonly char[] splitter;
  52. private readonly IList<ProjectTargetElement> targetElements;
  53. private readonly IList<Pair<string, ProjectUsingTaskElement>> usingTaskElements;
  54. static Evaluator()
  55. {
  56. Evaluator<P, I, M, D>.splitter = new char[] { ';' };
  57. Evaluator<P, I, M, D>.debugEvaluation = Environment.GetEnvironmentVariable("MSBUILDDEBUGEVALUATION") != null;
  58. }
  59. private Evaluator(IEvaluatorData<P, I, M, D> data, ProjectRootElement projectRootElement, ProjectLoadSettings loadSettings, int maxNodeCount, PropertyDictionary<ProjectPropertyInstance> environmentProperties, ILoggingService loggingService, IItemFactory<I, I> itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCache projectRootElementCache, BuildEventContext buildEventContext, ProjectInstance projectInstanceIfAnyForDebuggerOnly)
  60. {
  61. ErrorUtilities.VerifyThrowInternalNull(data, "data");
  62. ErrorUtilities.VerifyThrowInternalNull(projectRootElementCache, "projectRootElementCache");
  63. data.InitializeForEvaluation(toolsetProvider);
  64. this.expander = new Expander<P, I>(data, data);
  65. this.expander.WarnForUninitializedProperties = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDWARNONUNINITIALIZEDPROPERTY"));
  66. this.data = data;
  67. this.itemGroupElements = new List<ProjectItemGroupElement>();
  68. this.itemDefinitionGroupElements = new List<ProjectItemDefinitionGroupElement>();
  69. this.usingTaskElements = new List<Pair<string, ProjectUsingTaskElement>>();
  70. this.targetElements = new List<ProjectTargetElement>();
  71. this.importsSeen = new Dictionary<string, ProjectImportElement>(StringComparer.OrdinalIgnoreCase);
  72. this.initialTargetsList = new List<string>();
  73. this.projectSupportsReturnsAttribute = new Dictionary<ProjectRootElement, NGen<bool>>();
  74. this.projectRootElement = projectRootElement;
  75. this.loadSettings = loadSettings;
  76. this.maxNodeCount = maxNodeCount;
  77. this.environmentProperties = environmentProperties;
  78. this.loggingService = loggingService;
  79. this.itemFactory = itemFactory;
  80. this.projectRootElementCache = projectRootElementCache;
  81. this.buildEventContext = buildEventContext;
  82. this.projectInstanceIfAnyForDebuggerOnly = projectInstanceIfAnyForDebuggerOnly;
  83. }
  84. private void AddBeforeAndAfterTargetMappings(ProjectTargetElement targetElement, Dictionary<string, LinkedListNode<ProjectTargetElement>> activeTargets, Dictionary<string, List<TargetSpecification>> targetsWhichRunBeforeByTarget, Dictionary<string, List<TargetSpecification>> targetsWhichRunAfterByTarget)
  85. {
  86. IList<string> list = this.expander.ExpandIntoStringListLeaveEscaped(targetElement.BeforeTargets, ExpanderOptions.ExpandPropertiesAndItems, targetElement.BeforeTargetsLocation);
  87. IList<string> list2 = this.expander.ExpandIntoStringListLeaveEscaped(targetElement.AfterTargets, ExpanderOptions.ExpandPropertiesAndItems, targetElement.AfterTargetsLocation);
  88. foreach (string str in list)
  89. {
  90. string key = EscapingUtilities.UnescapeAll(str);
  91. if (activeTargets.ContainsKey(key))
  92. {
  93. List<TargetSpecification> list3 = null;
  94. if (!targetsWhichRunBeforeByTarget.TryGetValue(key, out list3))
  95. {
  96. list3 = new List<TargetSpecification>();
  97. targetsWhichRunBeforeByTarget[key] = list3;
  98. }
  99. list3.Add(new TargetSpecification(targetElement.Name, targetElement.BeforeTargetsLocation));
  100. continue;
  101. }
  102. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(targetElement.BeforeTargetsLocation), "TargetDoesNotExist", new object[] { key });
  103. }
  104. foreach (string str3 in list2)
  105. {
  106. string str4 = EscapingUtilities.UnescapeAll(str3);
  107. if (activeTargets.ContainsKey(str4))
  108. {
  109. List<TargetSpecification> list4 = null;
  110. if (!targetsWhichRunAfterByTarget.TryGetValue(str4, out list4))
  111. {
  112. list4 = new List<TargetSpecification>();
  113. targetsWhichRunAfterByTarget[str4] = list4;
  114. }
  115. list4.Add(new TargetSpecification(targetElement.Name, targetElement.AfterTargetsLocation));
  116. continue;
  117. }
  118. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(targetElement.AfterTargetsLocation), "TargetDoesNotExist", new object[] { str4 });
  119. }
  120. }
  121. private ICollection<P> AddBuiltInProperties()
  122. {
  123. string startupDirectory = BuildParameters.StartupDirectory;
  124. List<P> list = new List<P>(12) {
  125. this.SetBuiltInProperty("MSBuildToolsVersion", this.data.Toolset.ToolsVersion),
  126. this.SetBuiltInProperty("MSBuildToolsPath", this.data.Toolset.ToolsPath),
  127. this.SetBuiltInProperty("MSBuildBinPath", this.data.Toolset.ToolsPath),
  128. this.SetBuiltInProperty("MSBuildStartupDirectory", startupDirectory),
  129. this.SetBuiltInProperty("MSBuildNodeCount", this.maxNodeCount.ToString(CultureInfo.CurrentCulture)),
  130. this.SetBuiltInProperty("MSBuildProgramFiles32", FrameworkLocationHelper.programFiles32)
  131. };
  132. if (string.IsNullOrEmpty(this.projectRootElement.FullPath))
  133. {
  134. if (string.IsNullOrEmpty(this.projectRootElement.DirectoryPath))
  135. {
  136. list.Add(this.SetBuiltInProperty("MSBuildProjectDirectory", startupDirectory));
  137. return list;
  138. }
  139. list.Add(this.SetBuiltInProperty("MSBuildProjectDirectory", this.projectRootElement.DirectoryPath));
  140. return list;
  141. }
  142. string evaluatedValueEscaped = EscapingUtilities.Escape(Path.GetFileName(this.projectRootElement.FullPath));
  143. string str3 = EscapingUtilities.Escape(Path.GetFileNameWithoutExtension(this.projectRootElement.FullPath));
  144. string str4 = EscapingUtilities.Escape(Path.GetExtension(this.projectRootElement.FullPath));
  145. string str5 = EscapingUtilities.Escape(this.projectRootElement.FullPath);
  146. string path = EscapingUtilities.Escape(this.projectRootElement.DirectoryPath);
  147. int length = Path.GetPathRoot(path).Length;
  148. string str7 = EscapingUtilities.Escape(FileUtilities.EnsureNoLeadingSlash(FileUtilities.EnsureNoTrailingSlash(path.Substring(length))));
  149. list.Add(this.SetBuiltInProperty("MSBuildProjectFile", evaluatedValueEscaped));
  150. list.Add(this.SetBuiltInProperty("MSBuildProjectName", str3));
  151. list.Add(this.SetBuiltInProperty("MSBuildProjectExtension", str4));
  152. list.Add(this.SetBuiltInProperty("MSBuildProjectFullPath", str5));
  153. list.Add(this.SetBuiltInProperty("MSBuildProjectDirectory", path));
  154. list.Add(this.SetBuiltInProperty("MSBuildProjectDirectoryNoRoot", str7));
  155. return list;
  156. }
  157. private ICollection<P> AddEnvironmentProperties()
  158. {
  159. List<P> list = new List<P>(this.environmentProperties.Count);
  160. foreach (ProjectPropertyInstance instance in this.environmentProperties)
  161. {
  162. P item = this.data.SetProperty(instance.Name, ((IProperty) instance).EvaluatedValueEscaped, false, false);
  163. list.Add(item);
  164. }
  165. return list;
  166. }
  167. private ICollection<P> AddGlobalProperties()
  168. {
  169. if (this.data.GlobalPropertiesDictionary == null)
  170. {
  171. return ReadOnlyEmptyList<P>.Instance;
  172. }
  173. List<P> list = new List<P>(this.data.GlobalPropertiesDictionary.Count);
  174. foreach (ProjectPropertyInstance instance in this.data.GlobalPropertiesDictionary)
  175. {
  176. P item = this.data.SetProperty(instance.Name, ((IProperty) instance).EvaluatedValueEscaped, true, false);
  177. list.Add(item);
  178. }
  179. return list;
  180. }
  181. private ICollection<P> AddToolsetProperties()
  182. {
  183. List<P> list = new List<P>(this.data.Toolset.Properties.Count);
  184. foreach (ProjectPropertyInstance instance in this.data.Toolset.Properties.Values)
  185. {
  186. P item = this.data.SetProperty(instance.Name, ((IProperty) instance).EvaluatedValueEscaped, false, false);
  187. list.Add(item);
  188. }
  189. return list;
  190. }
  191. internal static List<I> CreateItemsFromInclude(string rootDirectory, ProjectItemElement itemElement, IItemFactory<I, I> itemFactory, string unevaluatedIncludeEscaped, Expander<P, I> expander)
  192. {
  193. ErrorUtilities.VerifyThrowArgumentLength(unevaluatedIncludeEscaped, "unevaluatedIncludeEscaped");
  194. List<I> list = new List<I>();
  195. itemFactory.ItemElement = itemElement;
  196. string expression = expander.ExpandIntoStringLeaveEscaped(unevaluatedIncludeEscaped, ExpanderOptions.ExpandProperties, itemElement.IncludeLocation);
  197. if (expression.Length > 0)
  198. {
  199. foreach (string str2 in ExpressionShredder.SplitSemiColonSeparatedList(expression))
  200. {
  201. bool flag;
  202. IList<I> list3 = expander.ExpandSingleItemVectorExpressionIntoItems<I>(str2, itemFactory, ExpanderOptions.ExpandItems, false, out flag, itemElement.IncludeLocation);
  203. if (list3 != null)
  204. {
  205. foreach (I local in list3)
  206. {
  207. list.Add(local);
  208. }
  209. continue;
  210. }
  211. string[] fileListEscaped = EngineFileUtilities.GetFileListEscaped(rootDirectory, str2);
  212. if (fileListEscaped.Length > 0)
  213. {
  214. foreach (string str3 in fileListEscaped)
  215. {
  216. list.Add(itemFactory.CreateItem(str3, str2));
  217. }
  218. }
  219. }
  220. }
  221. return list;
  222. }
  223. private IDictionary<string, object> Evaluate()
  224. {
  225. Evaluator<P, I, M, D>.InitializeForDebugging();
  226. ICollection<P> is2 = this.AddBuiltInProperties();
  227. ICollection<P> is3 = this.AddEnvironmentProperties();
  228. ICollection<P> is4 = this.AddToolsetProperties();
  229. ICollection<P> is5 = this.AddGlobalProperties();
  230. if (DebuggerManager.DebuggingEnabled)
  231. {
  232. this.initialLocals = new Dictionary<string, object>();
  233. DebuggerLocalType type = Evaluator<P, I, M, D>.initialLocalsTypes[0];
  234. this.initialLocals.Add(new KeyValuePair<string, object>(type.Name, this.projectInstanceIfAnyForDebuggerOnly));
  235. DebuggerLocalType type2 = Evaluator<P, I, M, D>.initialLocalsTypes[1];
  236. this.initialLocals.Add(new KeyValuePair<string, object>(type2.Name, is2));
  237. DebuggerLocalType type3 = Evaluator<P, I, M, D>.initialLocalsTypes[2];
  238. this.initialLocals.Add(new KeyValuePair<string, object>(type3.Name, is3));
  239. DebuggerLocalType type4 = Evaluator<P, I, M, D>.initialLocalsTypes[3];
  240. this.initialLocals.Add(new KeyValuePair<string, object>(type4.Name, is4));
  241. DebuggerLocalType type5 = Evaluator<P, I, M, D>.initialLocalsTypes[4];
  242. this.initialLocals.Add(new KeyValuePair<string, object>(type5.Name, is5));
  243. DebuggerManager.DefineState(this.projectRootElement.Location, this.projectRootElement.ElementName, Evaluator<P, I, M, D>.initialLocalsTypes);
  244. DebuggerManager.BakeStates(Path.GetFileNameWithoutExtension(this.projectRootElement.FullPath));
  245. DebuggerManager.PulseState(this.projectRootElement.Location, this.initialLocals);
  246. this.propertyPassLocals = new Dictionary<string, object>(this.initialLocals);
  247. DebuggerLocalType type6 = Evaluator<P, I, M, D>.propertyPassLocalsTypes[5];
  248. this.propertyPassLocals.Add(new KeyValuePair<string, object>(type6.Name, new ExpandExpression<P, I, M, D>(this.data.ExpandString)));
  249. DebuggerLocalType type7 = Evaluator<P, I, M, D>.propertyPassLocalsTypes[6];
  250. this.propertyPassLocals.Add(new KeyValuePair<string, object>(type7.Name, new EvaluateConditionalExpression<P, I, M, D>(this.data.EvaluateCondition)));
  251. DebuggerLocalType type8 = Evaluator<P, I, M, D>.propertyPassLocalsTypes[7];
  252. this.propertyPassLocals.Add(new KeyValuePair<string, object>(type8.Name, this.data.Toolset.ToolsVersion));
  253. DebuggerLocalType type9 = Evaluator<P, I, M, D>.propertyPassLocalsTypes[8];
  254. this.propertyPassLocals.Add(new KeyValuePair<string, object>(type9.Name, this.data.Properties));
  255. this.itemDefinitionPassLocals = new Dictionary<string, object>(this.propertyPassLocals);
  256. DebuggerLocalType type10 = Evaluator<P, I, M, D>.itemDefinitionPassLocalsTypes[9];
  257. this.itemDefinitionPassLocals.Add(new KeyValuePair<string, object>(type10.Name, this.data.ItemDefinitionsEnumerable));
  258. this.itemPassLocals = new Dictionary<string, object>(this.itemDefinitionPassLocals);
  259. DebuggerLocalType type11 = Evaluator<P, I, M, D>.itemPassLocalsTypes[10];
  260. this.itemPassLocals.Add(new KeyValuePair<string, object>(type11.Name, this.data.Items));
  261. this.importRelationships = new Dictionary<ProjectRootElement, ProjectRootElement>();
  262. this.projectLevelLocalsForBuild = this.itemPassLocals;
  263. }
  264. Microsoft.Internal.Performance.CodeMarkers.Instance.CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluatePass0End);
  265. if (!string.IsNullOrEmpty(this.projectRootElement.ProjectFileLocation.File))
  266. {
  267. string file = this.projectRootElement.ProjectFileLocation.File;
  268. }
  269. this.PerformDepthFirstPass(this.projectRootElement);
  270. List<string> list = new List<string>(this.initialTargetsList.Count);
  271. for (int i = 0; i < this.initialTargetsList.Count; i++)
  272. {
  273. list.Add(EscapingUtilities.UnescapeAll(this.initialTargetsList[i].Trim()));
  274. }
  275. this.data.InitialTargets = list;
  276. Microsoft.Internal.Performance.CodeMarkers.Instance.CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluatePass1End);
  277. foreach (ProjectItemDefinitionGroupElement element in this.itemDefinitionGroupElements)
  278. {
  279. this.EvaluateItemDefinitionGroupElement(element);
  280. }
  281. Microsoft.Internal.Performance.CodeMarkers.Instance.CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluatePass2End);
  282. foreach (ProjectItemGroupElement element2 in this.itemGroupElements)
  283. {
  284. this.EvaluateItemGroupElement(element2);
  285. }
  286. Microsoft.Internal.Performance.CodeMarkers.Instance.CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluatePass3End);
  287. foreach (Pair<string, ProjectUsingTaskElement> pair in this.usingTaskElements)
  288. {
  289. this.EvaluateUsingTaskElement(pair.Key, pair.Value);
  290. }
  291. if ((this.data.DefaultTargets == null) || (this.data.DefaultTargets.Count == 0))
  292. {
  293. List<string> list2 = new List<string>(this.targetElements.Count);
  294. if (this.targetElements.Count > 0)
  295. {
  296. list2.Add(this.targetElements[0].Name);
  297. }
  298. this.data.DefaultTargets = list2;
  299. }
  300. Dictionary<string, List<TargetSpecification>> targetsWhichRunBeforeByTarget = new Dictionary<string, List<TargetSpecification>>(StringComparer.OrdinalIgnoreCase);
  301. Dictionary<string, List<TargetSpecification>> targetsWhichRunAfterByTarget = new Dictionary<string, List<TargetSpecification>>(StringComparer.OrdinalIgnoreCase);
  302. LinkedList<ProjectTargetElement> activeTargetsByEvaluationOrder = new LinkedList<ProjectTargetElement>();
  303. Dictionary<string, LinkedListNode<ProjectTargetElement>> activeTargets = new Dictionary<string, LinkedListNode<ProjectTargetElement>>(StringComparer.OrdinalIgnoreCase);
  304. Microsoft.Internal.Performance.CodeMarkers.Instance.CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluatePass4End);
  305. foreach (ProjectTargetElement element3 in this.targetElements)
  306. {
  307. this.ReadTargetElement(element3, activeTargetsByEvaluationOrder, activeTargets);
  308. }
  309. foreach (ProjectTargetElement element4 in activeTargetsByEvaluationOrder)
  310. {
  311. this.AddBeforeAndAfterTargetMappings(element4, activeTargets, targetsWhichRunBeforeByTarget, targetsWhichRunAfterByTarget);
  312. }
  313. this.data.BeforeTargets = targetsWhichRunBeforeByTarget;
  314. this.data.AfterTargets = targetsWhichRunAfterByTarget;
  315. if (Evaluator<P, I, M, D>.debugEvaluation && (this.projectRootElement.Count > 0))
  316. {
  317. ProjectPropertyInstance instance = this.data.GlobalPropertiesDictionary["currentsolutionconfigurationcontents"];
  318. if (instance != null)
  319. {
  320. instance.EvaluatedValue.GetHashCode();
  321. }
  322. }
  323. return this.projectLevelLocalsForBuild;
  324. }
  325. internal static IDictionary<string, object> Evaluate(IEvaluatorData<P, I, M, D> data, ProjectRootElement root, ProjectLoadSettings loadSettings, int maxNodeCount, PropertyDictionary<ProjectPropertyInstance> environmentProperties, ILoggingService loggingService, IItemFactory<I, I> itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCache projectRootElementCache, BuildEventContext buildEventContext, ProjectInstance projectInstanceIfAnyForDebuggerOnly)
  326. {
  327. using (new Microsoft.Internal.Performance.CodeMarkerStartEnd(Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluateBegin, Microsoft.Internal.Performance.CodeMarkerEvent.perfMSBuildProjectEvaluateEnd))
  328. {
  329. Evaluator<P, I, M, D> evaluator = new Evaluator<P, I, M, D>(data, root, loadSettings, maxNodeCount, environmentProperties, loggingService, itemFactory, toolsetProvider, projectRootElementCache, buildEventContext, projectInstanceIfAnyForDebuggerOnly);
  330. return evaluator.Evaluate();
  331. }
  332. }
  333. private void EvaluateChooseElement(ProjectChooseElement chooseElement)
  334. {
  335. foreach (ProjectWhenElement element in chooseElement.WhenElements)
  336. {
  337. if (DebuggerManager.DebuggingEnabled)
  338. {
  339. DebuggerManager.PulseState(element.Location, this.propertyPassLocals);
  340. }
  341. if (this.EvaluateConditionCollectingConditionedProperties(element, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  342. {
  343. this.EvaluateWhenOrOtherwiseChildren(element.Children);
  344. return;
  345. }
  346. }
  347. if (chooseElement.OtherwiseElement != null)
  348. {
  349. if (DebuggerManager.DebuggingEnabled)
  350. {
  351. DebuggerManager.PulseState(chooseElement.OtherwiseElement.Location, this.propertyPassLocals);
  352. }
  353. this.EvaluateWhenOrOtherwiseChildren(chooseElement.OtherwiseElement.Children);
  354. }
  355. }
  356. private bool EvaluateCondition(ProjectElement element, ExpanderOptions expanderOptions, ParserOptions parserOptions)
  357. {
  358. string directoryPath;
  359. if (element.Condition.Length == 0)
  360. {
  361. return true;
  362. }
  363. if (((element is ProjectPropertyGroupElement) || (element is ProjectImportElement)) || (element is ProjectImportGroupElement))
  364. {
  365. directoryPath = element.ContainingProject.DirectoryPath;
  366. }
  367. else
  368. {
  369. directoryPath = this.data.Directory;
  370. }
  371. return ConditionEvaluator.EvaluateCondition<P, I>(element.Condition, parserOptions, this.expander, expanderOptions, directoryPath, element.ConditionLocation, this.loggingService, this.buildEventContext);
  372. }
  373. private bool EvaluateConditionCollectingConditionedProperties(ProjectElement element, ExpanderOptions expanderOptions, ParserOptions parserOptions)
  374. {
  375. if (element.Condition.Length == 0)
  376. {
  377. return true;
  378. }
  379. if (!this.data.ShouldEvaluateForDesignTime)
  380. {
  381. return this.EvaluateCondition(element, expanderOptions, parserOptions);
  382. }
  383. return ConditionEvaluator.EvaluateConditionCollectingConditionedProperties<P, I>(element.Condition, parserOptions, this.expander, expanderOptions, this.data.ConditionedProperties, element.ContainingProject.DirectoryPath, element.ConditionLocation, this.loggingService, this.buildEventContext);
  384. }
  385. private void EvaluateImportElement(string directoryOfImportingFile, ProjectImportElement importElement)
  386. {
  387. if (DebuggerManager.DebuggingEnabled)
  388. {
  389. DebuggerManager.EnterState(importElement.Location, this.propertyPassLocals);
  390. }
  391. if (this.EvaluateConditionCollectingConditionedProperties(importElement, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  392. {
  393. string importExpressionEscaped = this.expander.ExpandIntoStringLeaveEscaped(importElement.Project, ExpanderOptions.ExpandProperties, importElement.ProjectLocation);
  394. foreach (ProjectRootElement element in this.ExpandAndLoadImports(directoryOfImportingFile, importExpressionEscaped, importElement))
  395. {
  396. this.data.RecordImport(importElement, element, element.Version);
  397. if (DebuggerManager.DebuggingEnabled)
  398. {
  399. this.importRelationships.Add(element, importElement.ContainingProject);
  400. }
  401. this.PerformDepthFirstPass(element);
  402. }
  403. }
  404. if (DebuggerManager.DebuggingEnabled)
  405. {
  406. DebuggerManager.LeaveState(importElement.Location);
  407. }
  408. }
  409. private void EvaluateImportGroupElement(string directoryOfImportingFile, ProjectImportGroupElement importGroupElement)
  410. {
  411. if (DebuggerManager.DebuggingEnabled)
  412. {
  413. DebuggerManager.PulseState(importGroupElement.Location, this.propertyPassLocals);
  414. }
  415. if (this.EvaluateConditionCollectingConditionedProperties(importGroupElement, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  416. {
  417. foreach (ProjectImportElement element in importGroupElement.Imports)
  418. {
  419. this.EvaluateImportElement(directoryOfImportingFile, element);
  420. }
  421. }
  422. }
  423. private void EvaluateItemDefinitionElement(ProjectItemDefinitionElement itemDefinitionElement)
  424. {
  425. if (DebuggerManager.DebuggingEnabled)
  426. {
  427. DebuggerManager.PulseState(itemDefinitionElement.Location, this.itemDefinitionPassLocals);
  428. }
  429. IItemDefinition<M> itemDefinition = this.data.GetItemDefinition(itemDefinitionElement.ItemType);
  430. if (itemDefinition != null)
  431. {
  432. this.expander.Metadata = itemDefinition;
  433. }
  434. else
  435. {
  436. this.expander.Metadata = new EvaluatorMetadataTable(itemDefinitionElement.ItemType);
  437. }
  438. if (this.EvaluateCondition(itemDefinitionElement, ExpanderOptions.ExpandPropertiesAndMetadata, ParserOptions.AllowPropertiesAndCustomMetadata))
  439. {
  440. if (itemDefinition == null)
  441. {
  442. itemDefinition = this.data.AddItemDefinition(itemDefinitionElement.ItemType);
  443. this.expander.Metadata = itemDefinition;
  444. }
  445. foreach (ProjectMetadataElement element in itemDefinitionElement.Metadata)
  446. {
  447. if (DebuggerManager.DebuggingEnabled)
  448. {
  449. DebuggerManager.PulseState(element.Location, this.itemDefinitionPassLocals);
  450. }
  451. if (this.EvaluateCondition(element, ExpanderOptions.ExpandPropertiesAndMetadata, ParserOptions.AllowPropertiesAndCustomMetadata))
  452. {
  453. string evaluatedValue = this.expander.ExpandIntoStringLeaveEscaped(element.Value, ExpanderOptions.ExpandPropertiesAndCustomMetadata, itemDefinitionElement.Location);
  454. M metadata = itemDefinition.GetMetadata(element.Name);
  455. M itemDefinitionMetadatum = itemDefinition.SetMetadata(element, evaluatedValue, metadata);
  456. if (this.data.ShouldEvaluateForDesignTime)
  457. {
  458. this.data.AddToAllEvaluatedItemDefinitionMetadataList(itemDefinitionMetadatum);
  459. }
  460. }
  461. }
  462. }
  463. this.expander.Metadata = null;
  464. }
  465. private void EvaluateItemDefinitionGroupElement(ProjectItemDefinitionGroupElement itemDefinitionGroupElement)
  466. {
  467. if (DebuggerManager.DebuggingEnabled)
  468. {
  469. DebuggerManager.PulseState(itemDefinitionGroupElement.Location, this.itemDefinitionPassLocals);
  470. }
  471. if (this.EvaluateCondition(itemDefinitionGroupElement, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  472. {
  473. foreach (ProjectItemDefinitionElement element in itemDefinitionGroupElement.ItemDefinitions)
  474. {
  475. this.EvaluateItemDefinitionElement(element);
  476. }
  477. }
  478. }
  479. private void EvaluateItemElement(bool itemGroupConditionResult, ProjectItemElement itemElement)
  480. {
  481. if (DebuggerManager.DebuggingEnabled)
  482. {
  483. DebuggerManager.EnterState(itemElement.Location, this.itemPassLocals);
  484. }
  485. bool flag = this.EvaluateCondition(itemElement, ExpanderOptions.ExpandPropertiesAndItems, ParserOptions.AllowPropertiesAndItemLists);
  486. if (!flag && !this.data.ShouldEvaluateForDesignTime)
  487. {
  488. if (DebuggerManager.DebuggingEnabled)
  489. {
  490. DebuggerManager.LeaveState(itemElement.Location);
  491. }
  492. }
  493. else
  494. {
  495. IList<I> destinationItems = Evaluator<P, I, M, D>.CreateItemsFromInclude(this.projectRootElement.DirectoryPath, itemElement, this.itemFactory, itemElement.Include, this.expander);
  496. if (itemElement.Exclude.Length > 0)
  497. {
  498. string expression = this.expander.ExpandIntoStringLeaveEscaped(itemElement.Exclude, ExpanderOptions.ExpandPropertiesAndItems, itemElement.ExcludeLocation);
  499. if (expression.Length > 0)
  500. {
  501. IList<string> list2 = ExpressionShredder.SplitSemiColonSeparatedList(expression);
  502. HashSet<string> set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  503. foreach (string str2 in list2)
  504. {
  505. foreach (string str3 in EngineFileUtilities.GetFileListEscaped(this.projectRootElement.DirectoryPath, str2))
  506. {
  507. set.Add(EscapingUtilities.UnescapeAll(str3));
  508. }
  509. }
  510. List<I> list3 = new List<I>();
  511. for (int i = 0; i < destinationItems.Count; i++)
  512. {
  513. I local4 = destinationItems[i];
  514. if (!set.Contains(local4.EvaluatedInclude))
  515. {
  516. list3.Add(destinationItems[i]);
  517. }
  518. }
  519. destinationItems = list3;
  520. }
  521. }
  522. if (itemElement.HasMetadata)
  523. {
  524. List<string> expressions = new List<string>(itemElement.Count);
  525. foreach (ProjectMetadataElement element in itemElement.Metadata)
  526. {
  527. expressions.Add(element.Value);
  528. expressions.Add(element.Condition);
  529. }
  530. ItemsAndMetadataPair referencedItemNamesAndMetadata = ExpressionShredder.GetReferencedItemNamesAndMetadata(expressions);
  531. bool flag2 = false;
  532. if ((referencedItemNamesAndMetadata.Metadata != null) && (referencedItemNamesAndMetadata.Metadata.Values.Count > 0))
  533. {
  534. ItemsAndMetadataPair pair2 = ExpressionShredder.GetReferencedItemNamesAndMetadata(new List<string> { itemElement.Include });
  535. if ((pair2.Items != null) && (pair2.Items.Count > 0))
  536. {
  537. flag2 = true;
  538. }
  539. else if (referencedItemNamesAndMetadata.Metadata.Values.Count > 0)
  540. {
  541. flag2 = true;
  542. }
  543. }
  544. if (flag2)
  545. {
  546. foreach (I local in destinationItems)
  547. {
  548. this.expander.Metadata = local;
  549. foreach (ProjectMetadataElement element2 in itemElement.Metadata)
  550. {
  551. if (DebuggerManager.DebuggingEnabled)
  552. {
  553. DebuggerManager.PulseState(element2.Location, this.itemPassLocals);
  554. }
  555. if (this.EvaluateCondition(element2, ExpanderOptions.ExpandAll, ParserOptions.AllowAll))
  556. {
  557. string evaluatedValue = this.expander.ExpandIntoStringLeaveEscaped(element2.Value, ExpanderOptions.ExpandAll, element2.Location);
  558. local.SetMetadata(element2, evaluatedValue);
  559. }
  560. }
  561. }
  562. this.expander.Metadata = null;
  563. }
  564. else
  565. {
  566. EvaluatorMetadataTable table = new EvaluatorMetadataTable(itemElement.ItemType);
  567. this.expander.Metadata = table;
  568. List<Pair<ProjectMetadataElement, string>> metadata = new List<Pair<ProjectMetadataElement, string>>();
  569. foreach (ProjectMetadataElement element3 in itemElement.Metadata)
  570. {
  571. if (this.EvaluateCondition(element3, ExpanderOptions.ExpandAll, ParserOptions.AllowAll))
  572. {
  573. if (DebuggerManager.DebuggingEnabled)
  574. {
  575. DebuggerManager.PulseState(element3.Location, this.itemPassLocals);
  576. }
  577. string evaluatedValueEscaped = this.expander.ExpandIntoStringLeaveEscaped(element3.Value, ExpanderOptions.ExpandAll, element3.Location);
  578. table.SetValue(element3, evaluatedValueEscaped);
  579. metadata.Add(new Pair<ProjectMetadataElement, string>(element3, evaluatedValueEscaped));
  580. }
  581. }
  582. this.itemFactory.SetMetadata(metadata, destinationItems);
  583. this.expander.Metadata = null;
  584. }
  585. }
  586. if (flag && itemGroupConditionResult)
  587. {
  588. foreach (I local2 in destinationItems)
  589. {
  590. this.data.AddItem(local2);
  591. if (this.data.ShouldEvaluateForDesignTime)
  592. {
  593. this.data.AddToAllEvaluatedItemsList(local2);
  594. }
  595. }
  596. }
  597. if (this.data.ShouldEvaluateForDesignTime)
  598. {
  599. foreach (I local3 in destinationItems)
  600. {
  601. this.data.AddItemIgnoringCondition(local3);
  602. }
  603. }
  604. if (DebuggerManager.DebuggingEnabled)
  605. {
  606. DebuggerManager.LeaveState(itemElement.Location);
  607. }
  608. }
  609. }
  610. private void EvaluateItemGroupElement(ProjectItemGroupElement itemGroupElement)
  611. {
  612. if (DebuggerManager.DebuggingEnabled)
  613. {
  614. DebuggerManager.PulseState(itemGroupElement.Location, this.itemPassLocals);
  615. }
  616. bool itemGroupConditionResult = this.EvaluateCondition(itemGroupElement, ExpanderOptions.ExpandPropertiesAndItems, ParserOptions.AllowPropertiesAndItemLists);
  617. if (itemGroupConditionResult || this.data.ShouldEvaluateForDesignTime)
  618. {
  619. foreach (ProjectItemElement element in itemGroupElement.Items)
  620. {
  621. this.EvaluateItemElement(itemGroupConditionResult, element);
  622. }
  623. }
  624. }
  625. private void EvaluatePropertyElement(ProjectPropertyElement propertyElement)
  626. {
  627. if (DebuggerManager.DebuggingEnabled)
  628. {
  629. DebuggerManager.EnterState(propertyElement.Location, this.propertyPassLocals);
  630. }
  631. if (this.data.GlobalPropertiesDictionary.ContainsKey(propertyElement.Name))
  632. {
  633. if (DebuggerManager.DebuggingEnabled)
  634. {
  635. DebuggerManager.LeaveState(propertyElement.Location);
  636. }
  637. }
  638. else if (!this.EvaluateConditionCollectingConditionedProperties(propertyElement, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  639. {
  640. if (DebuggerManager.DebuggingEnabled)
  641. {
  642. DebuggerManager.LeaveState(propertyElement.Location);
  643. }
  644. }
  645. else
  646. {
  647. this.expander.UsedUninitializedProperties.CurrentlyEvaluatingPropertyElementName = propertyElement.Name;
  648. string evaluatedValueEscaped = this.expander.ExpandIntoStringLeaveEscaped(propertyElement.Value, ExpanderOptions.ExpandProperties, propertyElement.Location);
  649. if ((evaluatedValueEscaped.Length > 0) && this.expander.WarnForUninitializedProperties)
  650. {
  651. IElementLocation location = null;
  652. if (this.expander.UsedUninitializedProperties.Properties.TryGetValue(propertyElement.Name, out location))
  653. {
  654. this.expander.UsedUninitializedProperties.Properties.Remove(propertyElement.Name);
  655. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(propertyElement.Location), "UsedUninitializedProperty", new object[] { propertyElement.Name, location.LocationString });
  656. }
  657. }
  658. this.expander.UsedUninitializedProperties.CurrentlyEvaluatingPropertyElementName = null;
  659. P property = this.data.GetProperty(propertyElement.Name);
  660. this.data.SetProperty(propertyElement, evaluatedValueEscaped, property);
  661. if (DebuggerManager.DebuggingEnabled)
  662. {
  663. DebuggerManager.LeaveState(propertyElement.Location);
  664. }
  665. }
  666. }
  667. private void EvaluatePropertyGroupElement(ProjectPropertyGroupElement propertyGroupElement)
  668. {
  669. if (DebuggerManager.DebuggingEnabled)
  670. {
  671. DebuggerManager.PulseState(propertyGroupElement.Location, this.propertyPassLocals);
  672. }
  673. if (this.EvaluateConditionCollectingConditionedProperties(propertyGroupElement, ExpanderOptions.ExpandProperties, ParserOptions.AllowProperties))
  674. {
  675. foreach (ProjectPropertyElement element in propertyGroupElement.Properties)
  676. {
  677. this.EvaluatePropertyElement(element);
  678. }
  679. }
  680. }
  681. private void EvaluateUsingTaskElement(string directoryOfImportingFile, ProjectUsingTaskElement projectUsingTaskElement)
  682. {
  683. if (DebuggerManager.DebuggingEnabled)
  684. {
  685. DebuggerManager.PulseState(projectUsingTaskElement.Location, this.itemPassLocals);
  686. }
  687. TaskRegistry.RegisterTasksFromUsingTaskElement<P, I>(this.loggingService, this.buildEventContext, directoryOfImportingFile, projectUsingTaskElement, this.data.TaskRegistry, this.expander, ExpanderOptions.ExpandPropertiesAndItems);
  688. }
  689. private bool EvaluateWhenOrOtherwiseChildren(IEnumerable<ProjectElement> children)
  690. {
  691. foreach (ProjectElement element in children)
  692. {
  693. ProjectPropertyGroupElement propertyGroupElement = element as ProjectPropertyGroupElement;
  694. if (propertyGroupElement != null)
  695. {
  696. this.EvaluatePropertyGroupElement(propertyGroupElement);
  697. }
  698. else
  699. {
  700. ProjectItemGroupElement item = element as ProjectItemGroupElement;
  701. if (item != null)
  702. {
  703. this.itemGroupElements.Add(item);
  704. continue;
  705. }
  706. ProjectChooseElement chooseElement = element as ProjectChooseElement;
  707. if (chooseElement != null)
  708. {
  709. this.EvaluateChooseElement(chooseElement);
  710. continue;
  711. }
  712. ErrorUtilities.ThrowInternalError("Unexpected child type", new object[0]);
  713. }
  714. }
  715. return true;
  716. }
  717. private List<ProjectRootElement> ExpandAndLoadImports(string directoryOfImportingFile, string importExpressionEscaped, ProjectImportElement importElement)
  718. {
  719. IElementLocation elementLocation = importElement.Location;
  720. List<ProjectRootElement> list = new List<ProjectRootElement>();
  721. string[] fileListEscaped = null;
  722. try
  723. {
  724. if ((importExpressionEscaped.Length == 0) || (importExpressionEscaped.Trim().Length == 0))
  725. {
  726. FileUtilities.NormalizePath(EscapingUtilities.UnescapeAll(importExpressionEscaped));
  727. }
  728. fileListEscaped = EngineFileUtilities.GetFileListEscaped(directoryOfImportingFile, importExpressionEscaped);
  729. }
  730. catch (Exception exception)
  731. {
  732. if (ExceptionHandling.NotExpectedException(exception))
  733. {
  734. throw;
  735. }
  736. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidAttributeValueWithException", EscapingUtilities.UnescapeAll(importExpressionEscaped), "Project", "Import", exception.Message);
  737. }
  738. foreach (string str in fileListEscaped)
  739. {
  740. ProjectRootElementCache.OpenProjectRootElement openProjectRootElement = null;
  741. string importFileUnescaped = EscapingUtilities.UnescapeAll(str);
  742. try
  743. {
  744. if ((directoryOfImportingFile != null) && !Path.IsPathRooted(importFileUnescaped))
  745. {
  746. importFileUnescaped = Path.Combine(directoryOfImportingFile, importFileUnescaped);
  747. }
  748. importFileUnescaped = FileUtilities.NormalizePath(importFileUnescaped);
  749. }
  750. catch (Exception exception2)
  751. {
  752. if (ExceptionHandling.NotExpectedException(exception2))
  753. {
  754. throw;
  755. }
  756. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidAttributeValueWithException", importFileUnescaped, "Project", "Import", exception2.Message);
  757. }
  758. if (string.Equals(this.projectRootElement.FullPath, importFileUnescaped, StringComparison.OrdinalIgnoreCase))
  759. {
  760. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(elementLocation), "SelfImport", new object[] { importFileUnescaped });
  761. }
  762. else if ((((this.loadSettings & ProjectLoadSettings.RejectCircularImports) != ProjectLoadSettings.Default) || ((this.loadSettings & ProjectLoadSettings.RecordDuplicateButNotCircularImports) != ProjectLoadSettings.Default)) && this.IntroducesCircularity(importFileUnescaped, importElement))
  763. {
  764. string str2 = importElement.ContainingProject.FullPath ?? string.Empty;
  765. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(elementLocation), "ImportIntroducesCircularity", new object[] { importFileUnescaped, str2 });
  766. if ((this.loadSettings & ProjectLoadSettings.RejectCircularImports) != ProjectLoadSettings.Default)
  767. {
  768. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "ImportIntroducesCircularity", importFileUnescaped, str2);
  769. }
  770. }
  771. else
  772. {
  773. ProjectImportElement element;
  774. bool flag = false;
  775. if (this.importsSeen.TryGetValue(importFileUnescaped, out element))
  776. {
  777. string str3 = string.Empty;
  778. if ((element.ContainingProject != this.projectRootElement) && (importElement.ContainingProject != this.projectRootElement))
  779. {
  780. str3 = "[" + this.projectRootElement.FullPath + "]";
  781. }
  782. this.loggingService.LogWarning(this.buildEventContext, null, new BuildEventFileInfo(elementLocation), "DuplicateImport", new object[] { importFileUnescaped, element.Location.LocationString, str3 });
  783. flag = true;
  784. }
  785. try
  786. {
  787. if (openProjectRootElement == null)
  788. {
  789. openProjectRootElement = () => ProjectRootElement.OpenProjectOrSolution(importFileUnescaped, new ReadOnlyConvertingDictionary<string, ProjectPropertyInstance, string>(((Evaluator<P, I, M, D>) this).data.GlobalPropertiesDictionary, instance => ((IProperty) instance).EvaluatedValueEscaped), ((Evaluator<P, I, M, D>) this).data.ExplicitToolsVersion, ((Evaluator<P, I, M, D>) this).loggingService, ((Evaluator<P, I, M, D>) this).projectRootElementCache, ((Evaluator<P, I, M, D>) this).buildEventContext);
  790. }
  791. ProjectRootElement import = this.projectRootElementCache.Get(importFileUnescaped, openProjectRootElement);
  792. if (flag)
  793. {
  794. if ((this.loadSettings & ProjectLoadSettings.RecordDuplicateButNotCircularImports) != ProjectLoadSettings.Default)
  795. {
  796. this.data.RecordImportWithDuplicates(importElement, import, import.Version);
  797. }
  798. goto Label_0362;
  799. }
  800. list.Add(import);
  801. }
  802. catch (InvalidProjectFileException exception3)
  803. {
  804. if (ExceptionHandling.IsIoRelatedException(exception3.InnerException))
  805. {
  806. if (!File.Exists(importFileUnescaped))
  807. {
  808. if ((this.loadSettings & ProjectLoadSettings.IgnoreMissingImports) != ProjectLoadSettings.Default)
  809. {
  810. goto Label_0362;
  811. }
  812. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "ImportedProjectNotFound", importFileUnescaped);
  813. }
  814. else
  815. {
  816. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidImportedProjectFile", importFileUnescaped, exception3.InnerException.Message);

Large files files are truncated, but you can click here to view the full file