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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System.Collections.Generic;
using System.Linq;
namespace QtVsTools.Core.MsBuild
{
public class MsBuildProjectContainer
{
private readonly IPropertyStorageProvider provider;
public MsBuildProjectContainer(IPropertyStorageProvider provider)
{
this.provider = provider;
}
private string GetPropertyValueByName(object propertyStorage, string itemType,
string propertyName)
{
return provider.GetProperty(propertyStorage, itemType, propertyName);
}
private bool SetPropertyValueByName(object propertyStorage, string itemType,
string propertyName, string propertyValue)
{
return provider.SetProperty(propertyStorage, itemType, propertyName, propertyValue);
}
private bool DeletePropertyByName(object propertyStorage, string itemType,
string propertyName)
{
return provider.DeleteProperty(propertyStorage, itemType, propertyName);
}
private class ItemPropertyChange
{
//key
public string ConfigName;
public string ItemTypeName;
public string ItemName;
public string PropertyName;
//value
public string PropertyValue;
public object PropertyStorage;
public void CopyFrom(ItemPropertyChange change)
{
ConfigName = change.ConfigName;
ItemTypeName = change.ItemTypeName;
ItemName = change.ItemName;
PropertyName = change.PropertyName;
PropertyValue = change.PropertyValue;
PropertyStorage = change.PropertyStorage;
}
public bool IsMocSource => ItemTypeName == QtMoc.ItemTypeName
&& !HelperFunctions.IsHeaderFile(ItemName);
public string GroupKey => string.Join(
",", ConfigName, ItemTypeName, PropertyName, PropertyValue, IsMocSource.ToString()
);
public string Key => string.Join(",", ConfigName, ItemTypeName, PropertyName, ItemName);
}
private IEnumerable<object> GetItems(string itemType, string configName = "")
{
return provider.GetItems(GetProject(), itemType, configName);
}
private int GetItemCount(string itemType, bool? isMocSource = null, string configName = "")
{
var items = GetItems(itemType, configName);
if (!isMocSource.HasValue)
return items.Count();
if (!isMocSource.Value) {
return items.Count(x => provider.GetItemType(x) != QtMoc.ItemTypeName
|| HelperFunctions.IsHeaderFile(provider.GetItemName(x)));
}
return items.Count(x => provider.GetItemType(x) == QtMoc.ItemTypeName
&& !HelperFunctions.IsHeaderFile(provider.GetItemName(x)));
}
private object GetProjectConfiguration(string configName)
{
return provider.GetProjectConfiguration(GetProject(), configName);
}
private readonly Dictionary<string, ItemPropertyChange> itemPropertyChanges = new();
private readonly Dictionary<string, List<ItemPropertyChange>> itemPropertyChangesGrouped = new();
private bool pendingChanges;
private void AddChange(ItemPropertyChange newChange)
{
if (itemPropertyChanges.TryGetValue(newChange.Key, out var oldChange)) {
if (oldChange.GroupKey == newChange.GroupKey) {
oldChange.CopyFrom(newChange);
return;
}
RemoveChange(oldChange);
}
if (!itemPropertyChangesGrouped.TryGetValue(newChange.GroupKey, out var changeGroup)) {
itemPropertyChangesGrouped.Add(
newChange.GroupKey,
changeGroup = new List<ItemPropertyChange>());
}
changeGroup.Add(newChange);
itemPropertyChanges.Add(newChange.Key, newChange);
}
private void RemoveChange(ItemPropertyChange change)
{
if (itemPropertyChangesGrouped.TryGetValue(change.GroupKey, out var changeGroup)) {
changeGroup.Remove(change);
if (changeGroup.Count == 0)
itemPropertyChangesGrouped.Remove(change.GroupKey);
}
itemPropertyChanges.Remove(change.Key);
}
private object GetProject()
{
var change = itemPropertyChanges.Values.FirstOrDefault();
return change == null ? null : provider.GetParentProject(change.PropertyStorage);
}
public bool BeginSetItemProperties()
{
if (pendingChanges)
return false;
itemPropertyChanges.Clear();
itemPropertyChangesGrouped.Clear();
pendingChanges = true;
return true;
}
public bool SetItemPropertyByName(object propertyStorage, string propertyName,
string propertyValue)
{
if (propertyStorage == null)
return false;
var configName = provider.GetConfigName(propertyStorage);
var itemType = provider.GetItemType(propertyStorage);
var itemName = provider.GetItemName(propertyStorage);
var newChange = new ItemPropertyChange
{
ConfigName = configName,
ItemTypeName = itemType,
ItemName = itemName,
PropertyName = propertyName,
PropertyValue = propertyValue,
PropertyStorage = propertyStorage
};
if (!pendingChanges) {
if (!BeginSetItemProperties())
return false;
AddChange(newChange);
if (!EndSetItemProperties())
return false;
} else {
AddChange(newChange);
}
return true;
}
private void SetGroupItemProperties(IReadOnlyCollection<ItemPropertyChange> changeGroup)
{
//grouped by configName, itemTypeName, propertyName, propertyValue, isMocSource
var firstChange = changeGroup.FirstOrDefault();
if (firstChange == null)
return;
var configName = firstChange.ConfigName;
var itemTypeName = firstChange.ItemTypeName;
var propertyName = firstChange.PropertyName;
var propertyValue = firstChange.PropertyValue;
var isMocSource = firstChange.IsMocSource;
var itemCount = GetItemCount(itemTypeName, isMocSource, configName);
var groupCount = changeGroup.Count;
var projConfig = GetProjectConfiguration(configName);
if (!isMocSource && groupCount == itemCount) {
//all items are setting the same value for this property
// -> set at project level
if (!SetPropertyValueByName(projConfig, itemTypeName, propertyName, propertyValue))
return;
// -> remove old property from each item
foreach (var change in changeGroup) {
if (!DeletePropertyByName(
change.PropertyStorage,
change.ItemTypeName,
change.PropertyName)) {
break;
}
}
} else {
//different property values per item
// -> set at each item
foreach (var change in changeGroup) {
if (GetPropertyValueByName(projConfig, change.ItemTypeName,
change.PropertyName) == change.PropertyValue) continue;
if (!SetPropertyValueByName(change.PropertyStorage, change.ItemTypeName,
change.PropertyName, change.PropertyValue)) {
break;
}
}
}
}
public bool EndSetItemProperties()
{
if (!pendingChanges)
return false;
var changeGroupsNormal = itemPropertyChangesGrouped.Values
.Where(x => x.Any() && !x.First().IsMocSource);
foreach (var changeGroup in changeGroupsNormal)
SetGroupItemProperties(changeGroup);
var changeGroupsMocSource = itemPropertyChangesGrouped.Values
.Where(x => x.Any() && x.First().IsMocSource);
foreach (var changeGroup in changeGroupsMocSource)
SetGroupItemProperties(changeGroup);
itemPropertyChanges.Clear();
itemPropertyChangesGrouped.Clear();
pendingChanges = false;
return true;
}
private string GetPropertyChangedValue(string configName, string itemTypeName,
string itemName, string propertyName)
{
if (!pendingChanges)
return null;
var change = new ItemPropertyChange
{
ConfigName = configName,
ItemTypeName = itemTypeName,
ItemName = itemName,
PropertyName = propertyName
};
return itemPropertyChanges.TryGetValue(change.Key, out change)
? change.PropertyValue : null;
}
public string GetPropertyChangedValue(QtMoc.Property property, string itemName,
string configName)
{
return GetPropertyChangedValue(configName, QtMoc.ItemTypeName, itemName,
property.ToString());
}
public bool SetCommandLine(string itemType, object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
return itemType switch
{
QtMoc.ItemTypeName => SetQtMocCommandLine(propertyStorage, commandLine, macros),
QtRcc.ItemTypeName => SetQtRccCommandLine(propertyStorage, commandLine, macros),
QtRepc.ItemTypeName => SetQtRepcCommandLine(propertyStorage, commandLine, macros),
QtUic.ItemTypeName => SetQtUicCommandLine(propertyStorage, commandLine, macros),
QtLRelease.ItemTypeName => SetQtLReleaseCommandLine(propertyStorage, commandLine, macros),
_ => false
};
}
#region QtMoc
private static QtMoc _qtMocInstance;
public static QtMoc QtMocInstance => _qtMocInstance ??= new QtMoc();
public bool SetItemProperty(object propertyStorage, QtMoc.Property property,
string propertyValue)
{
return SetItemPropertyByName(propertyStorage, property.ToString(), propertyValue);
}
private bool SetQtMocCommandLine(object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
if (!QtMocInstance.ParseCommandLine(commandLine, macros, out var properties))
return false;
return properties.All(property => SetItemProperty(propertyStorage, property.Key,
property.Value));
}
#endregion
#region QtRcc
private static QtRcc _qtRccInstance;
private static QtRcc QtRccInstance => _qtRccInstance ??= new QtRcc();
public bool SetItemProperty(object propertyStorage, QtRcc.Property property,
string propertyValue)
{
return SetItemPropertyByName(propertyStorage, property.ToString(), propertyValue);
}
private bool SetQtRccCommandLine(object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
if (!QtRccInstance.ParseCommandLine(commandLine, macros, out var properties))
return false;
return properties.All(property => SetItemProperty(propertyStorage, property.Key,
property.Value));
}
#endregion
#region QtRepc
private static QtRepc _qtRepcInstance;
private static QtRepc QtRepcInstance => _qtRepcInstance ??= new QtRepc();
public bool SetItemProperty(object propertyStorage, QtRepc.Property property,
string propertyValue)
{
return SetItemPropertyByName(propertyStorage, property.ToString(), propertyValue);
}
private bool SetQtRepcCommandLine(object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
if (!QtRepcInstance.ParseCommandLine(commandLine, macros, out var properties))
return false;
return properties.All(property => SetItemProperty(propertyStorage, property.Key,
property.Value));
}
#endregion
#region QtUic
private static QtUic _qtUicInstance;
private static QtUic QtUicInstance => _qtUicInstance ??= new QtUic();
public bool SetItemProperty(object propertyStorage, QtUic.Property property,
string propertyValue)
{
return SetItemPropertyByName(propertyStorage, property.ToString(), propertyValue);
}
private bool SetQtUicCommandLine(object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
if (!QtUicInstance.ParseCommandLine(commandLine, macros, out var properties))
return false;
return properties.All(property => SetItemProperty(propertyStorage, property.Key,
property.Value));
}
#endregion
#region QtLRelease
private static QtLRelease _qtLReleaseInstance;
private static QtLRelease QtLReleaseInstance => _qtLReleaseInstance ??= new QtLRelease();
public bool SetItemProperty(object propertyStorage, QtLRelease.Property property,
string propertyValue)
{
return SetItemPropertyByName(propertyStorage, property.ToString(), propertyValue);
}
public bool SetQtLReleaseCommandLine(object propertyStorage, string commandLine,
IVsMacroExpander macros)
{
if (!QtLReleaseInstance.ParseCommandLine(commandLine, macros, out var properties))
return false;
return properties.All(property => SetItemProperty(propertyStorage, property.Key,
property.Value));
}
#endregion
}
}
|