Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.WinGet.Client.Engine.Commands
/// <summary>
/// Used by Repair-WinGetPackageManager and Assert-WinGetPackageManager.
/// </summary>
public sealed class WinGetPackageManagerCommand : BaseCommand
public sealed class WinGetPackageManagerCommand : ManagementDeploymentCommand
{
private const string EnvPath = "env:PATH";

Expand Down Expand Up @@ -132,7 +132,7 @@ private async Task RepairStateMachineAsync(string expectedVersion, bool allUsers
switch (currentCategory)
{
case IntegrityCategory.UnexpectedVersion:
await this.InstallDifferentVersionAsync(new WinGetVersion(expectedVersion), allUsers, force);
await this.InstallDifferentVersionAsync(new WinGetVersion(expectedVersion), e.InstalledVersion, allUsers, force);
break;
case IntegrityCategory.NotInPath:
this.RepairEnvPath();
Expand Down Expand Up @@ -167,9 +167,13 @@ private async Task RepairStateMachineAsync(string expectedVersion, bool allUsers
}
}

private async Task InstallDifferentVersionAsync(WinGetVersion toInstallVersion, bool allUsers, bool force)
private async Task InstallDifferentVersionAsync(WinGetVersion toInstallVersion, WinGetVersion? installedVersion, bool allUsers, bool force)
{
var installedVersion = WinGetVersion.InstalledWinGetVersion(this);
if (installedVersion == null)
{
installedVersion = WinGetVersion.InstalledWinGetVersion(this);
}

bool isDowngrade = installedVersion.CompareAsDeployment(toInstallVersion) > 0;

string message = $"Installed WinGet version '{installedVersion.TagVersion}' " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
return;
}

WinGetCLICommandResult? versionResult = null;

try
{
// Start by calling winget without its WindowsApp PFN path.
// If it succeeds and the exit code is 0 then we are good.
var wingetCliWrapper = new WingetCLIWrapper(false);
var result = wingetCliWrapper.RunCommand(pwshCmdlet, "--version");
result.VerifyExitCode();
versionResult = WinGetVersion.RunWinGetVersionFromCLI(pwshCmdlet, false);
versionResult.VerifyExitCode();
}
catch (Win32Exception e)
{
Expand All @@ -68,15 +69,16 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
{
// This assumes caller knows that the version exist.
WinGetVersion expectedWinGetVersion = new WinGetVersion(expectedVersion);
var installedVersion = WinGetVersion.InstalledWinGetVersion(pwshCmdlet);
var installedVersion = WinGetVersion.InstalledWinGetVersion(pwshCmdlet, versionResult);
if (expectedWinGetVersion.CompareTo(installedVersion) != 0)
{
throw new WinGetIntegrityException(
IntegrityCategory.UnexpectedVersion,
string.Format(
Resources.IntegrityUnexpectedVersionMessage,
installedVersion.TagVersion,
expectedVersion));
expectedVersion))
{ InstalledVersion = installedVersion };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="WinGetIntegrityException.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand All @@ -9,6 +9,7 @@ namespace Microsoft.WinGet.Client.Engine.Exceptions
using System;
using System.Management.Automation;
using Microsoft.WinGet.Client.Engine.Common;
using Microsoft.WinGet.Client.Engine.Helpers;
using Microsoft.WinGet.Resources;

/// <summary>
Expand Down Expand Up @@ -53,6 +54,11 @@ public WinGetIntegrityException(IntegrityCategory category, string message)
/// </summary>
public IntegrityCategory Category { get; }

/// <summary>
/// Gets or sets the installed version.
/// </summary>
internal WinGetVersion? InstalledVersion { get; set; }

private static string GetMessage(IntegrityCategory category) => category switch
{
IntegrityCategory.Failure => Resources.IntegrityFailureMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,39 @@ public WinGetVersion(string version)
/// </summary>
public bool IsPrerelease { get; }

/// <summary>
/// Runs the winget version command.
/// </summary>
/// <param name="pwshCmdlet">PowerShell cmdlet.</param>
/// <param name="fullPath">Use full path or not.</param>
/// <returns>The command result.</returns>
public static WinGetCLICommandResult RunWinGetVersionFromCLI(PowerShellCmdlet pwshCmdlet, bool fullPath = true)
{
var wingetCliWrapper = new WingetCLIWrapper(fullPath);
return wingetCliWrapper.RunCommand(pwshCmdlet, "--version");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before running the command, perhaps check if winget is actually callable?
Ref #4005 / #5793

}

/// <summary>
/// Gets the version of the installed winget.
/// </summary>
/// <param name="pwshCmdlet">PowerShell cmdlet.</param>
/// <param name="versionResult">A command result from running previously.</param>
/// <returns>The WinGetVersion.</returns>
public static WinGetVersion InstalledWinGetVersion(PowerShellCmdlet pwshCmdlet)
public static WinGetVersion InstalledWinGetVersion(PowerShellCmdlet pwshCmdlet, WinGetCLICommandResult? versionResult = null)
{
// Try getting the version through COM if it is available (user might have an older build installed)
string? comVersion = PackageManagerWrapper.Instance.GetVersion();
if (comVersion != null)
if (versionResult == null || versionResult.ExitCode != 0)
{
return new WinGetVersion(comVersion);
// Try getting the version through COM if it is available (user might have an older build installed)
string? comVersion = PackageManagerWrapper.Instance.GetVersion();
if (comVersion != null)
{
return new WinGetVersion(comVersion);
}

versionResult = RunWinGetVersionFromCLI(pwshCmdlet);
}

var wingetCliWrapper = new WingetCLIWrapper();
var result = wingetCliWrapper.RunCommand(pwshCmdlet, "--version");
return new WinGetVersion(result.StdOut.Replace(Environment.NewLine, string.Empty));
return new WinGetVersion(versionResult.StdOut.Replace(Environment.NewLine, string.Empty));
}

/// <summary>
Expand Down
Loading