-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
311 lines (276 loc) · 16.1 KB
/
Program.cs
File metadata and controls
311 lines (276 loc) · 16.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
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
using System.CommandLine;
using System.Diagnostics;
using Essential.Diagnostics;
using LSP2GXL.LSP;
using LSP2GXL.Model;
using LSP2GXL.Utils;
using ShellProgressBar;
namespace LSP2GXL;
public class Program
{
private static string? GetExecutablePath(string executableName)
{
if (File.Exists(executableName))
{
return Path.GetFullPath(executableName);
}
string[] paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator) ?? [];
return paths.Select(path => Path.Combine(path, executableName)).FirstOrDefault(File.Exists);
}
private static async Task<int> Main(string[] args)
{
ColoredConsoleTraceListener listener = new(false);
listener.Template = "{EventType}: {Message}";
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
Argument<DirectoryInfo> projectRootArgument = new(name: "project",
description: "Path to the root of the project that shall be analyzed.");
Option<FileInfo?> outputFileOption = new(aliases: ["-o", "--output"],
description: "The output GXL file to write to. "
+ "If not given, the output graph will be discarded.");
Option<bool> overwriteOption = new(name: "--overwrite",
description: "Whether to overwrite the output file if it already exists.");
Option<LSPServer> lspServerOption = new(aliases: ["-l", "--lsp-server"],
description: "The LSP server to use.",
parseArgument: result =>
{
string name = result.Tokens.Single().Value;
LSPServer? server = LSPServer.GetByName(name);
if (server == null)
{
string availableServers = string.Join(", ", LSPServer.All.Select(x => x.Name));
result.ErrorMessage = "The given LSP server does not exist. "
+ "Available servers are: " + availableServers;
return LSPServer.All.First(); // Ignored
}
else
{
return server;
}
})
{ IsRequired = true };
Option<FileInfo?> lspServerPathOption = new(aliases: ["-x", "--lsp-server-executable"],
description: "The path to the executable of the LSP server. "
+ "Note that the given server must match the configured language server.");
Option<DirectoryInfo[]> sourcePathsOption = new(aliases: ["-s", "--source-path"],
description: "The path to a directory (under the project root) whose contents shall be analyzed. "
+ "If this is not given, we will query the whole project root.");
Option<DirectoryInfo[]> excludedSourcePathsOption = new(aliases: ["-e", "--exclude-source-path"],
description: "The path to a directory (under the project root) whose contents shall NOT be analyzed. ");
Option<bool> logLspOption = new(name: "--log-lsp",
description: $"Whether to log the raw LSP input and output to a temporary file at {Path.GetTempPath()}.");
Option<float> timeoutOption = new(name: "--timeout",
description: "The maximum time in seconds to wait for the LSP server to respond to any given query.",
getDefaultValue: () => 10);
Option<EdgeKind[]> edgeTypesOption = new(name: "--edge-type",
description: "LSP edge type to include in the import.",
getDefaultValue: () => [EdgeKind.All & ~(EdgeKind.Definition | EdgeKind.Declaration)]);
Option<bool> selfReferencesOption = new(name: "--self-references",
description: "Whether to allow self-references in the generated graph.");
Option<bool> parentReferencesOption = new(name: "--parent-references",
description: "Whether to allow references from an element to its direct parent in the generated graph.");
Option<NodeKind[]> nodeTypesOption = new(name: "--node-type",
description: "LSP node type to include in the import.",
getDefaultValue: () => [NodeKind.All]);
Option<DiagnosticKind[]> diagnosticLevelsOption = new(name: "--diagnostic-level",
description: "LSP diagnostic level to include in the import.",
getDefaultValue: () => [DiagnosticKind.All]);
Option<uint?> parallelismOption = new(aliases: ["-j", "--parallel-tasks"],
description: "The number of parallel tasks to use for the edge phase of the import.");
Option<bool> unoptimizedEdgeConnectionOption = new(name: "--unoptimized-edges",
description: "Whether to disable the optimized (kd-tree based) "
+ "edge connection algorithm.");
Option<FileInfo> performanceOutputFileOption = new(aliases: ["-p", "--performance-output"],
description: "The output file to write CSV performance information to. Must not exist yet.",
getDefaultValue: () => new FileInfo("performance.csv"));
projectRootArgument.AddValidator(result =>
{
if (!result.GetArgumentValueOrDefault(projectRootArgument)?.Exists ?? false)
{
result.ErrorMessage = "The given project root does not exist.";
}
});
outputFileOption.AddValidator(result =>
{
FileInfo? outputFile = result.GetValueForOption(outputFileOption);
if ((outputFile?.Exists ?? false) && !result.GetValueForOption(overwriteOption))
{
result.ErrorMessage = "The given output file already exists. Use --overwrite to overwrite it.";
}
});
sourcePathsOption.AddValidator(result =>
{
DirectoryInfo[] sourcePaths = result.GetOptionValueOrDefault(sourcePathsOption) ?? [];
foreach (DirectoryInfo sourcePath in sourcePaths)
{
if (!sourcePath.Exists)
{
result.ErrorMessage = $"The given source path '{sourcePath}' does not exist.";
}
}
});
excludedSourcePathsOption.AddValidator(result =>
{
DirectoryInfo[] excludedSourcePaths = result.GetOptionValueOrDefault(excludedSourcePathsOption) ?? [];
foreach (DirectoryInfo excludedSourcePath in excludedSourcePaths)
{
if (!excludedSourcePath.Exists)
{
result.ErrorMessage = $"The given excluded source path '{excludedSourcePath}' does not exist.";
}
}
});
timeoutOption.AddValidator(result =>
{
float timeout = result.GetValueForOption(timeoutOption);
if (timeout < 0)
{
result.ErrorMessage = "The timeout must be a non-negative number.";
}
});
performanceOutputFileOption.AddValidator(result =>
{
FileInfo? performanceOutput = result.GetOptionValueOrDefault(performanceOutputFileOption);
if (performanceOutput?.Exists ?? false)
{
result.ErrorMessage = "The performance output file already exists. Please choose a different name.";
}
});
RootCommand rootCommand = new("A tool that transforms LSP project information into a GXL file.")
{
projectRootArgument,
outputFileOption,
overwriteOption,
lspServerOption,
lspServerPathOption,
logLspOption,
sourcePathsOption,
excludedSourcePathsOption,
timeoutOption,
edgeTypesOption,
selfReferencesOption,
parentReferencesOption,
nodeTypesOption,
diagnosticLevelsOption,
parallelismOption,
unoptimizedEdgeConnectionOption,
performanceOutputFileOption,
};
int returnCode = 1;
rootCommand.SetHandler(async context =>
{
DirectoryInfo projectRoot = context.ParseResult.GetValueForArgument(projectRootArgument);
FileInfo? outputFile = context.ParseResult.GetValueForOption(outputFileOption);
LSPServer lspServer = context.ParseResult.GetValueForOption(lspServerOption)!;
FileInfo? lspServerPath = context.ParseResult.GetValueForOption(lspServerPathOption);
bool logLsp = context.ParseResult.GetValueForOption(logLspOption);
DirectoryInfo[] sourcePaths = context.ParseResult.GetValueForOption(sourcePathsOption) ?? [];
DirectoryInfo[] excludedSourcePaths = context.ParseResult.GetValueForOption(excludedSourcePathsOption) ?? [];
float timeout = context.ParseResult.GetValueForOption(timeoutOption);
EdgeKind edgeTypes = context.ParseResult.GetValueForOption(edgeTypesOption)!.Aggregate((a, b) => a | b);
bool selfReferences = context.ParseResult.GetValueForOption(selfReferencesOption);
bool parentReferences = context.ParseResult.GetValueForOption(parentReferencesOption);
NodeKind nodeTypes = context.ParseResult.GetValueForOption(nodeTypesOption)!.Aggregate((a, b) => a | b);
DiagnosticKind diagnosticLevels = context.ParseResult.GetValueForOption(diagnosticLevelsOption)!.Aggregate((a, b) => a | b);
uint? parallelism = context.ParseResult.GetValueForOption(parallelismOption);
bool unoptimizedEdgeConnection = context.ParseResult.GetValueForOption(unoptimizedEdgeConnectionOption);
FileInfo performanceOutputFile = context.ParseResult.GetValueForOption(performanceOutputFileOption)!;
returnCode = await ConvertAsync(projectRoot, outputFile, lspServer, lspServerPath, logLsp, sourcePaths,
excludedSourcePaths, timeout, edgeTypes, selfReferences, parentReferences,
nodeTypes, diagnosticLevels, unoptimizedEdgeConnection, parallelism,
performanceOutputFile, context.GetCancellationToken());
});
await rootCommand.InvokeAsync(args);
return returnCode;
}
private static async Task<int> ConvertAsync(DirectoryInfo project, FileInfo? outputFile,
LSPServer lspServer, FileInfo? lspServerPath, bool logLsp,
DirectoryInfo[] sourcePaths, DirectoryInfo[] excludedSourcePaths,
float timeout, EdgeKind edgeTypes, bool selfReferences,
bool parentReferences, NodeKind nodeTypes, DiagnosticKind diagnosticLevels,
bool unoptimizedEdgeConnection, uint? parallelism,
FileInfo performanceOutputFile, CancellationToken token)
{
string path = lspServerPath?.FullName ?? lspServer.ServerExecutable;
string? fullPath = GetExecutablePath(path);
if (fullPath == null)
{
Trace.TraceError($"The executable {path} for the LSP server {lspServer.Name} could not be found.");
return 1;
}
Trace.TraceInformation($"Analyzing project: {project}, output: {outputFile}");
token.ThrowIfCancellationRequested();
using ProgressBar progressBar = new(10_000, "Starting LSP server...");
Trace.Listeners.Clear();
ProgressBarTraceListener listener = new(progressBar);
Trace.Listeners.Add(listener);
if (sourcePaths.Length == 0)
{
sourcePaths = [project];
}
LSPHandler handler = new(lspServer, project.FullName, logLsp, TimeSpan.FromSeconds(timeout));
Graph? graph;
try
{
Performance startupPerf = Performance.Begin("LSP Startup", performanceOutputFile.FullName);
await handler.InitializeAsync(executablePath: fullPath, token: token);
token.ThrowIfCancellationRequested();
startupPerf.End();
progressBar.Tick(1);
LSPImporter importer = new(handler,
sourcePaths.Select(x => x.FullName).ToList(),
excludedSourcePaths.Select(x => x.FullName).ToList(),
performanceOutputFile.FullName,
OptimizedEdgeConnection: !unoptimizedEdgeConnection,
IncludeNodeTypes: nodeTypes,
IncludeDiagnostics: diagnosticLevels,
IncludeEdgeTypes: edgeTypes,
AvoidSelfReferences: !selfReferences,
AvoidParentReferences: !parentReferences,
ParallelTasks: parallelism);
progressBar.Message = "Creating graph using LSP...";
IProgress<float> progress = progressBar.AsProgress<float>();
graph = await importer.ImportAsync(progress.Report, x => progressBar.Message = x, token);
}
catch (TimeoutException)
{
string message = "The language server did not respond in time (or you aborted the process).";
if (logLsp)
{
message += $" Check the output log at {Path.GetTempPath()}outputLogLsp.txt";
}
else
{
message += " Enable logging in the graph provider to see what went wrong.";
}
Trace.TraceError(message);
return 1;
}
finally
{
progressBar.Message = "Shutting down LSP server...";
// Two of the language servers block in/out streams on shutdown, and the shutdown doesn't
// seem to accomplish much anyway, so we just disable it for them.
if (lspServer != LSPServer.Pyright && lspServer != LSPServer.TypescriptLanguageServer) {
await handler.ShutdownAsync(token);
}
handler.ReleaseStreams();
}
if (graph == null)
{
Trace.TraceError("The graph could not be created (see log messages).");
return 1;
}
if (outputFile != null)
{
Trace.TraceInformation($"Writing graph to {outputFile.FullName}.");
GraphWriter.Save(outputFile.FullName, graph);
}
else
{
Trace.TraceInformation("No output file given, discarding graph.");
}
Trace.TraceInformation("Done.");
return listener.AnyErrors ? 1 : 0;
}
}