-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update PackageManagement #1239
Update PackageManagement #1239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,41 @@ | |
// | ||
|
||
using System; | ||
using System.Management.Automation; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.PowerShell.EditorServices.Services; | ||
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext; | ||
using Microsoft.PowerShell.EditorServices.Utility; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Server; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Handlers | ||
{ | ||
internal class GetVersionHandler : IGetVersionHandler | ||
{ | ||
private static readonly Version s_desiredPackageManagementVersion = new Version(1, 4, 6); | ||
|
||
private readonly ILogger<GetVersionHandler> _logger; | ||
private readonly PowerShellContextService _powerShellContextService; | ||
private readonly ILanguageServer _languageServer; | ||
private readonly ConfigurationService _configurationService; | ||
|
||
public GetVersionHandler(ILoggerFactory factory) | ||
public GetVersionHandler( | ||
ILoggerFactory factory, | ||
PowerShellContextService powerShellContextService, | ||
ILanguageServer languageServer, | ||
ConfigurationService configurationService) | ||
{ | ||
_logger = factory.CreateLogger<GetVersionHandler>(); | ||
_powerShellContextService = powerShellContextService; | ||
_languageServer = languageServer; | ||
_configurationService = configurationService; | ||
} | ||
|
||
public Task<PowerShellVersion> Handle(GetVersionParams request, CancellationToken cancellationToken) | ||
public async Task<PowerShellVersion> Handle(GetVersionParams request, CancellationToken cancellationToken) | ||
{ | ||
var architecture = PowerShellProcessArchitecture.Unknown; | ||
// This should be changed to using a .NET call sometime in the future... but it's just for logging purposes. | ||
|
@@ -37,13 +55,18 @@ public Task<PowerShellVersion> Handle(GetVersionParams request, CancellationToke | |
} | ||
} | ||
|
||
return Task.FromResult(new PowerShellVersion | ||
if (VersionUtils.IsPS5 && _configurationService.CurrentSettings.PromptToUpdatePackageManagement) | ||
{ | ||
await CheckPackageManagement().ConfigureAwait(false); | ||
} | ||
|
||
return new PowerShellVersion | ||
{ | ||
Version = VersionUtils.PSVersionString, | ||
Edition = VersionUtils.PSEdition, | ||
DisplayVersion = VersionUtils.PSVersion.ToString(2), | ||
Architecture = architecture.ToString() | ||
}); | ||
}; | ||
} | ||
|
||
private enum PowerShellProcessArchitecture | ||
|
@@ -52,5 +75,68 @@ private enum PowerShellProcessArchitecture | |
X86, | ||
X64 | ||
} | ||
|
||
private async Task CheckPackageManagement() | ||
{ | ||
PSCommand getModule = new PSCommand().AddCommand("Get-Module").AddParameter("ListAvailable").AddParameter("Name", "PackageManagement"); | ||
foreach (PSModuleInfo module in await _powerShellContextService.ExecuteCommandAsync<PSModuleInfo>(getModule)) | ||
{ | ||
// The user has a good enough version of PackageManagement | ||
if(module.Version >= s_desiredPackageManagementVersion) | ||
{ | ||
break; | ||
} | ||
|
||
_logger.LogDebug("Old version of PackageManagement detected. Attempting to update."); | ||
|
||
var takeActionText = "Yes"; | ||
MessageActionItem messageAction = await _languageServer.Window.ShowMessage(new ShowMessageRequestParams | ||
{ | ||
Message = "You have an older version of PackageManagement known to cause issues with the PowerShell extension. Would you like to update PackageManagement (You will need to restart the PowerShell extension after)?", | ||
Type = MessageType.Warning, | ||
Actions = new [] | ||
{ | ||
new MessageActionItem | ||
{ | ||
Title = takeActionText | ||
}, | ||
new MessageActionItem | ||
{ | ||
Title = "Not now" | ||
} | ||
} | ||
}); | ||
|
||
// If the user chose "Not now" ignore it for the rest of the session. | ||
if (messageAction?.Title == takeActionText) | ||
{ | ||
StringBuilder errors = new StringBuilder(); | ||
await _powerShellContextService.ExecuteScriptStringAsync( | ||
"powershell.exe -NoLogo -NoProfile -Command 'Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber'", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This no longer works because the site requires TLS 1.2. I would recommend added code to force to use TLS 1.2
|
||
errors, | ||
writeInputToHost: true, | ||
writeOutputToHost: true, | ||
addToHistory: true).ConfigureAwait(false); | ||
|
||
if (errors.Length == 0) | ||
{ | ||
_languageServer.Window.ShowMessage(new ShowMessageParams | ||
{ | ||
Type = MessageType.Info, | ||
Message = "PackageManagement updated, If you already had PackageManagement loaded in your session, please restart the PowerShell extension." | ||
}); | ||
} | ||
else | ||
{ | ||
// There were errors installing PackageManagement. | ||
_languageServer.Window.ShowMessage(new ShowMessageParams | ||
{ | ||
Type = MessageType.Error, | ||
Message = "PackageManagement update failed. Please run the following command in a new Windows PowerShell session and then restart the PowerShell extension: `Install-Module PackageManagement -Force -AllowClobber -MinimumVersion 1.4.6`" | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not adding a "don't tell me ever again" because: