-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple.CodeParser.CodeWorkSpace.cs
More file actions
113 lines (88 loc) · 3.1 KB
/
Simple.CodeParser.CodeWorkSpace.cs
File metadata and controls
113 lines (88 loc) · 3.1 KB
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
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.MSBuild;
namespace Simple.CodeParser;
/// <summary>
/// Workspace to start working with
/// </summary>
public static class CodeWorkSpace {
#region Algorithm
private static void CoreLoad() {
if (MSBuildLocator.IsRegistered)
return;
var studio = MSBuildLocator
.QueryVisualStudioInstances()
.Where(instance => instance.Name.Contains(".NET Core SDK"))
.MaxBy(instance => instance.Version);
MSBuildLocator.RegisterInstance(studio);
VisualStudio = studio;
}
#endregion Algorithm
#region Create
static CodeWorkSpace() {
CoreLoad();
BuildWorkSpace = MSBuildWorkspace.Create();
}
#endregion Create
#region Public
/// <summary>
/// Visual Studio
/// </summary>
public static VisualStudioInstance? VisualStudio { get; private set; }
/// <summary>
/// Build Work Space
/// </summary>
public static MSBuildWorkspace BuildWorkSpace { get; }
/// <summary>
/// Open Solution
/// </summary>
/// <param name="solutionFileName">Solution File Name (.sln)</param>
/// <returns>Solution Opened</returns>
public static async Task<Solution> OpenSolutionAsync(string solutionFileName) =>
await BuildWorkSpace.OpenSolutionAsync(solutionFileName);
/// <summary>
/// Parse File
/// </summary>
/// <param name="filePath">File to Parse</param>
/// <returns>Parsed File</returns>
/// <exception cref="ArgumentNullException">When filePath is null</exception>
public static SyntaxNode ParseFile(string filePath) => filePath is not null
? CSharpSyntaxTree.ParseText(File.ReadAllText(filePath)).GetRoot()
: throw new ArgumentNullException(nameof(filePath));
/// <summary>
/// Parse File
/// </summary>
/// <param name="filePath">File to Parse</param>
/// <returns>Parsed File</returns>
/// <exception cref="ArgumentNullException">When filePath is null</exception>
public static async Task<SyntaxNode> ParseFileAsync(string filePath) {
if (filePath is null)
throw new ArgumentNullException(nameof(filePath));
string text = await File.ReadAllTextAsync(filePath);
return await Task.Run(() => CSharpSyntaxTree.ParseText(text).GetRoot());
}
/// <summary>
/// Parse File
/// </summary>
/// <param name="text">Text to Parse</param>
/// <returns>Parsed File</returns>
/// <exception cref="ArgumentNullException">When text is null</exception>
public static SyntaxNode ParseText(string text) {
if (text is null)
throw new ArgumentNullException(nameof(text));
return CSharpSyntaxTree.ParseText(text).GetRoot();
}
/// <summary>
/// Parse File
/// </summary>
/// <param name="text">Text to Parse</param>
/// <returns>Parsed File</returns>
/// <exception cref="ArgumentNullException">When text is null</exception>
public static Task<SyntaxNode> ParseTextAsync(string text) {
if (text is null)
throw new ArgumentNullException(nameof(text));
return Task.Run(() => CSharpSyntaxTree.ParseText(text).GetRoot());
}
#endregion Public
}