This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
/
ProjectUtility.cs
491 lines (425 loc) · 15.2 KB
/
ProjectUtility.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright 2018 Coati Software KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using VCProjectEngineWrapper;
namespace CoatiSoftware.SourcetrailExtension.Utility
{
public class ProjectUtility
{
public static List<string> GetPropertyNamesAndValues(Properties properties)
{
List<string> ret = new List<string>();
foreach (Property propertiy in properties)
{
string name = propertiy.Name;
string value = "";
try
{
value = propertiy.Value.ToString();
}
catch (Exception)
{
value = "Error occurred while converting value to string.";
}
ret.Add(name + ": " + value);
}
return ret;
}
public static bool HasProperty(Properties properties, string propertyName)
{
if (properties != null)
{
foreach (Property item in properties)
{
if (item != null && item.Name == propertyName)
{
return true;
}
}
}
return false;
}
public static bool ContainsCFiles(Project project)
{
List<ProjectItem> projectItems = GetProjectItems(project);
try
{
foreach (EnvDTE.ProjectItem item in projectItems)
{
if (item.FileCodeModel != null && item.FileCodeModel.Language == CodeModelLanguageConstants.vsCMLanguageVC)
{
string extension = item.Properties.Item("Extension").Value.ToString();
if (extension == ".c")
{
return true;
}
}
}
}
catch(Exception e)
{
Logging.Logging.LogError("Exception: " + e.Message);
}
return false;
}
static public List<ProjectItem> GetProjectItems(Project project)
{
List<ProjectItem> items = new List<ProjectItem>();
IEnumerator itemEnumerator = project.ProjectItems.GetEnumerator();
while (itemEnumerator.MoveNext())
{
ProjectItem currentItem = (ProjectItem)itemEnumerator.Current;
items.Add(GetProjectSubItemsRecursive(currentItem, ref items));
}
return items;
}
static private ProjectItem GetProjectSubItemsRecursive(ProjectItem item, ref List<ProjectItem> projectItems)
{
if (item.ProjectItems == null)
{
return item;
}
IEnumerator items = item.ProjectItems.GetEnumerator();
while (items.MoveNext())
{
ProjectItem currentItem = (ProjectItem)items.Current;
projectItems.Add(GetProjectSubItemsRecursive(currentItem, ref projectItems));
}
return item;
}
static private List<string> CleanIncludeDirectories(IVCProjectWrapper project, IPathResolver pathResolver, List<string> includeDirectories)
{
return includeDirectories
.Select(x => x.Replace("\\\"", ""))
.Select(x => x.Replace("\"", ""))
.Select(x => pathResolver.GetAsAbsoluteCanonicalPath(x, project))
.Select(x => x.Replace("\\", "/")) // backslashes would cause some string-escaping hassles...
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
static public List<string> GetProjectIncludeDirectories(IVCProjectWrapper project, IVCConfigurationWrapper vcProjectConfig, IPathResolver pathResolver)
{
// get additional include directories
// source: http://www.mztools.com/articles/2014/MZ2014005.aspx
Logging.Logging.LogInfo("Attempting to retrieve project Include Directories for project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.GetName()) + "'");
List<string> includeDirectories = new List<string>();
if (vcProjectConfig != null && vcProjectConfig.isValid())
{
if (vcProjectConfig.GetCLCompilerTool() != null && vcProjectConfig.GetCLCompilerTool().isValid())
{
includeDirectories.AddRange(GetIncludeDirectories(vcProjectConfig.GetCLCompilerTool()));
}
else if (vcProjectConfig.GetNMakeTool() != null && vcProjectConfig.GetNMakeTool().isValid())
{
includeDirectories.AddRange(GetIncludeDirectories(vcProjectConfig.GetNMakeTool()));
}
}
else
{
Logging.Logging.LogWarning("Could not retrieve Project Configuration. No include directories could be retrieved.");
return new List<string>();
}
Logging.Logging.LogInfo("Attempting to clean up.");
includeDirectories = CleanIncludeDirectories(project, pathResolver, includeDirectories);
Logging.Logging.LogInfo("Found " + includeDirectories.Count.ToString() + " distinct project include directories.");
return includeDirectories;
}
static public List<string> GetSystemIncludeDirectories(IVCProjectWrapper project, IVCConfigurationWrapper vcProjectConfig, IPathResolver pathResolver)
{
Logging.Logging.LogInfo("Attempting to retrieve system Include Directories for project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.GetName()) + "'");
List<string> includeDirectories = new List<string>();
try
{
IVCPlatformWrapper platform = vcProjectConfig.GetPlatform();
if (platform != null && platform.isValid())
{
foreach (string directory in platform.GetIncludeDirectories())
{
includeDirectories.AddRange(pathResolver.ResolveVsMacroInPath(directory, vcProjectConfig));
}
}
}
catch (Exception e)
{
Logging.Logging.LogError("Failed to retrieve platform include directories: " + e.Message);
return new List<string>();
}
Logging.Logging.LogInfo("Attempting to clean up.");
includeDirectories = CleanIncludeDirectories(project, pathResolver, includeDirectories);
Logging.Logging.LogInfo("Found " + includeDirectories.Count.ToString() + " distinct system include directories.");
return includeDirectories;
}
static private List<string> GetIncludeDirectories(IVCCLCompilerToolWrapper compilerTool)
{
List<string> includeDirectories = new List<string>();
if (compilerTool != null && compilerTool.isValid())
{
includeDirectories.AddRange(compilerTool.GetAdditionalIncludeDirectories());
}
else
{
Logging.Logging.LogWarning("No CL Compiler Tool provided");
}
return includeDirectories;
}
static private List<string> GetIncludeDirectories(IVCNMakeToolWrapper nmakeTool)
{
List<string> includeDirectories = new List<string>();
if (nmakeTool != null && nmakeTool.isValid())
{
includeDirectories.AddRange(nmakeTool.GetIncludeSearchPaths());
}
else
{
Logging.Logging.LogWarning("No NMake Tool provided");
}
return includeDirectories;
}
static private List<string> GetIncludeDirectories(IVCResourceCompilerToolWrapper compilerTool)
{
List<string> includeDirectories = new List<string>();
if (compilerTool != null && compilerTool.isValid())
{
includeDirectories.AddRange(compilerTool.GetAdditionalIncludeDirectories());
}
else
{
Logging.Logging.LogWarning("No Resource Compiler Tool provided");
}
return includeDirectories;
}
static public List<string> GetPreprocessorDefinitions(IVCProjectWrapper project, IVCConfigurationWrapper vcProjectConfiguration)
{
Logging.Logging.LogInfo("Attempting to retrieve Preprocessor Definitions for project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.GetName()) + "'");
List<string> preprocessorDefinitions = new List<string>();
if (vcProjectConfiguration != null && vcProjectConfiguration.isValid())
{
if (vcProjectConfiguration.GetCLCompilerTool() != null && vcProjectConfiguration.GetCLCompilerTool().isValid())
{
preprocessorDefinitions = GetPreprocessorDefinitions(vcProjectConfiguration.GetCLCompilerTool());
}
else if (vcProjectConfiguration.GetNMakeTool() != null && vcProjectConfiguration.GetNMakeTool().isValid())
{
preprocessorDefinitions = GetPreprocessorDefinitions(vcProjectConfiguration.GetNMakeTool());
}
}
else
{
Logging.Logging.LogWarning("Could not retrieve project configuration.");
}
Logging.Logging.LogInfo("Found " + preprocessorDefinitions.Count.ToString() + " distinct preprocessor definitions.");
return preprocessorDefinitions;
}
static private List<string> GetPreprocessorDefinitions(IVCCLCompilerToolWrapper compilerTool)
{
List<string> preprocessorDefinitions = new List<string>();
if (compilerTool != null && compilerTool.isValid())
{
preprocessorDefinitions = compilerTool.GetPreprocessorDefinitions()
.Select(x => x.Replace("\\\"", "\""))
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
else
{
Logging.Logging.LogWarning("No CL Compiler Tool provided");
}
return preprocessorDefinitions;
}
static private List<string> GetPreprocessorDefinitions(IVCNMakeToolWrapper nmakeTool)
{
List<string> preprocessorDefinitions = new List<string>();
if (nmakeTool != null && nmakeTool.isValid())
{
preprocessorDefinitions = nmakeTool.GetPreprocessorDefinitions()
.Select(x => x.Replace("\\\"", "\""))
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
else
{
Logging.Logging.LogWarning("No NMake Tool provided");
}
return preprocessorDefinitions;
}
static private List<string> GetPreprocessorDefinitions(IVCResourceCompilerToolWrapper compilerTool)
{
List<string> preprocessorDefinitions = new List<string>();
if (compilerTool != null && compilerTool.isValid())
{
preprocessorDefinitions = compilerTool.GetPreprocessorDefinitions()
.Select(x => x.Replace("\\\"", "\""))
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
else
{
Logging.Logging.LogWarning("No Resource Compiler Tool provided");
}
return preprocessorDefinitions;
}
static public List<string> GetForcedIncludeFiles(IVCProjectWrapper project, IVCConfigurationWrapper vcProjectConfiguration, IPathResolver pathResolver)
{
Logging.Logging.LogInfo("Attempting to retrieve Forced Include Files for project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.GetName()) + "'");
List<string> forcedIncludeFiles = new List<string>();
if (vcProjectConfiguration != null && vcProjectConfiguration.isValid())
{
if (vcProjectConfiguration.GetCLCompilerTool() != null && vcProjectConfiguration.GetCLCompilerTool().isValid())
{
forcedIncludeFiles = vcProjectConfiguration.GetCLCompilerTool().GetForcedIncludeFiles()
.Select(x => x.Replace("\\", "/")) // backslashes would cause some string-escaping hassles...
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
else if(vcProjectConfiguration.GetNMakeTool() != null && vcProjectConfiguration.GetNMakeTool().isValid())
{
forcedIncludeFiles = vcProjectConfiguration.GetNMakeTool().GetForcedIncludes()
.Select(x => x.Replace("\\", "/")) // backslashes would cause some string-escaping hassles...
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToList();
}
}
Logging.Logging.LogInfo("Found " + forcedIncludeFiles.Count.ToString() + " distinct forced include files.");
return forcedIncludeFiles;
}
// AFAIK there is no way to programmatically get the c++ standard version supported by any given VS version
// instead I'm refearing to the VS docu for that information
// https://msdn.microsoft.com/en-us/library/hh567368.aspx
static public string GetCppStandardForProject(IVCProjectWrapper project)
{
string result = "";
string vsVersion = project.GetWrappedVersion();
string justNumbers = new String(vsVersion.Where(Char.IsDigit).ToArray());
int versionNumber = int.Parse(justNumbers);
if (versionNumber < 120) // version 11 (2012)
{
result = "c++11";
}
else if (versionNumber < 130) // version 12 (2013)
{
result = "c++14";
}
else if (versionNumber < 150) // version 14 (2015)
{
result = "c++14";
}
else if (versionNumber < 160) // version 15 (2017)
{
result = "c++14";
}
else if (versionNumber < 170) // version 16 (2019)
{
result = "c++17";
}
else // fallback
{
result = "c++17";
}
return result;
}
// returns a valid Guid if the project was reloaded or an empty Guid if the project did not need to be reloaded
static public Guid ReloadProject(Project project)
{
Logging.Logging.LogInfo("Attempting to reload project");
try
{
if (project != null && project.Kind == EnvDTE.Constants.vsProjectKindUnmodeled)
{
DTE dte = project.DTE;
ServiceProvider sp = new ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
IVsSolution vsSolution = sp.GetService(typeof(IVsSolution)) as IVsSolution;
IVsHierarchy hierarchy;
string solutionDirectory = "";
string solutionFile = "";
string userOptions = "";
vsSolution.GetSolutionInfo(out solutionDirectory, out solutionFile, out userOptions);
vsSolution.GetProjectOfUniqueName(solutionDirectory + project.UniqueName, out hierarchy);
if (hierarchy != null)
{
Guid projectGuid;
hierarchy.GetGuidProperty(
VSConstants.VSITEMID_ROOT,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out projectGuid);
if (projectGuid != null)
{
Logging.Logging.LogInfo("Project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "' with GUID {" + projectGuid.ToString() + "} loaded.");
(vsSolution as IVsSolution4).ReloadProject(projectGuid);
return projectGuid;
}
else
{
Logging.Logging.LogError("Failed to load project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "'");
}
}
else
{
Logging.Logging.LogError("Failed to retrieve IVsHierarchy. Can't load project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "'");
}
}
else
{
if (project == null)
{
Logging.Logging.LogWarning("Project is null");
}
else
{
Logging.Logging.LogInfo("Project '" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "' is already loaded");
}
}
}
catch(Exception e)
{
Logging.Logging.LogError("Exception: " + e.Message);
return Guid.Empty;
}
return Guid.Empty;
}
static public void UnloadProject(Guid guid, DTE dte)
{
Logging.Logging.LogInfo("Attempting to unload project with GUID {" + guid.ToString() + "}");
if (dte == null)
{
return;
}
try
{
ServiceProvider sp = new ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
IVsSolution vsSolution = sp.GetService(typeof(SVsSolution)) as IVsSolution;
(vsSolution as IVsSolution4).UnloadProject(guid, (uint)_VSProjectUnloadStatus.UNLOADSTATUS_UnloadedByUser);
Logging.Logging.LogInfo("Done unloading project with GUID {" + guid.ToString() + "}");
}
catch(Exception e)
{
Logging.Logging.LogError("Exception: " + e.Message);
}
}
}
}