PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Microsoft.Build/Microsoft.Build/Microsoft/Build/Execution/ProjectInstance.cs

#
C# | 1151 lines | 1047 code | 104 blank | 0 comment | 78 complexity | 34263f108fa06fd07b3959a9067d6120 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.Execution
  2. {
  3. using Microsoft.Build.BackEnd;
  4. using Microsoft.Build.BackEnd.Logging;
  5. using Microsoft.Build.BuildEngine;
  6. using Microsoft.Build.Collections;
  7. using Microsoft.Build.Construction;
  8. using Microsoft.Build.Evaluation;
  9. using Microsoft.Build.Exceptions;
  10. using Microsoft.Build.Framework;
  11. using Microsoft.Build.Internal;
  12. using Microsoft.Build.Shared;
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Runtime;
  20. using System.Runtime.CompilerServices;
  21. using System.Runtime.InteropServices;
  22. using System.Xml;
  23. [DebuggerDisplay("{FullPath} #Targets={TargetsCount} DefaultTargets={(DefaultTargets == null) ? System.String.Empty : System.String.Join(\";\", DefaultTargets.ToArray())} ToolsVersion={Toolset.ToolsVersion} InitialTargets={(InitialTargets == null) ? System.String.Empty : System.String.Join(\";\", InitialTargets.ToArray())} #GlobalProperties={globalProperties.Count} #Properties={properties.Count} #ItemTypes={items.ItemTypes.Count} #Items={items.Count}")]
  24. public class ProjectInstance : IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>, IPropertyProvider<ProjectPropertyInstance>, IItemProvider<ProjectItemInstance>
  25. {
  26. private Dictionary<string, ProjectTargetInstance> actualTargets;
  27. private string directory;
  28. private PropertyDictionary<ProjectPropertyInstance> environmentVariableProperties;
  29. private bool explicitToolsVersionSpecified;
  30. private PropertyDictionary<ProjectPropertyInstance> globalProperties;
  31. private HostServices hostServices;
  32. private IDictionary<string, object> initialGlobalsForDebugging;
  33. private Dictionary<string, ProjectItemDefinitionInstance> itemDefinitions;
  34. private ItemDictionary<ProjectItemInstance> items;
  35. private string originalProjectToolsVersion;
  36. private IElementLocation projectFileLocation;
  37. private PropertyDictionary<ProjectPropertyInstance> properties;
  38. private ReadOnlyDictionary<string, ProjectTargetInstance> targets;
  39. private bool treatingHigherToolsVersionsAs40;
  40. public ProjectInstance(ProjectRootElement xml) : this(xml, null, null, ProjectCollection.GlobalProjectCollection)
  41. {
  42. }
  43. private ProjectInstance(ProjectInstance that)
  44. {
  45. this.directory = that.directory;
  46. this.projectFileLocation = that.projectFileLocation;
  47. this.hostServices = that.hostServices;
  48. this.properties = new PropertyDictionary<ProjectPropertyInstance>(that.properties.Count);
  49. foreach (ProjectPropertyInstance instance in that.Properties)
  50. {
  51. this.properties.Set(instance.DeepClone());
  52. }
  53. this.items = new ItemDictionary<ProjectItemInstance>(that.items.ItemTypes.Count);
  54. foreach (ProjectItemInstance instance2 in that.Items)
  55. {
  56. this.items.Add(instance2.DeepClone());
  57. }
  58. this.globalProperties = new PropertyDictionary<ProjectPropertyInstance>(that.globalProperties.Count);
  59. foreach (ProjectPropertyInstance instance3 in that.GlobalPropertiesDictionary)
  60. {
  61. this.globalProperties.Set(instance3.DeepClone());
  62. }
  63. this.environmentVariableProperties = new PropertyDictionary<ProjectPropertyInstance>(that.environmentVariableProperties.Count);
  64. foreach (ProjectPropertyInstance instance4 in that.environmentVariableProperties)
  65. {
  66. this.environmentVariableProperties.Set(instance4.DeepClone());
  67. }
  68. this.DefaultTargets = new List<string>(that.DefaultTargets);
  69. this.InitialTargets = new List<string>(that.InitialTargets);
  70. this.Toolset = that.Toolset;
  71. this.targets = that.targets;
  72. this.itemDefinitions = that.itemDefinitions;
  73. this.ProjectRootElementCache = that.ProjectRootElementCache;
  74. }
  75. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  76. public ProjectInstance(string projectFile) : this(projectFile, (IDictionary<string, string>) null, (string) null)
  77. {
  78. }
  79. internal ProjectInstance(string projectFile, ProjectInstance projectToInheritFrom, IDictionary<string, string> globalProperties)
  80. {
  81. this.projectFileLocation = new ElementLocation(projectFile);
  82. this.globalProperties = new PropertyDictionary<ProjectPropertyInstance>(globalProperties.Count);
  83. this.Toolset = projectToInheritFrom.Toolset;
  84. this.properties = new PropertyDictionary<ProjectPropertyInstance>(projectToInheritFrom.properties);
  85. this.items = new ItemDictionary<ProjectItemInstance>();
  86. this.actualTargets = new Dictionary<string, ProjectTargetInstance>(StringComparer.OrdinalIgnoreCase);
  87. this.targets = ReadOnlyDictionary<string, ProjectTargetInstance>.CreateWrapper(this.actualTargets);
  88. this.environmentVariableProperties = projectToInheritFrom.environmentVariableProperties;
  89. this.itemDefinitions = new Dictionary<string, ProjectItemDefinitionInstance>(projectToInheritFrom.itemDefinitions, MSBuildNameIgnoreCaseComparer.Default);
  90. this.hostServices = projectToInheritFrom.hostServices;
  91. this.ProjectRootElementCache = projectToInheritFrom.ProjectRootElementCache;
  92. this.explicitToolsVersionSpecified = projectToInheritFrom.explicitToolsVersionSpecified;
  93. this.InitialTargets = new List<string>();
  94. this.DefaultTargets = new List<string>();
  95. this.DefaultTargets.Add("Build");
  96. this.TaskRegistry = projectToInheritFrom.TaskRegistry;
  97. IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance> data = this;
  98. data.AfterTargets = new Dictionary<string, List<TargetSpecification>>();
  99. data.BeforeTargets = new Dictionary<string, List<TargetSpecification>>();
  100. foreach (KeyValuePair<string, string> pair in globalProperties)
  101. {
  102. this.globalProperties[pair.Key] = new ProjectPropertyInstance(pair.Key, pair.Value);
  103. }
  104. }
  105. public ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion) : this(projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
  106. {
  107. }
  108. public ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globalProperties, string toolsVersion, ProjectCollection projectCollection)
  109. {
  110. BuildEventContext buildEventContext = new BuildEventContext(0, -1, -2, -1);
  111. this.Initialize(xml, globalProperties, toolsVersion, new BuildParameters(projectCollection), projectCollection.LoggingService, buildEventContext);
  112. }
  113. public ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, ProjectCollection projectCollection)
  114. {
  115. ErrorUtilities.VerifyThrowArgumentLength(projectFile, "projectFile");
  116. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(toolsVersion, "toolsVersion");
  117. projectFile = FileUtilities.NormalizePath(projectFile);
  118. BuildParameters buildParameters = new BuildParameters(projectCollection);
  119. BuildEventContext buildEventContext = new BuildEventContext(buildParameters.NodeId, -1, -2, -1);
  120. ProjectRootElement xml = ProjectRootElement.OpenProjectOrSolution(projectFile, globalProperties, toolsVersion, projectCollection.LoggingService, buildParameters.ProjectRootElementCache, buildEventContext);
  121. this.Initialize(xml, globalProperties, toolsVersion, buildParameters, projectCollection.LoggingService, buildEventContext);
  122. }
  123. internal ProjectInstance(Microsoft.Build.Evaluation.Project.Data data, string directory, string fullPath, HostServices hostServices, PropertyDictionary<ProjectPropertyInstance> environmentVariableProperties)
  124. {
  125. ErrorUtilities.VerifyThrowInternalNull(data, "data");
  126. ErrorUtilities.VerifyThrowInternalLength(directory, "directory");
  127. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(fullPath, "fullPath");
  128. this.directory = directory;
  129. this.projectFileLocation = new ElementLocation(fullPath);
  130. this.hostServices = hostServices;
  131. this.properties = new PropertyDictionary<ProjectPropertyInstance>(data.Properties.Count);
  132. foreach (ProjectProperty property in data.Properties)
  133. {
  134. ProjectPropertyInstance projectProperty = new ProjectPropertyInstance(property.Name, ((IProperty) property).EvaluatedValueEscaped, true);
  135. this.properties.Set(projectProperty);
  136. }
  137. this.itemDefinitions = new Dictionary<string, ProjectItemDefinitionInstance>(MSBuildNameIgnoreCaseComparer.Default);
  138. foreach (ProjectItemDefinition definition in data.ItemDefinitions.Values)
  139. {
  140. this.itemDefinitions.Add(definition.ItemType, new ProjectItemDefinitionInstance(this, definition));
  141. }
  142. this.items = new ItemDictionary<ProjectItemInstance>(data.ItemTypes.Count);
  143. foreach (ProjectItem item in data.Items)
  144. {
  145. List<ProjectItemDefinitionInstance> itemDefinitions = null;
  146. if (item.InheritedItemDefinitions != null)
  147. {
  148. itemDefinitions = new List<ProjectItemDefinitionInstance>(item.InheritedItemDefinitions.Count);
  149. foreach (ProjectItemDefinition definition2 in item.InheritedItemDefinitions)
  150. {
  151. itemDefinitions.Add(this.itemDefinitions[definition2.ItemType]);
  152. }
  153. }
  154. CopyOnWritePropertyDictionary<ProjectMetadataInstance> directMetadata = null;
  155. if (item.DirectMetadata != null)
  156. {
  157. directMetadata = new CopyOnWritePropertyDictionary<ProjectMetadataInstance>(item.DirectMetadataCount);
  158. foreach (ProjectMetadata metadata in item.DirectMetadata)
  159. {
  160. ProjectMetadataInstance instance2 = new ProjectMetadataInstance(metadata);
  161. directMetadata.Set(instance2);
  162. }
  163. }
  164. ProjectItemInstance projectItem = new ProjectItemInstance(this, item.ItemType, ((IItem) item).EvaluatedIncludeEscaped, item.EvaluatedIncludeBeforeWildcardExpansionEscaped, directMetadata, item.BuiltInMetadata, itemDefinitions);
  165. this.items.Add(projectItem);
  166. }
  167. this.globalProperties = new PropertyDictionary<ProjectPropertyInstance>(data.GlobalPropertiesDictionary.Count);
  168. foreach (ProjectPropertyInstance instance4 in data.GlobalPropertiesDictionary)
  169. {
  170. this.globalProperties.Set(instance4.DeepClone());
  171. }
  172. this.environmentVariableProperties = new PropertyDictionary<ProjectPropertyInstance>(environmentVariableProperties.Count);
  173. foreach (ProjectPropertyInstance instance5 in environmentVariableProperties)
  174. {
  175. this.environmentVariableProperties.Set(instance5.DeepClone());
  176. }
  177. this.DefaultTargets = new List<string>(data.DefaultTargets);
  178. this.InitialTargets = new List<string>(data.InitialTargets);
  179. this.BeforeTargets = ReadOnlyDictionary<string, List<TargetSpecification>>.CreateClone(data.BeforeTargets, StringComparer.OrdinalIgnoreCase);
  180. this.AfterTargets = ReadOnlyDictionary<string, List<TargetSpecification>>.CreateClone(data.AfterTargets, StringComparer.OrdinalIgnoreCase);
  181. this.targets = ReadOnlyDictionary<string, ProjectTargetInstance>.CreateClone(data.Targets, StringComparer.OrdinalIgnoreCase);
  182. this.Toolset = data.Toolset;
  183. this.TaskRegistry = data.TaskRegistry;
  184. this.ProjectRootElementCache = data.Project.ProjectCollection.ProjectRootElementCache;
  185. this.treatingHigherToolsVersionsAs40 = data.TreatingHigherToolsVersionsAs40;
  186. this.originalProjectToolsVersion = data.OriginalProjectToolsVersion;
  187. this.explicitToolsVersionSpecified = data.ExplicitToolsVersion != null;
  188. }
  189. internal ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globalProperties, string toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext)
  190. {
  191. ErrorUtilities.VerifyThrowArgumentNull(xml, "xml");
  192. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(toolsVersion, "toolsVersion");
  193. ErrorUtilities.VerifyThrowArgumentNull(buildParameters, "buildParameters");
  194. this.Initialize(xml, globalProperties, toolsVersion, buildParameters, loggingService, buildEventContext);
  195. }
  196. internal ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext)
  197. {
  198. ErrorUtilities.VerifyThrowArgumentLength(projectFile, "projectFile");
  199. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(toolsVersion, "toolsVersion");
  200. ErrorUtilities.VerifyThrowArgumentNull(buildParameters, "buildParameters");
  201. ProjectRootElement xml = ProjectRootElement.OpenProjectOrSolution(projectFile, globalProperties, toolsVersion, loggingService, buildParameters.ProjectRootElementCache, buildEventContext);
  202. this.Initialize(xml, globalProperties, toolsVersion, buildParameters, loggingService, buildEventContext);
  203. }
  204. public ProjectItemInstance AddItem(string itemType, string evaluatedInclude)
  205. {
  206. ProjectItemInstance projectItem = new ProjectItemInstance(this, itemType, evaluatedInclude);
  207. this.items.Add(projectItem);
  208. return projectItem;
  209. }
  210. public ProjectItemInstance AddItem(string itemType, string evaluatedInclude, IEnumerable<KeyValuePair<string, string>> metadata)
  211. {
  212. ProjectItemInstance projectItem = new ProjectItemInstance(this, itemType, evaluatedInclude, metadata);
  213. this.items.Add(projectItem);
  214. return projectItem;
  215. }
  216. internal ProjectTargetInstance AddTarget(string targetName, string condition, string inputs, string outputs, string returns, string keepDuplicateOutputs, string dependsOnTargets, bool parentProjectSupportsReturnsAttribute)
  217. {
  218. ErrorUtilities.VerifyThrowInternalLength(targetName, "targetName");
  219. ErrorUtilities.VerifyThrow(!this.actualTargets.ContainsKey(targetName), "Target {0} already exists.", targetName);
  220. ProjectTargetInstance instance = new ProjectTargetInstance(targetName, condition ?? string.Empty, inputs ?? string.Empty, outputs ?? string.Empty, returns, keepDuplicateOutputs ?? string.Empty, dependsOnTargets ?? string.Empty, this.projectFileLocation, string.IsNullOrEmpty(condition) ? null : ((IElementLocation) ElementLocation.EmptyLocation), string.IsNullOrEmpty(inputs) ? null : ((IElementLocation) ElementLocation.EmptyLocation), string.IsNullOrEmpty(outputs) ? null : ((IElementLocation) ElementLocation.EmptyLocation), string.IsNullOrEmpty(returns) ? null : ((IElementLocation) ElementLocation.EmptyLocation), string.IsNullOrEmpty(keepDuplicateOutputs) ? null : ((IElementLocation) ElementLocation.EmptyLocation), string.IsNullOrEmpty(dependsOnTargets) ? null : ((IElementLocation) ElementLocation.EmptyLocation), null, null, new System.Collections.ObjectModel.ReadOnlyCollection<ProjectTargetInstanceChild>(new List<ProjectTargetInstanceChild>()), new System.Collections.ObjectModel.ReadOnlyCollection<ProjectOnErrorInstance>(new List<ProjectOnErrorInstance>()), parentProjectSupportsReturnsAttribute);
  221. this.actualTargets[instance.Name] = instance;
  222. return instance;
  223. }
  224. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  225. public bool Build()
  226. {
  227. return this.Build(null);
  228. }
  229. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  230. public bool Build(IEnumerable<ILogger> loggers)
  231. {
  232. return this.Build((string[]) null, loggers, (IEnumerable<ForwardingLoggerRecord>) null);
  233. }
  234. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  235. public bool Build(IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
  236. {
  237. return this.Build((string[]) null, loggers, remoteLoggers);
  238. }
  239. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  240. public bool Build(string target, IEnumerable<ILogger> loggers)
  241. {
  242. return this.Build(target, loggers, null);
  243. }
  244. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  245. public bool Build(string[] targets, IEnumerable<ILogger> loggers)
  246. {
  247. return this.Build(targets, loggers, (IEnumerable<ForwardingLoggerRecord>) null);
  248. }
  249. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  250. public bool Build(string[] targets, IEnumerable<ILogger> loggers, out IDictionary<string, TargetResult> targetOutputs)
  251. {
  252. return this.Build(targets, loggers, null, null, out targetOutputs);
  253. }
  254. public bool Build(string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
  255. {
  256. IDictionary<string, TargetResult> dictionary;
  257. return this.Build(targets, loggers, remoteLoggers, out dictionary);
  258. }
  259. public bool Build(string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
  260. {
  261. string[] targets = (target == null) ? new string[0] : new string[] { target };
  262. return this.Build(targets, loggers, remoteLoggers);
  263. }
  264. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  265. public bool Build(string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers, out IDictionary<string, TargetResult> targetOutputs)
  266. {
  267. return this.Build(targets, loggers, remoteLoggers, null, out targetOutputs);
  268. }
  269. internal bool Build(string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers, ILoggingService loggingService, out IDictionary<string, TargetResult> targetOutputs)
  270. {
  271. if (targets == null)
  272. {
  273. targets = new string[0];
  274. }
  275. BuildManager defaultBuildManager = BuildManager.DefaultBuildManager;
  276. BuildRequestData requestData = new BuildRequestData(this, targets, this.hostServices);
  277. BuildParameters parameters = new BuildParameters();
  278. if (loggers != null)
  279. {
  280. parameters.Loggers = (loggers is ICollection<ILogger>) ? ((ICollection<ILogger>) loggers) : new List<ILogger>(loggers);
  281. }
  282. if (remoteLoggers != null)
  283. {
  284. parameters.ForwardingLoggers = (remoteLoggers is ICollection<ForwardingLoggerRecord>) ? ((ICollection<ForwardingLoggerRecord>) remoteLoggers) : new List<ForwardingLoggerRecord>(remoteLoggers);
  285. }
  286. parameters.EnvironmentPropertiesInternal = this.environmentVariableProperties;
  287. parameters.ProjectRootElementCache = this.ProjectRootElementCache;
  288. Microsoft.Build.Execution.BuildResult result = defaultBuildManager.Build(parameters, requestData);
  289. targetOutputs = result.ResultsByTarget;
  290. return (result.OverallResult == BuildResultCode.Success);
  291. }
  292. internal void Cache(INodePacketTranslator translator)
  293. {
  294. this.Translate(translator);
  295. if (translator.Mode == TranslationDirection.WriteToStream)
  296. {
  297. this.globalProperties = null;
  298. this.properties = null;
  299. this.items = null;
  300. }
  301. }
  302. public ProjectInstance DeepCopy()
  303. {
  304. return new ProjectInstance(this);
  305. }
  306. public bool EvaluateCondition(string condition)
  307. {
  308. Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(this, this);
  309. return ConditionEvaluator.EvaluateCondition<ProjectPropertyInstance, ProjectItemInstance>(condition, Microsoft.Build.Evaluation.ParserOptions.AllowPropertiesAndItemLists, expander, Microsoft.Build.Evaluation.ExpanderOptions.ExpandPropertiesAndItems, this.Directory, this.ProjectFileLocation, null, BuildEventContext.Invalid);
  310. }
  311. public string ExpandString(string unexpandedValue)
  312. {
  313. Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(this, this);
  314. return expander.ExpandIntoStringAndUnescape(unexpandedValue, Microsoft.Build.Evaluation.ExpanderOptions.ExpandPropertiesAndItems, this.ProjectFileLocation);
  315. }
  316. private static ProjectInstance[] GenerateSolutionWrapper(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, ILoggingService loggingService, BuildEventContext projectBuildEventContext)
  317. {
  318. SolutionParser solution = new SolutionParser {
  319. SolutionFile = projectFile
  320. };
  321. solution.ParseSolutionFile();
  322. if (solution.SolutionParserComments.Count > 0)
  323. {
  324. foreach (string str in solution.SolutionParserComments)
  325. {
  326. loggingService.LogCommentFromText(projectBuildEventContext, MessageImportance.Low, str);
  327. }
  328. }
  329. return SolutionProjectGenerator.Generate(solution, globalProperties, toolsVersion, projectBuildEventContext, loggingService);
  330. }
  331. [MethodImpl(MethodImplOptions.NoInlining)]
  332. private static ProjectInstance[] GenerateSolutionWrapperUsingOldOM(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, Microsoft.Build.Evaluation.ProjectRootElementCache projectRootElementCache, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext projectBuildEventContext)
  333. {
  334. string str;
  335. List<DictionaryEntry> list = null;
  336. try
  337. {
  338. foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
  339. {
  340. string key = entry.Key as string;
  341. if ((key != null) && ((!XmlUtilities.IsValidElementName(key) || (XMakeElements.IllegalItemPropertyNames[key] != null)) || Microsoft.Build.Internal.ReservedPropertyNames.IsReservedProperty(key)))
  342. {
  343. if (list == null)
  344. {
  345. list = new List<DictionaryEntry>();
  346. }
  347. Environment.SetEnvironmentVariable(key, null);
  348. list.Add(entry);
  349. }
  350. }
  351. str = SolutionWrapperProject.Generate(projectFile, toolsVersion, projectBuildEventContext);
  352. }
  353. catch (Microsoft.Build.BuildEngine.InvalidProjectFileException exception)
  354. {
  355. throw new Microsoft.Build.Exceptions.InvalidProjectFileException(exception.ProjectFile, exception.LineNumber, exception.ColumnNumber, exception.EndLineNumber, exception.EndColumnNumber, exception.Message, exception.ErrorSubcategory, exception.ErrorCode, exception.HelpKeyword, exception.InnerException);
  356. }
  357. finally
  358. {
  359. if (list != null)
  360. {
  361. foreach (DictionaryEntry entry2 in list)
  362. {
  363. Environment.SetEnvironmentVariable(entry2.Key as string, entry2.Value as string);
  364. }
  365. }
  366. }
  367. ProjectRootElement xml = new ProjectRootElement(XmlReader.Create(new StringReader(str)), projectRootElementCache) {
  368. DirectoryPath = Path.GetDirectoryName(projectFile)
  369. };
  370. ProjectInstance instance = new ProjectInstance(xml, globalProperties, toolsVersion, buildParameters, loggingService, projectBuildEventContext);
  371. return new ProjectInstance[] { instance };
  372. }
  373. public static string GetEvaluatedItemIncludeEscaped(ProjectItemDefinitionInstance item)
  374. {
  375. ErrorUtilities.VerifyThrowArgumentNull(item, "item");
  376. return ((IItem) item).EvaluatedIncludeEscaped;
  377. }
  378. public static string GetEvaluatedItemIncludeEscaped(ProjectItemInstance item)
  379. {
  380. ErrorUtilities.VerifyThrowArgumentNull(item, "item");
  381. return ((IItem) item).EvaluatedIncludeEscaped;
  382. }
  383. public ICollection<ProjectItemInstance> GetItems(string itemType)
  384. {
  385. return ((IItemProvider<ProjectItemInstance>) this).GetItems(itemType);
  386. }
  387. public static string GetMetadataValueEscaped(ProjectMetadataInstance metadatum)
  388. {
  389. ErrorUtilities.VerifyThrowArgumentNull(metadatum, "metadatum");
  390. return metadatum.EvaluatedValueEscaped;
  391. }
  392. public static string GetMetadataValueEscaped(ProjectItemDefinitionInstance item, string name)
  393. {
  394. ErrorUtilities.VerifyThrowArgumentNull(item, "item");
  395. return ((IItem) item).GetMetadataValueEscaped(name);
  396. }
  397. public static string GetMetadataValueEscaped(ProjectItemInstance item, string name)
  398. {
  399. ErrorUtilities.VerifyThrowArgumentNull(item, "item");
  400. return ((IItem) item).GetMetadataValueEscaped(name);
  401. }
  402. [DebuggerStepThrough]
  403. public ProjectPropertyInstance GetProperty(string name)
  404. {
  405. return this.properties[name];
  406. }
  407. public string GetPropertyValue(string name)
  408. {
  409. ProjectPropertyInstance instance = this.properties[name];
  410. return ((instance == null) ? string.Empty : instance.EvaluatedValue);
  411. }
  412. public static string GetPropertyValueEscaped(ProjectPropertyInstance property)
  413. {
  414. ErrorUtilities.VerifyThrowArgumentNull(property, "property");
  415. return ((IProperty) property).EvaluatedValueEscaped;
  416. }
  417. internal IList<TargetSpecification> GetTargetsWhichRunAfter(string target)
  418. {
  419. List<TargetSpecification> list;
  420. if (this.AfterTargets.TryGetValue(target, out list))
  421. {
  422. return list;
  423. }
  424. return ReadOnlyEmptyList<TargetSpecification>.Instance;
  425. }
  426. internal IList<TargetSpecification> GetTargetsWhichRunBefore(string target)
  427. {
  428. List<TargetSpecification> list;
  429. if (this.BeforeTargets.TryGetValue(target, out list))
  430. {
  431. return list;
  432. }
  433. return ReadOnlyEmptyList<TargetSpecification>.Instance;
  434. }
  435. private void Initialize(ProjectRootElement xml, IDictionary<string, string> globalProperties, string explicitToolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext)
  436. {
  437. Version version;
  438. ErrorUtilities.VerifyThrowArgumentNull(xml, "xml");
  439. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(explicitToolsVersion, "toolsVersion");
  440. ErrorUtilities.VerifyThrowArgumentNull(buildParameters, "buildParameters");
  441. this.directory = xml.DirectoryPath;
  442. this.projectFileLocation = (xml.ProjectFileLocation != null) ? xml.ProjectFileLocation : ElementLocation.EmptyLocation;
  443. this.properties = new PropertyDictionary<ProjectPropertyInstance>();
  444. this.items = new ItemDictionary<ProjectItemInstance>();
  445. this.actualTargets = new Dictionary<string, ProjectTargetInstance>(StringComparer.OrdinalIgnoreCase);
  446. this.targets = ReadOnlyDictionary<string, ProjectTargetInstance>.CreateWrapper(this.actualTargets);
  447. this.globalProperties = new PropertyDictionary<ProjectPropertyInstance>((globalProperties == null) ? 0 : globalProperties.Count);
  448. this.environmentVariableProperties = buildParameters.EnvironmentPropertiesInternal;
  449. this.itemDefinitions = new Dictionary<string, ProjectItemDefinitionInstance>(MSBuildNameIgnoreCaseComparer.Default);
  450. this.hostServices = buildParameters.HostServices;
  451. this.ProjectRootElementCache = buildParameters.ProjectRootElementCache;
  452. string toolsVersion = explicitToolsVersion;
  453. this.explicitToolsVersionSpecified = explicitToolsVersion != null;
  454. IElementLocation elementLocation = xml.Location;
  455. if (((Environment.GetEnvironmentVariable("MSBUILDTREATHIGHERTOOLSVERSIONASCURRENT") == "1") && Version.TryParse(xml.ToolsVersion, out version)) && ((version.Major >= 4) && (version.Minor > 0)))
  456. {
  457. toolsVersion = "4.0";
  458. this.treatingHigherToolsVersionsAs40 = true;
  459. this.originalProjectToolsVersion = xml.ToolsVersion;
  460. }
  461. if (toolsVersion == null)
  462. {
  463. if (xml.ToolsVersion.Length > 0)
  464. {
  465. toolsVersion = xml.ToolsVersion;
  466. elementLocation = xml.ToolsVersionLocation;
  467. this.originalProjectToolsVersion = toolsVersion;
  468. if (buildParameters.GetToolset(toolsVersion) == null)
  469. {
  470. toolsVersion = "4.0";
  471. this.treatingHigherToolsVersionsAs40 = true;
  472. }
  473. }
  474. else
  475. {
  476. toolsVersion = buildParameters.DefaultToolsVersion;
  477. }
  478. }
  479. this.Toolset = buildParameters.GetToolset(toolsVersion);
  480. if (this.Toolset == null)
  481. {
  482. string str2 = Microsoft.Build.Internal.Utilities.CreateToolsVersionListString(buildParameters.Toolsets);
  483. ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "UnrecognizedToolsVersion", toolsVersion, str2);
  484. }
  485. this.TaskRegistry = new Microsoft.Build.Execution.TaskRegistry(this.Toolset, this.ProjectRootElementCache);
  486. if (globalProperties != null)
  487. {
  488. foreach (KeyValuePair<string, string> pair in globalProperties)
  489. {
  490. this.globalProperties.Set(new ProjectPropertyInstance(pair.Key, pair.Value));
  491. }
  492. }
  493. this.initialGlobalsForDebugging = Evaluator<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.Evaluate(this, xml, Microsoft.Build.Evaluation.ProjectLoadSettings.Default, buildParameters.MaxNodeCount, buildParameters.EnvironmentPropertiesInternal, loggingService, new ProjectItemInstance.TaskItem.ProjectItemInstanceFactory(this), buildParameters.ToolsetProvider, this.ProjectRootElementCache, buildEventContext, this);
  494. }
  495. internal static ProjectInstance[] LoadSolutionForBuild(string projectFile, PropertyDictionary<ProjectPropertyInstance> globalPropertiesInstances, string toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext projectBuildEventContext)
  496. {
  497. ErrorUtilities.VerifyThrowArgumentLength(projectFile, "projectFile");
  498. ErrorUtilities.VerifyThrowArgumentNull(globalPropertiesInstances, "globalPropertiesInstances");
  499. ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(toolsVersion, "toolsVersion");
  500. ErrorUtilities.VerifyThrowArgumentNull(buildParameters, "buildParameters");
  501. ErrorUtilities.VerifyThrow(FileUtilities.IsSolutionFilename(projectFile), "Project file {0} is not a solution.", projectFile);
  502. ProjectInstance[] instanceArray = null;
  503. Dictionary<string, string> globalProperties = new Dictionary<string, string>(globalPropertiesInstances.Count, StringComparer.OrdinalIgnoreCase);
  504. foreach (ProjectPropertyInstance instance in globalPropertiesInstances)
  505. {
  506. globalProperties[instance.Name] = ((IProperty) instance).EvaluatedValueEscaped;
  507. }
  508. if (toolsVersion != null)
  509. {
  510. if ((string.Equals(toolsVersion, "2.0", StringComparison.OrdinalIgnoreCase) || string.Equals(toolsVersion, "3.0", StringComparison.OrdinalIgnoreCase)) || string.Equals(toolsVersion, "3.5", StringComparison.OrdinalIgnoreCase))
  511. {
  512. loggingService.LogComment(projectBuildEventContext, MessageImportance.Low, "OldWrapperGeneratedExplicitToolsVersion", new object[] { toolsVersion });
  513. return GenerateSolutionWrapperUsingOldOM(projectFile, globalProperties, toolsVersion, buildParameters.ProjectRootElementCache, buildParameters, loggingService, projectBuildEventContext);
  514. }
  515. return GenerateSolutionWrapper(projectFile, globalProperties, toolsVersion, loggingService, projectBuildEventContext);
  516. }
  517. if (toolsVersion != null)
  518. {
  519. return instanceArray;
  520. }
  521. int solutionFileMajorVersion = SolutionParser.GetSolutionFileMajorVersion(projectFile);
  522. if (solutionFileMajorVersion <= 9)
  523. {
  524. loggingService.LogComment(projectBuildEventContext, MessageImportance.Low, "OldWrapperGeneratedOldSolutionVersion", new object[] { "2.0", solutionFileMajorVersion });
  525. return GenerateSolutionWrapperUsingOldOM(projectFile, globalProperties, "2.0", buildParameters.ProjectRootElementCache, buildParameters, loggingService, projectBuildEventContext);
  526. }
  527. if (solutionFileMajorVersion == 10)
  528. {
  529. loggingService.LogComment(projectBuildEventContext, MessageImportance.Low, "OldWrapperGeneratedOldSolutionVersion", new object[] { "3.5", solutionFileMajorVersion });
  530. return GenerateSolutionWrapperUsingOldOM(projectFile, globalProperties, "3.5", buildParameters.ProjectRootElementCache, buildParameters, loggingService, projectBuildEventContext);
  531. }
  532. return GenerateSolutionWrapper(projectFile, globalProperties, toolsVersion, loggingService, projectBuildEventContext);
  533. }
  534. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddItem(ProjectItemInstance item)
  535. {
  536. this.items.Add(item);
  537. }
  538. IItemDefinition<ProjectMetadataInstance> IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddItemDefinition(string itemType)
  539. {
  540. ProjectItemDefinitionInstance instance = new ProjectItemDefinitionInstance(this, itemType);
  541. this.itemDefinitions.Add(itemType, instance);
  542. return instance;
  543. }
  544. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddItemIgnoringCondition(ProjectItemInstance item)
  545. {
  546. ErrorUtilities.ThrowInternalErrorUnreachable();
  547. }
  548. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddTarget(ProjectTargetInstance target)
  549. {
  550. this.actualTargets[target.Name] = target;
  551. }
  552. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddToAllEvaluatedItemDefinitionMetadataList(ProjectMetadataInstance itemDefinitionMetadatum)
  553. {
  554. ErrorUtilities.ThrowInternalErrorUnreachable();
  555. }
  556. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddToAllEvaluatedItemsList(ProjectItemInstance item)
  557. {
  558. ErrorUtilities.ThrowInternalErrorUnreachable();
  559. }
  560. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.AddToAllEvaluatedPropertiesList(ProjectPropertyInstance property)
  561. {
  562. ErrorUtilities.ThrowInternalErrorUnreachable();
  563. }
  564. IItemDefinition<ProjectMetadataInstance> IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.GetItemDefinition(string itemType)
  565. {
  566. ProjectItemDefinitionInstance instance;
  567. this.itemDefinitions.TryGetValue(itemType, out instance);
  568. return instance;
  569. }
  570. ProjectTargetInstance IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.GetTarget(string targetName)
  571. {
  572. ProjectTargetInstance instance;
  573. this.targets.TryGetValue(targetName, out instance);
  574. return instance;
  575. }
  576. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.InitializeForEvaluation(IToolsetProvider toolsetProvider)
  577. {
  578. }
  579. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.RecordImport(ProjectImportElement importElement, ProjectRootElement import, int versionEvaluated)
  580. {
  581. }
  582. void IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.RecordImportWithDuplicates(ProjectImportElement importElement, ProjectRootElement import, int versionEvaluated)
  583. {
  584. }
  585. ProjectPropertyInstance IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.SetProperty(ProjectPropertyElement propertyElement, string evaluatedValueEscaped, ProjectPropertyInstance predecessor)
  586. {
  587. ProjectPropertyInstance projectProperty = new ProjectPropertyInstance(propertyElement.Name, evaluatedValueEscaped);
  588. this.properties.Set(projectProperty);
  589. return projectProperty;
  590. }
  591. ProjectPropertyInstance IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.SetProperty(string name, string evaluatedValueEscaped, bool isGlobalProperty, bool mayBeReserved)
  592. {
  593. ProjectPropertyInstance projectProperty = new ProjectPropertyInstance(name, evaluatedValueEscaped, mayBeReserved);
  594. this.properties.Set(projectProperty);
  595. return projectProperty;
  596. }
  597. ICollection<ProjectItemInstance> IItemProvider<ProjectItemInstance>.GetItems(string itemType)
  598. {
  599. return this.items[itemType];
  600. }
  601. [DebuggerStepThrough]
  602. ProjectPropertyInstance IPropertyProvider<ProjectPropertyInstance>.GetProperty(string name, int startIndex, int endIndex)
  603. {
  604. return this.properties.GetProperty(name, startIndex, endIndex);
  605. }
  606. public bool RemoveItem(ProjectItemInstance item)
  607. {
  608. return this.items.Remove(item);
  609. }
  610. public bool RemoveProperty(string name)
  611. {
  612. return this.properties.Remove(name);
  613. }
  614. internal void RemoveTarget(string targetName)
  615. {
  616. this.actualTargets.Remove(targetName);
  617. }
  618. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  619. internal void RetrieveFromCache(INodePacketTranslator translator)
  620. {
  621. this.Translate(translator);
  622. }
  623. public ProjectPropertyInstance SetProperty(string name, string evaluatedValue)
  624. {
  625. ProjectPropertyInstance projectProperty = new ProjectPropertyInstance(name, evaluatedValue);
  626. this.properties.Set(projectProperty);
  627. return projectProperty;
  628. }
  629. public ProjectRootElement ToProjectRootElement()
  630. {
  631. ProjectRootElement rootElement = ProjectRootElement.Create();
  632. rootElement.InitialTargets = string.Join(";", this.InitialTargets.ToArray());
  633. rootElement.DefaultTargets = string.Join(";", this.DefaultTargets.ToArray());
  634. rootElement.ToolsVersion = this.ToolsVersion;
  635. ProjectItemDefinitionGroupElement parent = rootElement.AddItemDefinitionGroup();
  636. foreach (ProjectItemDefinitionInstance instance in this.itemDefinitions.Values)
  637. {
  638. instance.ToProjectItemDefinitionElement(parent);
  639. }
  640. foreach (string str in this.items.ItemTypes)
  641. {
  642. ProjectItemGroupElement element3 = rootElement.AddItemGroup();
  643. foreach (ProjectItemInstance instance2 in this.items.GetItems(str))
  644. {
  645. instance2.ToProjectItemElement(element3);
  646. }
  647. }
  648. ProjectPropertyGroupElement element4 = rootElement.AddPropertyGroup();
  649. foreach (ProjectPropertyInstance instance3 in this.properties)
  650. {
  651. if ((!Microsoft.Build.Internal.ReservedPropertyNames.IsReservedProperty(instance3.Name) && (!this.globalProperties.Contains(instance3.Name) || !string.Equals(this.globalProperties[instance3.Name].EvaluatedValue, instance3.EvaluatedValue, StringComparison.OrdinalIgnoreCase))) && (!this.environmentVariableProperties.Contains(instance3.Name) || !string.Equals(this.environmentVariableProperties[instance3.Name].EvaluatedValue, instance3.EvaluatedValue, StringComparison.OrdinalIgnoreCase)))
  652. {
  653. instance3.ToProjectPropertyElement(element4);
  654. }
  655. }
  656. foreach (ProjectTargetInstance instance4 in this.Targets.Values)
  657. {
  658. instance4.ToProjectTargetElement(rootElement);
  659. }
  660. return rootElement;
  661. }
  662. private void Translate(INodePacketTranslator translator)
  663. {
  664. NodePacketValueFactory<ProjectItemInstance> factory = null;
  665. NodePacketValueFactory<ProjectItemInstance> factory2 = null;
  666. translator.TranslateDictionary<PropertyDictionary<ProjectPropertyInstance>, ProjectPropertyInstance>(ref this.globalProperties, new NodePacketValueFactory<ProjectPropertyInstance>(ProjectPropertyInstance.FactoryForDeserialization));
  667. translator.TranslateDictionary<PropertyDictionary<ProjectPropertyInstance>, ProjectPropertyInstance>(ref this.properties, new NodePacketValueFactory<ProjectPropertyInstance>(ProjectPropertyInstance.FactoryForDeserialization));
  668. if (translator.Mode == TranslationDirection.ReadFromStream)
  669. {
  670. int num = 0;
  671. translator.Translate(ref num);
  672. this.items = new ItemDictionary<ProjectItemInstance>(num);
  673. for (int i = 0; i < num; i++)
  674. {
  675. int num3 = 0;
  676. translator.Translate(ref num3);
  677. for (int j = 0; j < num3; j++)
  678. {
  679. ProjectItemInstance instance = null;
  680. if (factory == null)
  681. {
  682. factory = outerTranslator => ProjectItemInstance.FactoryForDeserialization(translator, this);
  683. }
  684. translator.Translate<ProjectItemInstance>(ref instance, factory);
  685. this.items.Add(instance);
  686. }
  687. }
  688. }
  689. else
  690. {
  691. int count = this.items.ItemTypes.Count;
  692. translator.Translate(ref count);
  693. foreach (string str in this.items.ItemTypes)
  694. {
  695. ICollection<ProjectItemInstance> is2 = this.items[str];
  696. int num6 = is2.Count;
  697. translator.Translate(ref num6);
  698. foreach (ProjectItemInstance instance2 in is2)
  699. {
  700. ProjectItemInstance instance3 = instance2;
  701. if (factory2 == null)
  702. {
  703. factory2 = outerTranslator => ProjectItemInstance.FactoryForDeserialization(translator, this);
  704. }
  705. translator.Translate<ProjectItemInstance>(ref instance3, factory2);
  706. }
  707. }
  708. }
  709. }
  710. public List<string> DefaultTargets
  711. {
  712. [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  713. get
  714. {
  715. return this.<DefaultTargets>k__BackingField;
  716. }
  717. [CompilerGenerated]
  718. private set
  719. {
  720. this.<DefaultTargets>k__BackingField = value;
  721. }
  722. }
  723. public string Directory
  724. {
  725. [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  726. get
  727. {
  728. return this.directory;
  729. }
  730. }
  731. internal string ExplicitToolsVersion
  732. {
  733. get
  734. {
  735. if (!this.explicitToolsVersionSpecified)
  736. {
  737. return null;
  738. }
  739. return this.Toolset.ToolsVersion;
  740. }
  741. }
  742. internal bool ExplicitToolsVersionSpecified
  743. {
  744. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  745. get
  746. {
  747. return this.explicitToolsVersionSpecified;
  748. }
  749. }
  750. public string FullPath
  751. {
  752. [DebuggerStepThrough]
  753. get
  754. {
  755. return this.projectFileLocation.File;
  756. }
  757. }
  758. public IDictionary<string, string> GlobalProperties
  759. {
  760. [DebuggerStepThrough]
  761. get
  762. {
  763. if ((this.globalProperties == null) || (this.globalProperties.Count == 0))
  764. {
  765. return ReadOnlyEmptyDictionary<string, string>.Instance;
  766. }
  767. Dictionary<string, string> backing = new Dictionary<string, string>(this.globalProperties.Count, MSBuildNameIgnoreCaseComparer.Default);
  768. foreach (ProjectPropertyInstance instance in this.globalProperties)
  769. {
  770. backing[instance.Name] = ((IProperty) instance).EvaluatedValueEscaped;
  771. }
  772. return ReadOnlyDictionary<string, string>.CreateWrapper(backing);
  773. }
  774. }
  775. internal PropertyDictionary<ProjectPropertyInstance> GlobalPropertiesDictionary
  776. {
  777. [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  778. get
  779. {
  780. return this.globalProperties;
  781. }
  782. }
  783. internal IDictionary<string, object> InitialGlobalsForDebugging
  784. {
  785. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  786. get
  787. {
  788. return this.initialGlobalsForDebugging;
  789. }
  790. }
  791. public List<string> InitialTargets
  792. {
  793. [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  794. get

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