Skip to content

Commit 6c59ec8

Browse files
committed
Verified version of gtm.
1 parent a14b865 commit 6c59ec8

1 file changed

Lines changed: 65 additions & 17 deletions

File tree

src/GtmExtension/GtmPackage.cs

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,56 @@ public GtmPackage()
5555

5656
#region Helper Functions
5757

58-
/// <summary>
59-
/// Checks if executable <paramref name="exeName"/> can be found in system's PATH.
60-
/// </summary>
61-
/// <seealso href="https://stackoverflow.com/a/24405838/9080566"/>
62-
public static bool ExistsOnPath(string exeName)
58+
private static Process ExecuteProcess(string exeName, string arguments)
6359
{
6460
try
6561
{
6662
var p = new Process();
6763
p.StartInfo.UseShellExecute = false;
68-
p.StartInfo.FileName = "where";
69-
p.StartInfo.Arguments = exeName;
64+
p.StartInfo.FileName = exeName;
65+
p.StartInfo.Arguments = arguments;
66+
p.StartInfo.CreateNoWindow = true;
67+
p.StartInfo.RedirectStandardOutput = true;
7068
p.Start();
7169
p.WaitForExit();
72-
return p.ExitCode == 0;
70+
return p;
7371
}
7472
catch (Win32Exception)
7573
{
76-
throw new Exception("Command 'where' was not found.");
74+
throw new Exception($"Executable \"{exeName}\" was not found.");
7775
}
7876
}
77+
private static int Execute(string exeName, string arguments = null)
78+
{
79+
return ExecuteProcess(exeName, arguments).ExitCode;
80+
}
81+
private static string ExecuteForOutput(string exeName, string arguments = null)
82+
{
83+
Process p = ExecuteProcess(exeName, arguments);
84+
return p.StandardOutput.ReadToEnd();
85+
}
86+
87+
/// <summary>
88+
/// Checks if executable <paramref name="exeName"/> can be found in system's PATH.
89+
/// </summary>
90+
/// <seealso href="https://stackoverflow.com/a/24405838/9080566"/>
91+
private static bool ExistsOnPath(string exeName)
92+
{
93+
return Execute("where", exeName) == 0;
94+
}
95+
96+
private async Task ShowErrorAsync(string message)
97+
{
98+
// Switch to UI thread.
99+
await JoinableTaskFactory.SwitchToMainThreadAsync();
100+
101+
// Show message box.
102+
var uiShell = (IVsUIShell)await GetServiceAsync(typeof(SVsUIShell));
103+
if (uiShell == null) { return; }
104+
Guid clsid = Guid.Empty;
105+
ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, ref clsid, "GtmExtension", message, string.Empty, 0,
106+
OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_CRITICAL, 0, out var result));
107+
}
79108

80109
#endregion
81110

@@ -98,18 +127,37 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
98127

99128
// When initialized asynchronously, the current thread may be a background thread at this point.
100129
// Do any initialization that requires the UI thread after switching to the UI thread.
101-
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
130+
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
102131

103-
// Show error, if we didn't find `gtm` on PATH.
132+
// Show error if we don't find `gtm` on PATH.
104133
if (gtmExe == null)
105134
{
106-
var uiShell = (IVsUIShell)await GetServiceAsync(typeof(SVsUIShell));
107-
if (uiShell == null) { return; }
108-
Guid clsid = Guid.Empty;
109-
int result;
110-
ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, ref clsid, "GtmPackage", "We couldn't find gtm executable.", string.Empty, 0,
111-
OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_CRITICAL, 0, out result));
135+
await ShowErrorAsync("We couldn't find gtm executable.");
136+
return;
137+
}
138+
139+
// Verify version.
140+
if (!ExecuteForOutput(gtmExe, "verify \">= 1.1.0\"").Contains("true"))
141+
{
142+
await ShowErrorAsync("Old version of gtm is installed. Please install at least version 1.1.0");
143+
return;
112144
}
145+
146+
// Show info in status bar.
147+
148+
// Get the status bar.
149+
var statusBar = (IVsStatusbar)await GetServiceAsync(typeof(SVsStatusbar));
150+
if (statusBar == null) { throw new InvalidOperationException("No status bar."); }
151+
152+
// Unfroze it if it's frozen.
153+
ErrorHandler.ThrowOnFailure(statusBar.IsFrozen(out var frozen));
154+
if (frozen != 0)
155+
{
156+
ErrorHandler.ThrowOnFailure(statusBar.FreezeOutput(0));
157+
}
158+
159+
// TODO.
160+
statusBar.SetText("We control the status bar now!");
113161
}
114162

115163
#endregion

0 commit comments

Comments
 (0)