3
3
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4
4
//
5
5
6
+ using System ;
6
7
using System . Collections . Generic ;
7
8
using System . Management . Automation ;
9
+ using System . Text ;
8
10
using System . Threading ;
9
11
using System . Threading . Tasks ;
10
12
using Microsoft . Extensions . Logging ;
11
13
using Microsoft . PowerShell . EditorServices . Services ;
12
14
using MediatR ;
13
15
using OmniSharp . Extensions . JsonRpc ;
16
+ using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
17
+ using OmniSharp . Extensions . LanguageServer . Protocol . Models ;
14
18
15
19
namespace Microsoft . PowerShell . EditorServices . Handlers
16
20
{
@@ -36,24 +40,26 @@ internal class GetCommandHandler : IGetCommandHandler
36
40
{
37
41
private readonly ILogger < GetCommandHandler > _logger ;
38
42
private readonly PowerShellContextService _powerShellContextService ;
43
+ private readonly ILanguageServer _languageServer ;
39
44
40
- public GetCommandHandler ( ILoggerFactory factory , PowerShellContextService powerShellContextService )
45
+ private bool _skipPackageManagementCheck ;
46
+
47
+ public GetCommandHandler ( ILoggerFactory factory , PowerShellContextService powerShellContextService , ILanguageServer languageServer )
41
48
{
42
49
_logger = factory . CreateLogger < GetCommandHandler > ( ) ;
43
50
_powerShellContextService = powerShellContextService ;
51
+ _languageServer = languageServer ;
44
52
}
45
53
46
54
public async Task < List < PSCommandMessage > > Handle ( GetCommandParams request , CancellationToken cancellationToken )
47
55
{
48
56
PSCommand psCommand = new PSCommand ( ) ;
49
57
50
58
// 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
52
60
psCommand
53
61
. AddCommand ( "Microsoft.PowerShell.Core\\ Get-Command" )
54
62
. AddParameter ( "CommandType" , new [ ] { "Function" , "Cmdlet" , "ExternalScript" } )
55
- . AddCommand ( "Microsoft.PowerShell.Utility\\ Select-Object" )
56
- . AddParameter ( "Property" , new [ ] { "Name" , "ModuleName" } )
57
63
. AddCommand ( "Microsoft.PowerShell.Utility\\ Sort-Object" )
58
64
. AddParameter ( "Property" , "Name" ) ;
59
65
@@ -64,6 +70,65 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance
64
70
{
65
71
foreach ( dynamic command in result )
66
72
{
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
+
67
132
commandList . Add ( new PSCommandMessage
68
133
{
69
134
Name = command . Name ,
0 commit comments