-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ProjectExtensionsElement.cs
214 lines (182 loc) · 7.4 KB
/
ProjectExtensionsElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Xml;
using Microsoft.Build.ObjectModelRemoting;
using Microsoft.Build.Shared;
#nullable disable
namespace Microsoft.Build.Construction
{
/// <summary>
/// ProjectExtensionsElement represents the ProjectExtensions element in the MSBuild project.
/// ProjectExtensions can contain arbitrary XML content.
/// The ProjectExtensions element is deprecated and provided only for backward compatibility.
/// Use a property instead. Properties can also contain XML content.
/// </summary>
public class ProjectExtensionsElement : ProjectElement
{
internal ProjectExtensionsElementLink ExtensionLink => (ProjectExtensionsElementLink)Link;
/// <summary>
/// External projects support
/// </summary>
internal ProjectExtensionsElement(ProjectExtensionsElementLink link)
: base(link)
{
}
/// <summary>
/// Initialize a parented ProjectExtensionsElement instance
/// </summary>
internal ProjectExtensionsElement(XmlElement xmlElement, ProjectRootElement parent, ProjectRootElement project)
: base(xmlElement, parent, project)
{
ErrorUtilities.VerifyThrowArgumentNull(parent, nameof(parent));
}
/// <summary>
/// Initialize an unparented ProjectExtensionsElement instance
/// </summary>
private ProjectExtensionsElement(XmlElement xmlElement, ProjectRootElement project)
: base(xmlElement, null, project)
{
}
/// <summary>
/// Condition should never be set, but the getter returns null instead of throwing
/// because a nonexistent condition is implicitly true
/// </summary>
public override string Condition
{
get => null;
set => ErrorUtilities.ThrowInvalidOperation("OM_CannotGetSetCondition");
}
/// <summary>
/// Gets and sets the raw XML content
/// </summary>
public string Content
{
[DebuggerStepThrough]
get
{
return Link != null ? ExtensionLink.Content : XmlElement.InnerXml;
}
set
{
ErrorUtilities.VerifyThrowArgumentNull(value, nameof(Content));
if (Link != null)
{
ExtensionLink.Content = value;
return;
}
XmlElement.InnerXml = value;
MarkDirty("Set ProjectExtensions raw {0}", value);
}
}
/// <summary>
/// This does not allow conditions, so it should not be called.
/// </summary>
public override ElementLocation ConditionLocation
{
get
{
ErrorUtilities.ThrowInternalError("Should not evaluate this");
return null;
}
}
/// <summary>
/// Get or set the content of the first sub-element
/// with the provided name.
/// </summary>
public string this[string name]
{
get
{
ErrorUtilities.VerifyThrowArgumentLength(name, nameof(name));
if (Link != null)
{
return ExtensionLink.GetSubElement(name);
}
XmlElement idElement = XmlElement[name];
// remove the xmlns attribute, because the IDE's not expecting that
return idElement == null ? String.Empty : Internal.Utilities.RemoveXmlNamespace(idElement.InnerXml);
}
set
{
ErrorUtilities.VerifyThrowArgumentLength(name, nameof(name));
ErrorUtilities.VerifyThrowArgumentNull(value, "value");
if (Link != null)
{
ExtensionLink.SetSubElement(name, value);
return;
}
XmlElement idElement = XmlElement[name];
if (idElement == null)
{
if (value.Length == 0)
{
return;
}
idElement = XmlDocument.CreateElement(name, XmlElement.NamespaceURI);
XmlElement.AppendChild(idElement);
}
// The actual InnerXml may have the MSBuild namespace but be otherwise identical
// to the setting, in which case the namespace was probably inherited from the
// document and should be ignored.
if (idElement.InnerXml != value &&
idElement.InnerXml.Replace(ProjectRootElement.EmptyProjectFileXmlNamespace, string.Empty) != value)
{
if (value.Length == 0)
{
XmlElement.RemoveChild(idElement);
}
else
{
idElement.InnerXml = value;
}
MarkDirty("Set ProjectExtensions content {0}", value);
}
}
}
/// <inheritdoc/>
public override void CopyFrom(ProjectElement element)
{
ErrorUtilities.VerifyThrowArgumentNull(element, nameof(element));
ErrorUtilities.VerifyThrowArgument(GetType().IsEquivalentTo(element.GetType()), "CannotCopyFromElementOfThatType");
if (this == element)
{
return;
}
Label = element.Label;
var other = (ProjectExtensionsElement)element;
Content = other.Content;
MarkDirty("CopyFrom", null);
}
/// <summary>
/// Creates a ProjectExtensionsElement parented by a project
/// </summary>
internal static ProjectExtensionsElement CreateParented(XmlElementWithLocation element, ProjectRootElement parent, ProjectRootElement containingProject)
{
return new ProjectExtensionsElement(element, parent, containingProject);
}
/// <summary>
/// Creates an unparented ProjectExtensionsElement, wrapping an unparented XmlElement.
/// Caller should then ensure the element is added to a parent
/// </summary>
internal static ProjectExtensionsElement CreateDisconnected(ProjectRootElement containingProject)
{
XmlElementWithLocation element = containingProject.CreateElement(XMakeElements.projectExtensions);
return new ProjectExtensionsElement(element, containingProject);
}
/// <summary>
/// Overridden to verify that the potential parent and siblings
/// are acceptable. Throws InvalidOperationException if they are not.
/// </summary>
internal override void VerifyThrowInvalidOperationAcceptableLocation(ProjectElementContainer parent, ProjectElement previousSibling, ProjectElement nextSibling)
{
ErrorUtilities.VerifyThrowInvalidOperation(parent is ProjectRootElement, "OM_CannotAcceptParent");
}
/// <inheritdoc />
protected override ProjectElement CreateNewInstance(ProjectRootElement owner)
{
return owner.CreateProjectExtensionsElement();
}
}
}