Skip to content

Commit d1beb85

Browse files
committed
Comment Cleanup
1 parent 4e24c79 commit d1beb85

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/PowerShell/Microsoft.WinGet.Client.Engine/Common/WinGetIntegrity.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
4545
{
4646
try
4747
{
48-
// First check if winget is registered in the current session.
49-
// If Get-Command doesn't find it, then winget is not registered in the current session.
48+
// First check if winget is registered in the current session using Get-Command.
5049
var ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
5150
{
51+
// Ignore the error, we will check the results instead
5252
ps.AddCommand("Get-Command").AddParameter("Name", "winget").AddParameter("ErrorAction", "Ignore");
5353
Collection<PSObject> results = ps.Invoke();
5454
ps.Dispose();
@@ -57,12 +57,13 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
5757
results.ElementAt(0).BaseObject is not CommandInfo)
5858
{
5959
// It's expected that the command is found, is the only command, and is a CommandInfo object.
60+
// If it is not, then winget is not properly registered in the current session and we need to figure out why.
6061
pwshCmdlet.Write(StreamType.Verbose, $"'winget' was not found using Get-Command");
61-
throw new WinGetIntegrityException(GetReason(pwshCmdlet));
62+
throw new WinGetIntegrityException(GetReason(pwshCmdlet, false));
6263
}
6364
}
6465

65-
// Then try calling winget without its WindowsApp PFN path.
66+
// If the command is registered, try calling winget without its WindowsApp PFN path.
6667
// If it succeeds and the exit code is 0 then we are good.
6768
var wingetCliWrapper = new WingetCLIWrapper(false);
6869
var result = wingetCliWrapper.RunCommand(pwshCmdlet, "--version");
@@ -76,7 +77,7 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
7677
catch (Win32Exception e)
7778
{
7879
pwshCmdlet.Write(StreamType.Verbose, $"'winget.exe' Win32Exception {e.Message}");
79-
throw new WinGetIntegrityException(GetReason(pwshCmdlet));
80+
throw new WinGetIntegrityException(GetReason(pwshCmdlet, true));
8081
}
8182
catch (Exception e) when (e is WinGetCLIException || e is WinGetCLITimeoutException)
8283
{
@@ -108,44 +109,39 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
108109
}
109110
}
110111

111-
private static IntegrityCategory GetReason(PowerShellCmdlet pwshCmdlet)
112+
private static IntegrityCategory GetReason(PowerShellCmdlet pwshCmdlet, bool commandIsRegistered)
112113
{
113114
// Ok, so you are here because calling winget --version failed. Lets try to figure out why.
114115
var category = IntegrityCategory.Unknown;
115-
pwshCmdlet.ExecuteInPowerShellThread(() =>
116+
117+
// If the command is registered, then we can try to call it
118+
// Otherwise we will skip this step since it will always result in category remaining unknown
119+
if (commandIsRegistered)
116120
{
121+
pwshCmdlet.ExecuteInPowerShellThread(() =>
122+
{
117123
// When running winget.exe on PowerShell the message of the Win32Exception will distinguish between
118124
// 'The system cannot find the file specified' and 'No applicable app licenses found' but of course
119125
// the HRESULT is the same (E_FAIL).
120126
// To not compare strings let Powershell handle it. If calling winget throws an
121127
// ApplicationFailedException then is most likely that the license is not there.
122-
try
123-
{
124-
var ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
125-
ps.AddCommand("Get-Command").AddParameter("Name", "winget").AddParameter("ErrorAction", "Ignore");
126-
Collection<PSObject> results = ps.Invoke();
127-
if (results == null ||
128-
results.Count != 1 ||
129-
results.ElementAt(0).BaseObject is not CommandInfo)
128+
try
130129
{
131-
// It's expected that the command is found, is the only command, and is a CommandInfo object.
132-
pwshCmdlet.Write(StreamType.Verbose, $"'winget' was not found using Get-Command");
130+
using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace))
131+
{
132+
ps.AddCommand("winget").Invoke();
133+
}
133134
}
134-
else
135+
catch (ApplicationFailedException e)
135136
{
136-
ps.Commands.Clear();
137-
ps.AddCommand("winget").Invoke();
137+
pwshCmdlet.Write(StreamType.Verbose, e.Message);
138+
category = IntegrityCategory.AppInstallerNoLicense;
138139
}
139-
}
140-
catch (ApplicationFailedException e)
141-
{
142-
pwshCmdlet.Write(StreamType.Verbose, e.Message);
143-
category = IntegrityCategory.AppInstallerNoLicense;
144-
}
145-
catch (Exception)
146-
{
147-
}
148-
});
140+
catch (Exception)
141+
{
142+
}
143+
});
144+
}
149145

150146
if (category != IntegrityCategory.Unknown)
151147
{

0 commit comments

Comments
 (0)