-
Notifications
You must be signed in to change notification settings - Fork 509
/
VerifyCommand.cs
261 lines (210 loc) · 9.82 KB
/
VerifyCommand.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Try.Markdown;
using Microsoft.DotNet.Try.Project;
using Microsoft.DotNet.Try.Protocol;
using MLS.Agent.Markdown;
using WorkspaceServer;
using WorkspaceServer.Servers;
using Buffer = Microsoft.DotNet.Try.Protocol.Buffer;
namespace MLS.Agent.CommandLine
{
public static class VerifyCommand
{
public static async Task<int> Do(
VerifyOptions options,
IConsole console,
Func<IDirectoryAccessor> getDirectoryAccessor,
PackageRegistry packageRegistry,
StartupOptions startupOptions = null)
{
var directoryAccessor = getDirectoryAccessor();
var markdownProject = new MarkdownProject(
directoryAccessor,
packageRegistry,
startupOptions);
var errorCount = 0;
var workspaceServer = new Lazy<IWorkspaceServer>(() => new WorkspaceServerMultiplexer(packageRegistry));
var markdownFiles = markdownProject.GetAllMarkdownFiles().ToArray();
if (markdownFiles.Length == 0)
{
console.Error.WriteLine($"No markdown files found under {directoryAccessor.GetFullyQualifiedRoot()}");
return -1;
}
foreach (var markdownFile in markdownFiles)
{
var fullName = directoryAccessor.GetFullyQualifiedPath(markdownFile.Path).FullName;
var markdownFileDirectoryAccessor = directoryAccessor.GetDirectoryAccessorForRelativePath(markdownFile.Path.Directory);
console.Out.WriteLine();
console.Out.WriteLine(fullName);
console.Out.WriteLine(new string('-', fullName.Length));
var codeLinkBlocks = await markdownFile.GetAnnotatedCodeBlocks();
var sessions = codeLinkBlocks.GroupBy(block => block.Annotations?.Session);
foreach (var session in sessions)
{
if (session.Select(block => block.ProjectOrPackageName()).Distinct().Count() != 1)
{
SetError();
console.Out.WriteLine($"Session cannot span projects or packages: --session {session.Key}");
continue;
}
foreach (var codeLinkBlock in session)
{
ReportCodeLinkageResults(codeLinkBlock, markdownFileDirectoryAccessor);
}
Console.ResetColor();
if (!session.Any(block => block.Diagnostics.Any()))
{
var (buffersToInclude, filesToInclude) = await markdownFile.GetIncludes(markdownFileDirectoryAccessor);
await ReportCompileResults(
session,
markdownFile,
filesToInclude,
buffersToInclude,
markdownFileDirectoryAccessor);
}
Console.ResetColor();
}
}
if (errorCount > 0)
{
SetError(false);
}
else
{
SetOk();
}
console.Out.WriteLine($"\n\ndotnet try verify found {errorCount} error(s)");
Console.ResetColor();
return errorCount == 0
? 0
: 1;
void SetError(bool incrementCount = true)
{
Console.ForegroundColor = ConsoleColor.Red;
if (incrementCount)
{
errorCount++;
}
}
void SetOk()
{
Console.ForegroundColor = ConsoleColor.Green;
}
async Task ReportCompileResults(
IGrouping<string, AnnotatedCodeBlock> session,
MarkdownFile markdownFile,
Dictionary<string, File[]> filesToInclude,
IReadOnlyDictionary<string, Buffer[]> buffersToInclude,
IDirectoryAccessor accessor)
{
var description = session.Count() == 1 || string.IsNullOrWhiteSpace(session.Key)
? $"region \"{session.Select(s => s.Annotations.Region).Distinct().First()}\""
: $"session \"{session.Key}\"";
console.Out.WriteLine($"\n Compiling samples for {description}\n");
var projectOrPackageName = session
.Select(b => b.ProjectOrPackageName())
.FirstOrDefault(name => !string.IsNullOrWhiteSpace(name));
var editableCodeBlocks = session.Where(b => b.Annotations.Editable).ToList();
var buffers = editableCodeBlocks
.Select(block => block.GetBufferAsync(accessor, markdownFile))
.ToList();
var files = new List<File>();
if (filesToInclude.TryGetValue("global", out var globalIncludes))
{
files.AddRange(globalIncludes);
}
if (!string.IsNullOrWhiteSpace(session.Key) && filesToInclude.TryGetValue(session.Key, out var sessionIncludes))
{
files.AddRange(sessionIncludes);
}
if (buffersToInclude.TryGetValue("global", out var globalSessionBuffersToInclude))
{
buffers.AddRange(globalSessionBuffersToInclude);
}
if (!string.IsNullOrWhiteSpace(session.Key) && buffersToInclude.TryGetValue(session.Key, out var localSessionBuffersToInclude))
{
buffers.AddRange(localSessionBuffersToInclude);
}
var workspace = new Workspace(
workspaceType: projectOrPackageName,
files: files.ToArray(),
buffers: buffers.ToArray());
var mergeTransformer = new CodeMergeTransformer();
var inliningTransformer = new BufferInliningTransformer();
var processed = await mergeTransformer.TransformAsync(workspace);
processed = await inliningTransformer.TransformAsync(processed);
processed = new Workspace(usings: processed.Usings, workspaceType: processed.WorkspaceType, files: processed.Files);
var result = await workspaceServer.Value.Compile(new WorkspaceRequest(processed));
var projectDiagnostics = result.GetFeature<ProjectDiagnostics>()
.Where(e => e.Severity == DiagnosticSeverity.Error)
.ToArray();
if (projectDiagnostics.Any())
{
SetError();
console.Out.WriteLine($" Build failed for project {projectOrPackageName}");
foreach (var diagnostic in projectDiagnostics)
{
console.Out.WriteLine($"\t\t{diagnostic.Location}: {diagnostic.Message}");
}
}
else
{
var symbol = !result.Succeeded
? "X"
: "✓";
if (result.Succeeded)
{
SetOk();
console.Out.WriteLine($" {symbol} No errors found within samples for {description}");
}
else
{
SetError();
console.Out.WriteLine($" {symbol} Errors found within samples for {description}");
foreach (var diagnostic in result.GetFeature<Diagnostics>())
{
console.Out.WriteLine($"\t\t{diagnostic.Message}");
}
}
}
}
void ReportCodeLinkageResults(
AnnotatedCodeBlock codeLinkBlock,
IDirectoryAccessor accessor)
{
var diagnostics = codeLinkBlock.Diagnostics.ToArray();
Console.ResetColor();
console.Out.WriteLine(" Checking Markdown...");
if (diagnostics.Any())
{
SetError();
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
}
var blockOptions = (LocalCodeBlockAnnotations)codeLinkBlock.Annotations;
var file = blockOptions?.SourceFile ?? blockOptions?.DestinationFile;
var fullyQualifiedPath =
file != null
? accessor.GetFullyQualifiedPath(file).FullName
: "UNKNOWN";
var project = codeLinkBlock.ProjectOrPackageName() ?? "UNKNOWN";
var symbol = diagnostics.Any()
? "X"
: "✓";
console.Out.WriteLine($" {symbol} Line {codeLinkBlock.Line + 1}:\t{fullyQualifiedPath} (in project {project})");
foreach (var diagnostic in diagnostics)
{
console.Out.WriteLine($"\t\t{diagnostic}");
}
}
}
}
}