-
Notifications
You must be signed in to change notification settings - Fork 695
/
VsProjectAdapter.cs
411 lines (329 loc) · 13.2 KB
/
VsProjectAdapter.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.ProjectManagement;
using NuGet.RuntimeModel;
using NuGet.VisualStudio;
namespace NuGet.PackageManagement.VisualStudio
{
[DebuggerDisplay("{ProjectName}")]
internal class VsProjectAdapter : IVsProjectAdapter
{
#region Private members
private readonly VsHierarchyItem _vsHierarchyItem;
private readonly Lazy<EnvDTE.Project> _dteProject;
private readonly IDeferredProjectWorkspaceService _workspaceService;
private readonly IVsProjectThreadingService _threadingService;
private readonly string _projectTypeGuid;
#endregion Private members
#region Properties
public string BaseIntermediateOutputPath
{
get
{
var baseIntermediateOutputPath = BuildProperties.GetPropertyValue(ProjectBuildProperties.BaseIntermediateOutputPath);
if (string.IsNullOrEmpty(baseIntermediateOutputPath))
{
return null;
}
return Path.Combine(ProjectDirectory, baseIntermediateOutputPath);
}
}
public string RestorePackagesPath
{
get
{
var restorePackagesPath = BuildProperties.GetPropertyValue(ProjectBuildProperties.RestorePackagesPath);
if (string.IsNullOrWhiteSpace(restorePackagesPath))
{
return null;
}
return restorePackagesPath;
}
}
public string RestoreSources
{
get
{
var restoreSources = BuildProperties.GetPropertyValue(ProjectBuildProperties.RestoreSources);
if (string.IsNullOrWhiteSpace(restoreSources))
{
return null;
}
return restoreSources;
}
}
public string RestoreFallbackFolders
{
get
{
var restoreFallbackFolders = BuildProperties.GetPropertyValue(ProjectBuildProperties.RestoreFallbackFolders);
if (string.IsNullOrWhiteSpace(restoreFallbackFolders))
{
return null;
}
return restoreFallbackFolders;
}
}
public IProjectBuildProperties BuildProperties { get; private set; }
public string CustomUniqueName => ProjectNames.CustomUniqueName;
public string FullName => ProjectNames.FullName;
public string ProjectDirectory
{
get
{
if (!IsDeferred)
{
return EnvDTEProjectInfoUtility.GetFullPath(Project);
}
else
{
return Path.GetDirectoryName(FullProjectPath);
}
}
}
public string FullProjectPath { get; private set; }
public bool IsDeferred
{
get
{
#if VS14
return false;
#else
_threadingService.ThrowIfNotOnUIThread();
object isDeferred;
if (ErrorHandler.Failed(VsHierarchy.GetProperty(
(uint)VSConstants.VSITEMID.Root,
(int)__VSHPROPID9.VSHPROPID_IsDeferred,
out isDeferred)))
{
return false;
}
return object.Equals(true, isDeferred);
#endif
}
}
public bool IsSupported
{
get
{
if (!IsDeferred)
{
return EnvDTEProjectUtility.IsSupported(Project);
}
return VsHierarchyUtility.IsSupported(VsHierarchy, _projectTypeGuid);
}
}
public string PackageTargetFallback
{
get
{
return BuildProperties.GetPropertyValue(ProjectBuildProperties.PackageTargetFallback);
}
}
public string AssetTargetFallback
{
get
{
return BuildProperties.GetPropertyValue(ProjectBuildProperties.AssetTargetFallback);
}
}
public EnvDTE.Project Project => _dteProject.Value;
public string ProjectId
{
get
{
Guid id;
if (!_vsHierarchyItem.TryGetProjectId(out id))
{
id = Guid.Empty;
}
return id.ToString();
}
}
public string ProjectName => ProjectNames.ShortName;
public ProjectNames ProjectNames { get; private set; }
public string UniqueName => ProjectNames.UniqueName;
public string Version
{
get
{
_threadingService.ThrowIfNotOnUIThread();
var packageVersion = BuildProperties.GetPropertyValue(ProjectBuildProperties.PackageVersion);
if (string.IsNullOrEmpty(packageVersion))
{
packageVersion = BuildProperties.GetPropertyValue(ProjectBuildProperties.Version);
if (string.IsNullOrEmpty(packageVersion))
{
packageVersion = "1.0.0";
}
}
return packageVersion;
}
}
public IVsHierarchy VsHierarchy => _vsHierarchyItem.VsHierarchy;
public string RestoreAdditionalProjectSources => BuildProperties.GetPropertyValue(ProjectBuildProperties.RestoreAdditionalProjectSources);
public string RestoreAdditionalProjectFallbackFolders => BuildProperties.GetPropertyValue(ProjectBuildProperties.RestoreAdditionalProjectFallbackFolders);
public string NoWarn => BuildProperties.GetPropertyValue(ProjectBuildProperties.NoWarn);
public string WarningsAsErrors => BuildProperties.GetPropertyValue(ProjectBuildProperties.WarningsAsErrors);
public string TreatWarningsAsErrors => BuildProperties.GetPropertyValue(ProjectBuildProperties.TreatWarningsAsErrors);
#endregion Properties
#region Constructors
public VsProjectAdapter(
VsHierarchyItem vsHierarchyItem,
ProjectNames projectNames,
string fullProjectPath,
string projectTypeGuid,
Func<IVsHierarchy, EnvDTE.Project> loadDteProject,
IProjectBuildProperties buildProperties,
IVsProjectThreadingService threadingService,
IDeferredProjectWorkspaceService workspaceService = null)
{
Assumes.Present(vsHierarchyItem);
_vsHierarchyItem = vsHierarchyItem;
_dteProject = new Lazy<EnvDTE.Project>(() => loadDteProject(_vsHierarchyItem.VsHierarchy));
_workspaceService = workspaceService;
_threadingService = threadingService;
_projectTypeGuid = projectTypeGuid;
FullProjectPath = fullProjectPath;
ProjectNames = projectNames;
BuildProperties = buildProperties;
}
#endregion Constructors
#region Getters
public async Task<string[]> GetProjectTypeGuidsAsync()
{
if (!IsDeferred)
{
return VsHierarchyUtility.GetProjectTypeGuids(Project);
}
else
{
// Get ProjectTypeGuids from msbuild property, if it doesn't exist, fall back to projectTypeGuid.
var projectTypeGuids = await BuildProperties.GetPropertyValueAsync(ProjectBuildProperties.ProjectTypeGuids);
if (!string.IsNullOrEmpty(projectTypeGuids))
{
return MSBuildStringUtility.Split(projectTypeGuids);
}
if (!string.IsNullOrEmpty(_projectTypeGuid))
{
return new string[] { _projectTypeGuid };
}
return new string[0];
}
}
public async Task<FrameworkName> GetDotNetFrameworkNameAsync()
{
var targetFrameworkMoniker = await GetTargetFrameworkStringAsync();
if (!string.IsNullOrEmpty(targetFrameworkMoniker))
{
return new FrameworkName(targetFrameworkMoniker);
}
return null;
}
public async Task<IEnumerable<string>> GetReferencedProjectsAsync()
{
if (!IsDeferred)
{
if (Project.Kind != null
&& SupportedProjectTypes.IsSupportedForAddingReferences(Project.Kind))
{
return EnvDTEProjectUtility.GetReferencedProjects(Project).Select(p => p.UniqueName);
}
return Enumerable.Empty<string>();
}
else
{
var projectTypeGuids = await GetProjectTypeGuidsAsync();
if (projectTypeGuids.All(SupportedProjectTypes.IsSupportedForAddingReferences))
{
return await _workspaceService.GetProjectReferencesAsync(FullProjectPath);
}
return Enumerable.Empty<string>();
}
}
public async Task<IEnumerable<RuntimeDescription>> GetRuntimeIdentifiersAsync()
{
_threadingService.ThrowIfNotOnUIThread();
var unparsedRuntimeIdentifer = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.RuntimeIdentifier);
var unparsedRuntimeIdentifers = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.RuntimeIdentifiers);
var runtimes = Enumerable.Empty<string>();
if (unparsedRuntimeIdentifer != null)
{
runtimes = runtimes.Concat(new[] { unparsedRuntimeIdentifer });
}
if (unparsedRuntimeIdentifers != null)
{
runtimes = runtimes.Concat(unparsedRuntimeIdentifers.Split(';'));
}
runtimes = runtimes
.Select(x => x.Trim())
.Where(x => !string.IsNullOrEmpty(x));
return runtimes
.Select(runtime => new RuntimeDescription(runtime));
}
public async Task<IEnumerable<CompatibilityProfile>> GetRuntimeSupportsAsync()
{
_threadingService.ThrowIfNotOnUIThread();
var unparsedRuntimeSupports = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.RuntimeSupports);
if (unparsedRuntimeSupports == null)
{
return Enumerable.Empty<CompatibilityProfile>();
}
return unparsedRuntimeSupports
.Split(';')
.Select(x => x.Trim())
.Where(x => !string.IsNullOrEmpty(x))
.Select(support => new CompatibilityProfile(support));
}
public async Task<NuGetFramework> GetTargetFrameworkAsync()
{
var frameworkString = await GetTargetFrameworkStringAsync();
if (!string.IsNullOrEmpty(frameworkString))
{
return NuGetFramework.Parse(frameworkString);
}
return NuGetFramework.UnsupportedFramework;
}
private async Task<string> GetTargetFrameworkStringAsync()
{
await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();
var projectPath = FullName;
var platformIdentifier = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.TargetPlatformIdentifier);
var platformVersion = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.TargetPlatformVersion);
var platformMinVersion = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.TargetPlatformMinVersion);
var targetFrameworkMoniker = await BuildProperties.GetPropertyValueAsync(
ProjectBuildProperties.TargetFrameworkMoniker);
// Projects supporting TargetFramework and TargetFrameworks are detected before
// this check. The values can be passed as null here.
var frameworkStrings = MSBuildProjectFrameworkUtility.GetProjectFrameworkStrings(
projectFilePath: projectPath,
targetFrameworks: null,
targetFramework: null,
targetFrameworkMoniker: targetFrameworkMoniker,
targetPlatformIdentifier: platformIdentifier,
targetPlatformVersion: platformVersion,
targetPlatformMinVersion: platformMinVersion);
return frameworkStrings.FirstOrDefault();
}
#endregion Getters
}
}