Skip to content
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

Merged
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 @@ -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.
Expand All @@ -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
Expand All @@ -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"
}
Comment on lines +103 to +106
Copy link
Member Author

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:

  1. I don't know how to set a setting in the client from the server without adding yet another custom message
  2. I kinda wanna make it hard to find the setting to disable this because they really should update their PackageManagement...

}
});

// 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'",
Copy link

Choose a reason for hiding this comment

The 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

powershell.exe -NoLogo -NoProfile -Command '[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber

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`"
});
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal class LanguageServerSettings
private readonly object updateLock = new object();
public bool EnableProfileLoading { get; set; }

public bool PromptToUpdatePackageManagement { get; set; }

public ScriptAnalysisSettings ScriptAnalysis { get; set; }

public CodeFormattingSettings CodeFormatting { get; set; }
Expand All @@ -46,6 +48,7 @@ public void Update(
lock (updateLock)
{
this.EnableProfileLoading = settings.EnableProfileLoading;
this.PromptToUpdatePackageManagement = settings.PromptToUpdatePackageManagement;
this.ScriptAnalysis.Update(
settings.ScriptAnalysis,
workspaceRootPath,
Expand Down