/Microsoft.Build/Microsoft.Build/Microsoft/Build/Evaluation/ProjectItem.cs
C# | 608 lines | 555 code | 53 blank | 0 comment | 81 complexity | 6a31feaabd3ad2df700450de32ae5b2d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
- namespace Microsoft.Build.Evaluation
- {
- using Microsoft.Build.Collections;
- using Microsoft.Build.Construction;
- using Microsoft.Build.Shared;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Runtime;
-
- [DebuggerDisplay("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
- public class ProjectItem : IItem<ProjectMetadata>, IItem, IKeyed, IProjectMetadataParent, IMetadataTable
- {
- private Microsoft.Build.Evaluation.BuiltInMetadata builtInMetadata;
- private PropertyDictionary<ProjectMetadata> directMetadata;
- private readonly string evaluatedIncludeBeforeWildcardExpansionEscaped;
- private string evaluatedIncludeEscaped;
- private readonly List<ProjectItemDefinition> inheritedItemDefinitions;
- private readonly Microsoft.Build.Evaluation.Project project;
- private ProjectItemElement xml;
-
- internal ProjectItem(Microsoft.Build.Evaluation.Project project, ProjectItemElement xml, string evaluatedIncludeEscaped, string evaluatedIncludeBeforeWildcardExpansionEscaped, PropertyDictionary<ProjectMetadata> directMetadataCloned, List<ProjectItemDefinition> inheritedItemDefinitionsCloned)
- {
- ErrorUtilities.VerifyThrowInternalNull(project, "project");
- ErrorUtilities.VerifyThrowArgumentNull(xml, "xml");
- ErrorUtilities.VerifyThrowArgumentNull(evaluatedIncludeEscaped, "evaluatedIncludeEscaped");
- ErrorUtilities.VerifyThrowArgumentNull(evaluatedIncludeBeforeWildcardExpansionEscaped, "evaluatedIncludeBeforeWildcardExpansionEscaped");
- this.xml = xml;
- this.project = project;
- this.evaluatedIncludeEscaped = evaluatedIncludeEscaped;
- this.evaluatedIncludeBeforeWildcardExpansionEscaped = evaluatedIncludeBeforeWildcardExpansionEscaped;
- this.directMetadata = directMetadataCloned;
- this.inheritedItemDefinitions = inheritedItemDefinitionsCloned;
- }
-
- internal void ChangeItemType(string newItemType)
- {
- ErrorUtilities.VerifyThrowArgumentLength(newItemType, "ItemType");
- this.Project.VerifyThrowInvalidOperationNotImported(this.xml.ContainingProject);
- ErrorUtilities.VerifyThrowInvalidOperation((this.xml.Parent != null) && (this.xml.Parent.Parent != null), "OM_ObjectIsNoLongerActive");
- if (!string.Equals(this.ItemType, newItemType, StringComparison.Ordinal))
- {
- this.project.SplitItemElementIfNecessary(this.xml);
- this.project.RemoveItemBeforeItemTypeChange(this);
- try
- {
- this.xml.ChangeItemType(newItemType);
- }
- finally
- {
- this.project.ReAddExistingItemAfterItemTypeChange(this);
- }
- }
- }
-
- private string GetBuiltInMetadataEscaped(string name)
- {
- string str = null;
- if (!FileUtilities.ItemSpecModifiers.IsItemSpecModifier(name))
- {
- return str;
- }
- this.builtInMetadata = this.builtInMetadata ?? new Microsoft.Build.Evaluation.BuiltInMetadata(this.project.DirectoryPath);
- return this.builtInMetadata.GetMetadataValueEscaped(this.project.DirectoryPath, this.evaluatedIncludeBeforeWildcardExpansionEscaped, this.evaluatedIncludeEscaped, name);
- }
-
- private ProjectMetadata GetItemDefinitionMetadata(string name)
- {
- ProjectMetadata metadata = null;
- ProjectItemDefinition definition2;
- if (this.inheritedItemDefinitions != null)
- {
- foreach (ProjectItemDefinition definition in this.inheritedItemDefinitions)
- {
- metadata = definition.GetMetadata(name);
- if (metadata != null)
- {
- return metadata;
- }
- }
- }
- if (this.project.ItemDefinitions.TryGetValue(this.ItemType, out definition2))
- {
- metadata = definition2.GetMetadata(name);
- }
- return metadata;
- }
-
- public ProjectMetadata GetMetadata(string name)
- {
- ErrorUtilities.VerifyThrowArgumentLength(name, "name");
- ProjectMetadata itemDefinitionMetadata = null;
- if (this.directMetadata != null)
- {
- itemDefinitionMetadata = this.directMetadata[name];
- }
- if (itemDefinitionMetadata == null)
- {
- itemDefinitionMetadata = this.GetItemDefinitionMetadata(name);
- }
- return itemDefinitionMetadata;
- }
-
- public string GetMetadataValue(string name)
- {
- return EscapingUtilities.UnescapeAll(((IItem) this).GetMetadataValueEscaped(name));
- }
-
- public bool HasMetadata(string name)
- {
- return (((this.directMetadata != null) && this.directMetadata.Contains(name)) || (FileUtilities.ItemSpecModifiers.IsItemSpecModifier(name) || (this.GetItemDefinitionMetadata(name) != null)));
- }
-
- string IItem.GetMetadataValueEscaped(string name)
- {
- ErrorUtilities.VerifyThrowArgumentLength(name, "name");
- string evaluatedValueEscaped = null;
- if (this.directMetadata != null)
- {
- ProjectMetadata metadata = this.directMetadata[name];
- if (metadata != null)
- {
- evaluatedValueEscaped = metadata.EvaluatedValueEscaped;
- }
- }
- if (evaluatedValueEscaped == null)
- {
- evaluatedValueEscaped = this.GetBuiltInMetadataEscaped(name);
- }
- if (evaluatedValueEscaped == null)
- {
- ProjectMetadata itemDefinitionMetadata = this.GetItemDefinitionMetadata(name);
- if ((itemDefinitionMetadata != null) && Expander<ProjectProperty, ProjectItem>.ExpressionMayContainExpandableExpressions(itemDefinitionMetadata.EvaluatedValueEscaped))
- {
- evaluatedValueEscaped = new Expander<ProjectProperty, ProjectItem>(null, null, new BuiltInMetadataTable(this)).ExpandIntoStringLeaveEscaped(itemDefinitionMetadata.EvaluatedValueEscaped, ExpanderOptions.ExpandBuiltInMetadata, itemDefinitionMetadata.Location);
- }
- else if (itemDefinitionMetadata != null)
- {
- return itemDefinitionMetadata.EvaluatedValueEscaped;
- }
- }
- return (evaluatedValueEscaped ?? string.Empty);
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- ProjectMetadata IItem<ProjectMetadata>.GetMetadata(string name)
- {
- return this.GetMetadata(name);
- }
-
- ProjectMetadata IItem<ProjectMetadata>.SetMetadata(ProjectMetadataElement metadataElement, string evaluatedInclude)
- {
- this.directMetadata = this.directMetadata ?? new PropertyDictionary<ProjectMetadata>();
- ProjectMetadata predecessor = this.GetMetadata(metadataElement.Name);
- ProjectMetadata projectProperty = new ProjectMetadata(this, metadataElement, evaluatedInclude, predecessor);
- this.directMetadata.Set(projectProperty);
- return projectProperty;
- }
-
- string IMetadataTable.GetEscapedValue(string name)
- {
- return ((IMetadataTable) this).GetEscapedValue(null, name);
- }
-
- string IMetadataTable.GetEscapedValue(string itemType, string name)
- {
- return (((IMetadataTable) this).GetEscapedValueIfPresent(itemType, name) ?? string.Empty);
- }
-
- string IMetadataTable.GetEscapedValueIfPresent(string itemType, string name)
- {
- if ((itemType == null) || MSBuildNameIgnoreCaseComparer.Default.Equals(this.ItemType, itemType))
- {
- string metadataValueEscaped = ((IItem) this).GetMetadataValueEscaped(name);
- if ((metadataValueEscaped.Length > 0) || this.HasMetadata(name))
- {
- return metadataValueEscaped;
- }
- }
- return null;
- }
-
- public bool RemoveMetadata(string name)
- {
- ErrorUtilities.VerifyThrowArgumentLength(name, "name");
- ErrorUtilities.VerifyThrowArgument(!FileUtilities.ItemSpecModifiers.IsItemSpecModifier(name), "ItemSpecModifierCannotBeCustomMetadata", name);
- this.Project.VerifyThrowInvalidOperationNotImported(this.xml.ContainingProject);
- ErrorUtilities.VerifyThrowInvalidOperation((this.xml.Parent != null) && (this.xml.Parent.Parent != null), "OM_ObjectIsNoLongerActive");
- if (((this.directMetadata == null) ? null : this.directMetadata[name]) == null)
- {
- ErrorUtilities.VerifyThrowInvalidOperation(this.GetItemDefinitionMetadata(name) == null, "OM_CannotRemoveMetadataOriginatingFromItemDefinition", name);
- return false;
- }
- this.project.SplitItemElementIfNecessary(this.xml);
- ProjectMetadata metadata = this.directMetadata[name];
- this.xml.RemoveChild(metadata.Xml);
- this.directMetadata.Remove(name);
- return true;
- }
-
- public void Rename(string name)
- {
- this.Project.VerifyThrowInvalidOperationNotImported(this.xml.ContainingProject);
- ErrorUtilities.VerifyThrowInvalidOperation((this.xml.Parent != null) && (this.xml.Parent.Parent != null), "OM_ObjectIsNoLongerActive");
- if (!string.Equals(this.UnevaluatedInclude, name, StringComparison.Ordinal))
- {
- if (((this.xml.Count == 0) && this.project.IsSuitableExistingItemXml(this.xml, name, null)) && !FileMatcher.HasWildcardsSemicolonItemOrPropertyReferences(name))
- {
- this.evaluatedIncludeEscaped = name;
- }
- else
- {
- bool flag = this.project.SplitItemElementIfNecessary(this.xml);
- this.xml.Include = name;
- if (flag)
- {
- this.evaluatedIncludeEscaped = name;
- }
- else
- {
- this.evaluatedIncludeEscaped = this.project.ExpandItemIncludeBestEffortLeaveEscaped(this.xml);
- }
- this.builtInMetadata = null;
- }
- }
- }
-
- public ProjectMetadata SetMetadataValue(string name, string unevaluatedValue)
- {
- ProjectMetadata metadata;
- this.Project.VerifyThrowInvalidOperationNotImported(this.xml.ContainingProject);
- XmlUtilities.VerifyThrowArgumentValidElementName(name);
- ErrorUtilities.VerifyThrowArgument(!FileUtilities.ItemSpecModifiers.IsItemSpecModifier(name), "ItemSpecModifierCannotBeCustomMetadata", name);
- ErrorUtilities.VerifyThrowInvalidOperation(XMakeElements.IllegalItemPropertyNames[name] == null, "CannotModifyReservedItemMetadata", name);
- ErrorUtilities.VerifyThrowInvalidOperation((this.xml.Parent != null) && (this.xml.Parent.Parent != null), "OM_ObjectIsNoLongerActive");
- this.project.SplitItemElementIfNecessary(this.xml);
- if (this.directMetadata != null)
- {
- metadata = this.directMetadata[name];
- if (metadata != null)
- {
- metadata.UnevaluatedValue = unevaluatedValue;
- return metadata;
- }
- }
- ProjectMetadataElement xml = this.xml.AddMetadata(name, unevaluatedValue);
- this.directMetadata = this.directMetadata ?? new PropertyDictionary<ProjectMetadata>();
- string evaluatedValueEscaped = this.project.ExpandMetadataValueBestEffortLeaveEscaped(this, unevaluatedValue, xml.Location);
- metadata = new ProjectMetadata(this, xml, evaluatedValueEscaped, null);
- this.directMetadata.Set(metadata);
- return metadata;
- }
-
- internal void SplitOwnItemElement()
- {
- ProjectItemElement xml = this.xml;
- this.xml = this.xml.ContainingProject.CreateItemElement(this.ItemType, ((IItem) this).EvaluatedIncludeEscaped);
- xml.Parent.InsertBeforeChild(this.xml, xml);
- if (this.directMetadata != null)
- {
- List<ProjectMetadata> list = new List<ProjectMetadata>(this.directMetadata.Count);
- foreach (ProjectMetadata metadata in this.directMetadata)
- {
- list.Add(metadata);
- }
- this.directMetadata = (this.directMetadata == null) ? null : new PropertyDictionary<ProjectMetadata>(this.directMetadata.Count);
- foreach (ProjectMetadata metadata2 in list)
- {
- this.SetMetadataValue(metadata2.Name, metadata2.EvaluatedValueEscaped);
- }
- }
- }
-
- internal Microsoft.Build.Evaluation.BuiltInMetadata BuiltInMetadata
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.builtInMetadata;
- }
- }
-
- public IEnumerable<ProjectMetadata> DirectMetadata
- {
- get
- {
- return (this.directMetadata ?? ReadOnlyEmptyCollection<ProjectMetadata>.Instance);
- }
- }
-
- public int DirectMetadataCount
- {
- [DebuggerStepThrough]
- get
- {
- if (this.directMetadata == null)
- {
- return 0;
- }
- return this.directMetadata.Count;
- }
- }
-
- public string EvaluatedInclude
- {
- [DebuggerStepThrough]
- get
- {
- return EscapingUtilities.UnescapeAll(this.evaluatedIncludeEscaped);
- }
- }
-
- internal string EvaluatedIncludeBeforeWildcardExpansion
- {
- [DebuggerStepThrough]
- get
- {
- return EscapingUtilities.UnescapeAll(this.evaluatedIncludeBeforeWildcardExpansionEscaped);
- }
- }
-
- internal string EvaluatedIncludeBeforeWildcardExpansionEscaped
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.evaluatedIncludeBeforeWildcardExpansionEscaped;
- }
- }
-
- internal List<ProjectItemDefinition> InheritedItemDefinitions
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.inheritedItemDefinitions;
- }
- }
-
- public bool IsImported
- {
- get
- {
- return !object.ReferenceEquals(this.xml.ContainingProject, this.project.Xml);
- }
- }
-
- public string ItemType
- {
- [DebuggerStepThrough]
- get
- {
- return this.xml.ItemType;
- }
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- set
- {
- this.ChangeItemType(value);
- }
- }
-
- public ICollection<ProjectMetadata> Metadata
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.MetadataCollection;
- }
- }
-
- internal ICollection<ProjectMetadata> MetadataCollection
- {
- get
- {
- Dictionary<string, ProjectMetadata> dictionary = new Dictionary<string, ProjectMetadata>(MSBuildNameIgnoreCaseComparer.Default);
- ProjectItemDefinition definition = null;
- if (this.project.ItemDefinitions.TryGetValue(this.ItemType, out definition))
- {
- foreach (ProjectMetadata metadata in definition.Metadata)
- {
- dictionary[metadata.Name] = metadata;
- }
- }
- if (this.inheritedItemDefinitions != null)
- {
- for (int i = this.inheritedItemDefinitions.Count - 1; i >= 0; i--)
- {
- foreach (ProjectMetadata metadata2 in this.inheritedItemDefinitions[i].Metadata)
- {
- dictionary[metadata2.Name] = metadata2;
- }
- }
- }
- if (this.directMetadata != null)
- {
- foreach (ProjectMetadata metadata3 in this.directMetadata)
- {
- dictionary[metadata3.Name] = metadata3;
- }
- }
- return dictionary.Values;
- }
- }
-
- public int MetadataCount
- {
- [DebuggerStepThrough]
- get
- {
- return (this.MetadataCollection.Count + FileUtilities.ItemSpecModifiers.All.Length);
- }
- }
-
- string IKeyed.Key
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.ItemType;
- }
- }
-
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- string IItem.EvaluatedIncludeEscaped
- {
- [DebuggerStepThrough]
- get
- {
- return this.evaluatedIncludeEscaped;
- }
- }
-
- string IItem.ProjectDirectory
- {
- get
- {
- return this.project.DirectoryPath;
- }
- }
-
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public Microsoft.Build.Evaluation.Project Project
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.project;
- }
- }
-
- public string UnevaluatedInclude
- {
- [DebuggerStepThrough]
- get
- {
- return this.xml.Include;
- }
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- set
- {
- this.Rename(value);
- }
- }
-
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public ProjectItemElement Xml
- {
- [DebuggerStepThrough, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.xml;
- }
- }
-
- private class BuiltInMetadataTable : IMetadataTable
- {
- private ProjectItem item;
-
- internal BuiltInMetadataTable(ProjectItem item)
- {
- this.item = item;
- }
-
- public string GetEscapedValue(string name)
- {
- return this.item.GetBuiltInMetadataEscaped(name);
- }
-
- public string GetEscapedValue(string itemType, string name)
- {
- return (this.GetEscapedValueIfPresent(itemType, name) ?? string.Empty);
- }
-
- public string GetEscapedValueIfPresent(string itemType, string name)
- {
- string str = null;
- if ((itemType != null) && !string.Equals(this.item.ItemType, itemType, StringComparison.OrdinalIgnoreCase))
- {
- return str;
- }
- return this.GetEscapedValue(name);
- }
- }
-
- internal class ProjectItemFactory : IItemFactory<ProjectItem, ProjectItem>
- {
- private readonly Project project;
- private ProjectItemElement xml;
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- internal ProjectItemFactory(Project project)
- {
- this.project = project;
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- internal ProjectItemFactory(Project project, ProjectItemElement xml)
- {
- this.project = project;
- this.xml = xml;
- }
-
- public ProjectItem CreateItem(ProjectItem source)
- {
- return this.CreateItem(source.evaluatedIncludeEscaped, source.evaluatedIncludeBeforeWildcardExpansionEscaped, source);
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- public ProjectItem CreateItem(string include)
- {
- return this.CreateItem(include, include);
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- public ProjectItem CreateItem(string evaluatedIncludeEscaped, ProjectItem source)
- {
- return this.CreateItem(evaluatedIncludeEscaped, evaluatedIncludeEscaped, source);
- }
-
- public ProjectItem CreateItem(string evaluatedIncludeEscaped, string evaluatedIncludeBeforeWildcardExpansion)
- {
- ErrorUtilities.VerifyThrowInternalNull(this.xml, "xml");
- return new ProjectItem(this.project, this.xml, evaluatedIncludeEscaped, evaluatedIncludeBeforeWildcardExpansion, null, null);
- }
-
- private ProjectItem CreateItem(string evaluatedIncludeEscaped, string evaluatedIncludeBeforeWildcardExpansionEscaped, ProjectItem source)
- {
- ProjectItemDefinition definition;
- ErrorUtilities.VerifyThrowInternalNull(this.xml, "xml");
- PropertyDictionary<ProjectMetadata> directMetadataCloned = null;
- if (source.DirectMetadataCount > 0)
- {
- directMetadataCloned = new PropertyDictionary<ProjectMetadata>(source.DirectMetadataCount);
- foreach (ProjectMetadata metadata in source.directMetadata)
- {
- directMetadataCloned.Set(metadata.DeepClone());
- }
- }
- int num = (source.inheritedItemDefinitions == null) ? 0 : source.inheritedItemDefinitions.Count;
- List<ProjectItemDefinition> inheritedItemDefinitionsCloned = null;
- if (source.inheritedItemDefinitions != null)
- {
- inheritedItemDefinitionsCloned = inheritedItemDefinitionsCloned ?? new List<ProjectItemDefinition>(num + 1);
- inheritedItemDefinitionsCloned.AddRange(source.inheritedItemDefinitions);
- }
- if (this.project.ItemDefinitions.TryGetValue(source.ItemType, out definition))
- {
- inheritedItemDefinitionsCloned = inheritedItemDefinitionsCloned ?? new List<ProjectItemDefinition>(num + 1);
- inheritedItemDefinitionsCloned.Add(definition);
- }
- return new ProjectItem(this.project, this.xml, evaluatedIncludeEscaped, evaluatedIncludeBeforeWildcardExpansionEscaped, directMetadataCloned, inheritedItemDefinitionsCloned);
- }
-
- public void SetMetadata(IEnumerable<Pair<ProjectMetadataElement, string>> metadata, IEnumerable<ProjectItem> destinationItems)
- {
- foreach (IItem<ProjectMetadata> item in destinationItems)
- {
- foreach (Pair<ProjectMetadataElement, string> pair in metadata)
- {
- item.SetMetadata(pair.Key, pair.Value);
- }
- }
- }
-
- public ProjectItemElement ItemElement
- {
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- set
- {
- this.xml = value;
- }
- }
-
- public string ItemType
- {
- get
- {
- return this.xml.ItemType;
- }
- set
- {
- ErrorUtilities.ThrowInternalError("Cannot change the item type on ProjectItem.ProjectItemFactory", new object[0]);
- }
- }
- }
- }
- }
-