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
|
// 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;
using System.Collections.Generic;
using Microsoft.VisualStudio.VCProjectEngine;
namespace QtVsTools.Core.MsBuild
{
internal class VcPropertyStorageProvider : IPropertyStorageProvider
{
private string GetProperty(IVCRulePropertyStorage propertyStorage, string propertyName)
{
return propertyStorage?.GetUnevaluatedPropertyValue(propertyName) ?? "";
}
public string GetProperty(object obj, string itemType, string propertyName)
{
switch (obj) {
case VCFileConfiguration { Tool: IVCRulePropertyStorage propertyStorage }:
return GetProperty(propertyStorage, propertyName);
case VCConfiguration vcConfiguration: {
var ruleName = GetRuleName(vcConfiguration, itemType);
return GetProperty(vcConfiguration.Rules.Item(ruleName) as IVCRulePropertyStorage,
propertyName);
}
default:
return "";
}
}
private static bool SetProperty(IVCRulePropertyStorage storage, string name, string value)
{
if (storage?.GetUnevaluatedPropertyValue(name) != value)
storage?.SetPropertyValue(name, value);
return storage != null;
}
public bool SetProperty(object propertyStorage, string itemType, string propertyName,
string propertyValue)
{
switch (propertyStorage) {
case VCFileConfiguration { Tool: IVCRulePropertyStorage storage }:
return SetProperty(storage, propertyName, propertyValue);
case VCConfiguration vcConfiguration:
var ruleName = GetRuleName(vcConfiguration, itemType);
return SetProperty(vcConfiguration.Rules.Item(ruleName) as IVCRulePropertyStorage,
propertyName, propertyValue);
}
return false;
}
private static bool DeleteProperty(IVCRulePropertyStorage propertyStorage, string name)
{
propertyStorage?.DeleteProperty(name);
return propertyStorage != null;
}
public bool DeleteProperty(object propertyStorage, string itemType, string propertyName)
{
switch (propertyStorage) {
case VCFileConfiguration { Tool: IVCRulePropertyStorage storage }:
return DeleteProperty(storage, propertyName);
case VCConfiguration vcConfiguration:
var ruleName = GetRuleName(vcConfiguration, itemType);
return DeleteProperty(vcConfiguration.Rules.Item(ruleName) as IVCRulePropertyStorage,
propertyName);
}
return false;
}
public string GetConfigName(object propertyStorage)
{
return propertyStorage switch
{
VCFileConfiguration vcFileConfiguration => vcFileConfiguration.Name,
VCConfiguration vcConfiguration => vcConfiguration.Name,
_ => ""
};
}
public string GetItemType(object propertyStorage)
{
return propertyStorage is VCFileConfiguration { File: VCFile vcFile }
? vcFile.ItemType : "";
}
public string GetItemName(object propertyStorage)
{
return propertyStorage is VCFileConfiguration { File: VCFile vcFile }
? vcFile.Name : "";
}
public object GetParentProject(object propertyStorage)
{
return propertyStorage switch
{
VCFileConfiguration { ProjectConfiguration: VCConfiguration projectConfiguration }
=> projectConfiguration.project as VCProject,
VCConfiguration storage => storage.project as VCProject,
_ => null
};
}
public object GetProjectConfiguration(object project, string configName)
{
var vcProject = project as VCProject;
if (vcProject?.Configurations is not IVCCollection configurations)
return null;
foreach (VCConfiguration projConfig in configurations) {
if (projConfig.Name == configName)
return projConfig;
}
return null;
}
public IEnumerable<object> GetItems(object project, string itemType, string configName = "")
{
var items = new List<VCFileConfiguration>();
var vcProject = project as VCProject;
if (vcProject?.GetFilesWithItemType(itemType) is not IVCCollection allItems)
return items;
foreach (VCFile vcFile in allItems) {
if (vcFile.FileConfigurations is not IVCCollection fileConfigurations)
continue;
foreach (VCFileConfiguration fileConfiguration in fileConfigurations) {
if (!string.IsNullOrEmpty(configName) && fileConfiguration.Name != configName)
continue;
items.Add(fileConfiguration);
}
}
return items;
}
private static string GetRuleName(VCConfiguration config, string itemType)
{
if (config == null)
return string.Empty;
try {
return config.GetEvaluatedPropertyValue(itemType + "RuleName");
} catch (Exception exception) {
exception.Log();
return string.Empty;
}
}
}
}
|