Skip to content

Commit ca80bdf

Browse files
Update PackageManagement
1 parent abf2496 commit ca80bdf

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System;
67
using System.Collections.Generic;
78
using System.Management.Automation;
9+
using System.Text;
810
using System.Threading;
911
using System.Threading.Tasks;
1012
using Microsoft.Extensions.Logging;
1113
using Microsoft.PowerShell.EditorServices.Services;
1214
using MediatR;
1315
using OmniSharp.Extensions.JsonRpc;
16+
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
17+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1418

1519
namespace Microsoft.PowerShell.EditorServices.Handlers
1620
{
@@ -36,24 +40,26 @@ internal class GetCommandHandler : IGetCommandHandler
3640
{
3741
private readonly ILogger<GetCommandHandler> _logger;
3842
private readonly PowerShellContextService _powerShellContextService;
43+
private readonly ILanguageServer _languageServer;
3944

40-
public GetCommandHandler(ILoggerFactory factory, PowerShellContextService powerShellContextService)
45+
private bool _skipPackageManagementCheck;
46+
47+
public GetCommandHandler(ILoggerFactory factory, PowerShellContextService powerShellContextService, ILanguageServer languageServer)
4148
{
4249
_logger = factory.CreateLogger<GetCommandHandler>();
4350
_powerShellContextService = powerShellContextService;
51+
_languageServer = languageServer;
4452
}
4553

4654
public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, CancellationToken cancellationToken)
4755
{
4856
PSCommand psCommand = new PSCommand();
4957

5058
// Executes the following:
51-
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name
59+
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name
5260
psCommand
5361
.AddCommand("Microsoft.PowerShell.Core\\Get-Command")
5462
.AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" })
55-
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object")
56-
.AddParameter("Property", new[] { "Name", "ModuleName" })
5763
.AddCommand("Microsoft.PowerShell.Utility\\Sort-Object")
5864
.AddParameter("Property", "Name");
5965

@@ -64,6 +70,65 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance
6470
{
6571
foreach (dynamic command in result)
6672
{
73+
if (!_skipPackageManagementCheck && command.ModuleName == "PackageManagement" && command.Version < new Version(1, 4, 6))
74+
{
75+
_logger.LogDebug("Old version of PackageManagement detected. Attempting to update.");
76+
77+
var takeActionText = "Yes";
78+
MessageActionItem messageAction = await _languageServer.Window.ShowMessage(new ShowMessageRequestParams()
79+
{
80+
Message = "You have a version of PackageManagement that causes issues with the PowerShell extension. Would you like to update PackageManagement (You will need to restart the PowerShell extension after)?",
81+
Type = MessageType.Warning,
82+
Actions = new MessageActionItem[]
83+
{
84+
new MessageActionItem
85+
{
86+
Title = takeActionText
87+
},
88+
new MessageActionItem
89+
{
90+
Title = "Not now"
91+
}
92+
}
93+
});
94+
95+
// If the user chose "Not now" ignore it for the rest of the session.
96+
if (messageAction?.Title != takeActionText)
97+
{
98+
_skipPackageManagementCheck = true;
99+
}
100+
else
101+
{
102+
// Update PackageManagement
103+
psCommand
104+
.AddCommand("PowerShellGet\\Install-Module")
105+
.AddParameter("Name", "PackageManagement")
106+
.AddParameter("Force")
107+
.AddParameter("AllowClobber");
108+
109+
StringBuilder errors = new StringBuilder();
110+
await _powerShellContextService.ExecuteCommandAsync<object>(psCommand, errorMessages: errors, sendOutputToHost: true).ConfigureAwait(false);
111+
112+
// There were errors installing PackageManagement.
113+
if (errors.Length == 0)
114+
{
115+
_languageServer.Window.ShowMessage(new ShowMessageParams
116+
{
117+
Type = MessageType.Warning,
118+
Message = "PackageManagement updated, please restart the PowerShell extension."
119+
});
120+
}
121+
else
122+
{
123+
_languageServer.Window.ShowMessage(new ShowMessageParams
124+
{
125+
Type = MessageType.Error,
126+
Message = "PackageManagement update failed. Please run the following command in a Windows PowerShell prompt and restart the PowerShell extension: `Install-Module PackageManagement -Force -AllowClobber`"
127+
});
128+
}
129+
}
130+
}
131+
67132
commandList.Add(new PSCommandMessage
68133
{
69134
Name = command.Name,

0 commit comments

Comments
 (0)