forked from tomchavakis/nuget-license
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMsBuildAbstraction.cs
59 lines (48 loc) · 2.04 KB
/
MsBuildAbstraction.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
// Licensed to the projects contributors.
// The license conditions are provided in the LICENSE file located in the project root
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
using Microsoft.VisualStudio.SolutionPersistence;
using Microsoft.VisualStudio.SolutionPersistence.Serializer;
namespace NuGetUtility.Wrapper.MsBuildWrapper
{
public class MsBuildAbstraction : IMsBuildAbstraction
{
private ProjectCollection? _projects;
private ProjectCollection Projects => _projects ??= InitializeProjectCollection();
public MsBuildAbstraction()
{
RegisterMsBuildLocatorIfNeeded();
}
public IProject GetProject(string projectPath)
{
#if !NETFRAMEWORK
if (projectPath.EndsWith("vcxproj"))
{
throw new MsBuildAbstractionException($"Please use the .net Framework version to analyze c++ projects (Project: {projectPath})");
}
#endif
Project project = Projects.LoadProject(projectPath);
return new ProjectWrapper(project);
}
public async Task<IEnumerable<string>> GetProjectsFromSolutionAsync(string inputPath)
{
ISolutionSerializer serializer = SolutionSerializers.GetSerializerByMoniker(inputPath) ?? throw new MsBuildAbstractionException("Failed to determine serializer for solution");
Microsoft.VisualStudio.SolutionPersistence.Model.SolutionModel model = await serializer.OpenAsync(inputPath, CancellationToken.None);
return model.SolutionProjects.Select(p => p.FilePath);
}
private static void RegisterMsBuildLocatorIfNeeded()
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
}
private static ProjectCollection InitializeProjectCollection()
{
ProjectCollection collection = ProjectCollection.GlobalProjectCollection;
collection.UnloadAllProjects();
return collection;
}
}
}