-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPrQuantifierExtendMenu.cs
More file actions
195 lines (169 loc) · 7.23 KB
/
PrQuantifierExtendMenu.cs
File metadata and controls
195 lines (169 loc) · 7.23 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
namespace PrQuantifier.Vsix.Client
{
using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EnvDTE;
using Microsoft;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PullRequestQuantifier.Vsix.Client;
using Task = System.Threading.Tasks.Task;
/// <summary>
/// Command handler.
/// </summary>
internal sealed class PrQuantifierExtendMenu
{
private static DateTimeOffset changedEventDateTime = default;
private static DateTimeOffset runPrQuantifierDateTime = default;
private static DocumentEvents documentEvents;
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly AsyncPackage package;
/// <summary>
/// Initializes a new instance of the <see cref="PrQuantifierExtendMenu"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file).
/// </summary>
/// <param name="package">Owner package, not null.</param>
/// <param name="commandService">Command service to add command to, not null.</param>
private PrQuantifierExtendMenu(AsyncPackage package, OleMenuCommandService commandService)
{
this.package = package ?? throw new ArgumentNullException(nameof(package));
ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await ExecuteAsync();
});
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static PrQuantifierExtendMenu Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task InitializeAsync(AsyncPackage package)
{
// Switch to the main thread - the call to AddCommand in PrQuantifierExtendMenu's constructor requires
// the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new PrQuantifierExtendMenu(package, commandService);
}
private async Task ExecuteAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
IVsStatusbar statusBar = (IVsStatusbar)await ServiceProvider.GetServiceAsync(typeof(SVsStatusbar));
Assumes.Present(statusBar);
DTE dte = (DTE)await ServiceProvider.GetServiceAsync(typeof(DTE));
Assumes.Present(dte);
var uri = new Uri(typeof(PrQuantifierExtendMenuPackage).Assembly.CodeBase, UriKind.Absolute);
documentEvents = dte.Events.DocumentEvents;
documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
var projects = SolutionProjects.Projects();
while (true)
{
if ((runPrQuantifierDateTime == changedEventDateTime
&& runPrQuantifierDateTime != default)
|| projects.Count() == 0)
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
// refresh projects list in case is still empty
if (projects.Count() == 0)
{
projects = SolutionProjects.Projects();
}
continue;
}
try
{
using var process = new System.Diagnostics.Process()
{
StartInfo = new ProcessStartInfo
{
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false,
FileName = Path.Combine(Path.GetDirectoryName(uri.LocalPath), @"PrQuantifier\PullRequestQuantifier.Local.Client.exe"),
Arguments = $"-GitRepoPath \"{projects.ElementAt(0).FullName}\" -output Detailed"
}
};
process.Start();
Print(statusBar, await ReadOutputAsync(process));
if (changedEventDateTime == default)
{
changedEventDateTime = DateTimeOffset.Now;
}
runPrQuantifierDateTime = changedEventDateTime;
}
catch (Exception ex)
{
statusBar.SetText($"PrQuantifier extension has encountered an error, please report it to ... Exception {ex.Message}");
return;
}
}
}
private void DocumentEvents_DocumentSaved(Document document)
{
changedEventDateTime = DateTimeOffset.Now;
}
private async Task<JObject> ReadOutputAsync(System.Diagnostics.Process process)
{
var result = string.Empty;
while (string.IsNullOrEmpty(result))
{
result = await process.StandardOutput.ReadToEndAsync();
if (result.IndexOf(
"GitRepoPath couldn't be found",
StringComparison.InvariantCultureIgnoreCase) > -1)
{
throw new ArgumentException("GitRepoPath couldn't be found");
}
try
{
var jObject = JObject.Parse(result);
return jObject;
}
catch
{
result = string.Empty;
}
}
throw new JsonReaderException();
}
private void Print(
IVsStatusbar statusBar,
JObject quantifierResult)
{
ThreadHelper.ThrowIfNotOnUIThread();
string output = $"PrQuantified = {quantifierResult["Label"]},\t" +
$"Diff +{quantifierResult["QuantifiedLinesAdded"]} -{quantifierResult["QuantifiedLinesDeleted"]} (Formula = {quantifierResult["Formula"]})," +
$"\tPercentiles: additions = {quantifierResult["PercentileAddition"]}%" +
$", deletions = {quantifierResult["PercentileDeletion"]}%, Diff = {quantifierResult["FormulaPercentile"]}%.";
statusBar.Clear();
statusBar.SetText(output);
}
}
}