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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
//
// ProjectItemMetadata.cs
//
// Author:
// Lluis Sanchez Gual <lluis@xamarin.com>
//
// Copyright (c) 2015 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using MonoDevelop.Core;
using MonoDevelop.Projects.MSBuild;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
namespace MonoDevelop.Projects
{
public class ProjectItemMetadata: IPropertySet
{
Dictionary<string,MSBuildProperty> properties;
List<MSBuildProperty> propertyList = new List<MSBuildProperty> ();
int initialMetadataCount;
MSBuildProject project;
internal ProjectItemMetadata ()
{
}
internal ProjectItemMetadata (MSBuildProject project)
{
this.project = project;
}
internal ProjectItemMetadata (ProjectItemMetadata other)
{
this.project = other.project;
if (other.properties != null) {
properties = new Dictionary<string, MSBuildProperty> (other.properties.Count, StringComparer.OrdinalIgnoreCase);
foreach (var p in other.propertyList) {
var pc = p.Clone ();
propertyList.Add (pc);
properties [p.Name] = pc;
}
}
initialMetadataCount = other.initialMetadataCount;
}
internal void OnLoaded ()
{
initialMetadataCount = propertyList.Count;
}
internal void SetProject (MSBuildProject project)
{
foreach (var p in propertyList) {
p.ParentProject = project;
p.ResolvePath ();
}
}
/* internal void LoadProperties (XmlElement element)
{
foreach (var pelem in element.ChildNodes.OfType<XmlElement> ()) {
MSBuildProperty prevSameName;
if (properties != null && properties.TryGetValue (pelem.Name, out prevSameName))
prevSameName.Overwritten = true;
if (properties == null) {
properties = new Dictionary<string,MSBuildProperty> ();
propertyList = new List<MSBuildProperty> ();
}
var prop = new MSBuildProperty (project, pelem);
propertyList.Add (prop);
properties [pelem.Name] = prop; // If a property is defined more than once, we only care about the last registered value
}
initialMetadataCount = properties.Count;
}*/
internal bool PropertyCountHasChanged {
get { return initialMetadataCount != (properties != null ? properties.Count : 0); }
}
public IMetadataProperty GetProperty (string name)
{
if (properties == null)
return null;
MSBuildProperty prop;
properties.TryGetValue (name, out prop);
return prop;
}
public IEnumerable<IMetadataProperty> GetProperties ()
{
if (propertyList == null)
return new IMetadataProperty [0];
return propertyList.Where (p => !p.Overwritten);
}
public string GetValue (string name, string defaultValue = null)
{
var prop = GetProperty (name);
if (prop != null)
return prop.Value;
else
return defaultValue;
}
public FilePath GetPathValue (string name, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
{
var prop = GetProperty (name);
if (prop != null)
return prop.GetPathValue (relativeToProject, relativeToPath);
else
return defaultValue;
}
public bool TryGetPathValue (string name, out FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
{
var prop = GetProperty (name);
if (prop != null)
return prop.TryGetPathValue (out value, relativeToProject, relativeToPath);
else {
value = defaultValue;
return value != default(FilePath);
}
}
public T GetValue<T> (string name)
{
var prop = GetProperty (name);
if (prop != null)
return prop.GetValue<T> ();
else
return default(T);
}
public T GetValue<T> (string name, T defaultValue)
{
var prop = GetProperty (name);
if (prop != null)
return prop.GetValue<T> ();
else
return defaultValue;
}
public object GetValue (string name, Type type, object defaultValue)
{
var prop = GetProperty (name);
if (prop != null)
return prop.GetValue (type);
else
return defaultValue;
}
MSBuildProperty AddProperty (string name, string condition = null)
{
if (properties == null) {
properties = new Dictionary<string, MSBuildProperty> (StringComparer.OrdinalIgnoreCase);
propertyList = new List<MSBuildProperty> ();
}
int i = propertyOrder != null ? propertyOrder.IndexOf (name) : -1;
int insertIndex = -1;
if (i != -1) {
var foundProp = FindExistingProperty (i - 1, -1);
if (foundProp != null) {
insertIndex = propertyList.IndexOf (foundProp) + 1;
} else {
foundProp = FindExistingProperty (i + 1, 1);
if (foundProp != null)
insertIndex = propertyList.IndexOf (foundProp) - 1;
}
}
var prop = new ItemMetadataProperty (name);
prop.ParentProject = project;
properties [name] = prop;
if (insertIndex != -1)
propertyList.Insert (insertIndex, prop);
else
propertyList.Add (prop);
return prop;
}
internal void AddProperty (ItemMetadataProperty prop)
{
if (properties == null) {
properties = new Dictionary<string, MSBuildProperty> (StringComparer.OrdinalIgnoreCase);
propertyList = new List<MSBuildProperty> ();
}
prop.ParentProject = project;
properties [prop.Name] = prop;
propertyList.Add (prop);
}
MSBuildProperty FindExistingProperty (int index, int inc)
{
while (index >= 0 && index < propertyOrder.Count) {
var ep = (MSBuildProperty) GetProperty (propertyOrder[index]);
if (ep != null)
return ep;
index += inc;
}
return null;
}
void IPropertySet.SetValue (string name, string value, string defaultValue, bool preserveExistingCase, bool mergeToMainGroup, string condition, MSBuildValueType valueType)
{
SetValue (name, value, defaultValue, preserveExistingCase, valueType);
}
public void SetValue (string name, string value, string defaultValue = null, bool preserveExistingCase = false, MSBuildValueType valueType = null)
{
// If no value type is specified, use the default
if (valueType == null)
valueType = preserveExistingCase ? MSBuildValueType.DefaultPreserveCase : MSBuildValueType.Default;
if (value == null && defaultValue == "")
value = "";
var prop = (MSBuildProperty) GetProperty (name);
var isDefault = value == defaultValue;
if (isDefault) {
// if the value is default, only remove the property if it was not already the default
// to avoid unnecessary project file churn
if (prop != null && (defaultValue == null || !valueType.Equals (defaultValue, prop.Value)))
RemoveProperty (prop);
return;
}
if (prop == null)
prop = AddProperty (name);
prop.SetValue (value, valueType:valueType);
prop.HasDefaultValue = isDefault;
}
void IPropertySet.SetValue (string name, FilePath value, FilePath defaultValue, bool relativeToProject, FilePath relativeToPath, bool mergeToMainGroup, string condition)
{
SetValue (name, value, defaultValue, relativeToProject, relativeToPath);
}
public void SetValue (string name, FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
{
var prop = (MSBuildProperty) GetProperty (name);
var isDefault = value.CanonicalPath == defaultValue.CanonicalPath;
if (isDefault) {
// if the value is default, only remove the property if it was not already the default
// to avoid unnecessary project file churn
if (prop != null && (defaultValue == null || defaultValue != prop.GetPathValue (relativeToProject, relativeToPath)))
RemoveProperty (prop);
return;
}
if (prop == null)
prop = AddProperty (name);
prop.SetValue (value, relativeToProject, relativeToPath);
prop.HasDefaultValue = isDefault;
}
void IPropertySet.SetValue (string name, object value, object defaultValue, bool mergeToMainGroup, string condition)
{
SetValue (name, value, defaultValue);
}
public void SetValue (string name, object value, object defaultValue = null)
{
var prop = (MSBuildProperty) GetProperty (name);
var isDefault = object.Equals (value, defaultValue);
if (isDefault) {
// if the value is default, only remove the property if it was not already the default
// to avoid unnecessary project file churn
if (prop != null && (defaultValue == null || !object.Equals (defaultValue, prop.GetValue (defaultValue.GetType ()))))
RemoveProperty (prop);
return;
}
if (prop == null)
prop = AddProperty (name);
prop.SetValue (value);
prop.HasDefaultValue = isDefault;
}
public bool RemoveProperty (string name)
{
MSBuildProperty prop = (MSBuildProperty) GetProperty (name);
if (prop != null) {
RemoveProperty (prop);
return true;
}
return false;
}
public void RemoveProperty (MSBuildProperty prop)
{
if (properties != null) {
properties.Remove (prop.Name);
propertyList.Remove (prop);
}
}
public override string ToString()
{
string s = "[ProjectItemMetadata:";
foreach (MSBuildProperty prop in GetProperties ())
s += " " + prop.Name + "=" + prop.Value;
return s + "]";
}
public bool HasProperty (string name)
{
return properties != null && properties.ContainsKey (name);
}
List<string> propertyOrder;
public void SetPropertyOrder (params string[] propertyNames)
{
if (propertyOrder == null)
propertyOrder = new List<string> ();
int i = 0;
foreach (var name in propertyNames) {
if (i < propertyOrder.Count) {
var pos = propertyOrder.IndexOf (name, i);
if (pos != -1) {
i = pos + 1;
continue;
} else {
propertyOrder.Insert (i, name);
i++;
continue;
}
}
propertyOrder.Add (name);
i++;
}
}
public void RemoveAllProperties ()
{
if (properties != null) {
properties.Clear ();
propertyList.Clear ();
}
}
}
}
|