forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunLinterOnSolutionCommand.cs
108 lines (93 loc) · 4.35 KB
/
RunLinterOnSolutionCommand.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace MarkdownLinter;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.Shell;
using Microsoft.VisualStudio.ProjectSystem.Query;
using Microsoft.VisualStudio.RpcContracts.ProgressReporting;
/// <summary>
/// A command to execute linter on all markdown files in a solution.
/// </summary>
/// <remarks>
/// This command utilizes <see cref="CommandConfiguration.EnabledWhen"/> to describe when commmand state is enabled.
/// </remarks>
[VisualStudioContribution]
internal class RunLinterOnSolutionCommand : Command
{
private readonly TraceSource logger;
#pragma warning disable CA2213 // This is an extension scoped service.
private readonly MarkdownDiagnosticsService diagnosticsProvider;
#pragma warning restore CA2213
/// <summary>
/// Initializes a new instance of the <see cref="RunLinterOnSolutionCommand"/> class.
/// </summary>
/// <param name="traceSource">Logger instance that can be used to log extension actions.</param>
/// <param name="diagnosticsProvider">Local diagnostics provider service instance.</param>
public RunLinterOnSolutionCommand(TraceSource traceSource, MarkdownDiagnosticsService diagnosticsProvider)
{
this.logger = Requires.NotNull(traceSource, nameof(traceSource));
this.diagnosticsProvider = Requires.NotNull(diagnosticsProvider, nameof(diagnosticsProvider));
this.logger.TraceEvent(TraceEventType.Information, 0, $"Initializing {nameof(RunLinterOnSolutionCommand)} instance.");
}
/// <inheritdoc />
public override CommandConfiguration CommandConfiguration => new("%MarkdownLinter.RunLinterOnSolutionCommand.DisplayName%")
{
Placements = new[] { CommandPlacement.KnownPlacements.ToolsMenu },
Icon = new(ImageMoniker.Custom("MarkdownIcon"), IconSettings.IconAndText),
EnabledWhen = ActivationConstraint.SolutionState(SolutionState.FullyLoaded),
};
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
try
{
var markdownFiles = await this.Extensibility.Workspaces().QueryProjectsAsync(
query => query.Get(project => project.Files).With(file => file.Path).Where(file => file.Extension == ".md"),
cancellationToken);
List<Uri> filesToProcess = new List<Uri>(markdownFiles.Select(f => new Uri(f.Path)));
if (filesToProcess.Count == 0)
{
return;
}
if (await this.Extensibility.Shell().ShowPromptAsync(
string.Format(Strings.Culture, Strings.MarkdownSolutionAnalysisPrompt, filesToProcess.Count),
PromptOptions.OKCancel,
cancellationToken))
{
await this.ProcessFilesAsync(filesToProcess, cancellationToken);
}
}
catch (InvalidOperationException ex)
{
this.logger.TraceEvent(TraceEventType.Error, 0, ex.ToString());
}
}
private static ProgressStatus CreateProgressStatus(int current, int max)
{
return new ProgressStatus((int)((current / (double)max) * 100));
}
private async System.Threading.Tasks.Task<ProgressReporter> ProcessFilesAsync(List<Uri> filesToProcess, CancellationToken cancellationToken)
{
using ProgressReporter progress = await this.Extensibility.Shell().StartProgressReportingAsync(
Strings.MarkdownAnalysisMessage,
new ProgressReporterOptions(isWorkCancellable: true),
cancellationToken);
for (int i = 0; i < filesToProcess.Count; i++)
{
progress.CancellationToken.ThrowIfCancellationRequested();
progress.Report(CreateProgressStatus(i, filesToProcess.Count));
// Adding an artificial delay for demonstration purposes.
await Task.Delay(2000);
await this.diagnosticsProvider.ProcessFileAsync(filesToProcess[i], cancellationToken);
}
return progress;
}
}