From b68a55e61fdd20a34921a90de2a8c89ee52ee071 Mon Sep 17 00:00:00 2001 From: "Tyler Leonhardt (POWERSHELL)" Date: Fri, 15 May 2020 09:05:43 -0700 Subject: [PATCH 1/4] Use PSReadLine in CLM --- .../PowerShellContextService.cs | 2 -- .../Host/EditorServicesPSHostUserInterface.cs | 2 +- .../Session/PSReadLinePromptContext.cs | 23 +++++++++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index fbbfbc9dc..26ae9bd93 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -415,8 +415,6 @@ public void Initialize( if (powerShellVersion.Major >= 5 && this.isPSReadLineEnabled && - // TODO: Figure out why PSReadLine isn't working in ConstrainedLanguage mode. - initialRunspace.SessionStateProxy.LanguageMode == PSLanguageMode.FullLanguage && PSReadLinePromptContext.TryGetPSReadLineProxy(logger, initialRunspace, out PSReadLineProxy proxy)) { this.PromptContext = new PSReadLinePromptContext( diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs index dba7b1c74..adbf1bb0d 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs @@ -729,7 +729,7 @@ private async Task WritePromptStringToHostAsync(CancellationToken cancellationTo { } - PSCommand promptCommand = new PSCommand().AddScript("prompt"); + PSCommand promptCommand = new PSCommand().AddScript("prompt", useLocalScope: true); cancellationToken.ThrowIfCancellationRequested(); string promptString = diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs index 7a14d8e7a..abc2be886 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs @@ -14,26 +14,35 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext { + using System.IO; using System.Management.Automation; internal class PSReadLinePromptContext : IPromptContext { - private const string ReadLineInitScript = @" + private static readonly string _psReadLineModulePath = Path.Combine( + Path.GetDirectoryName(typeof(PSReadLinePromptContext).Assembly.Location), + "..", + "..", + "..", + "PSReadLine"); + + private static readonly string ReadLineInitScript = $@" [System.Diagnostics.DebuggerHidden()] [System.Diagnostics.DebuggerStepThrough()] param() - end { + end {{ $module = Get-Module -ListAvailable PSReadLine | - Where-Object { $_.Version -gt '2.0.0' -or ($_.Version -eq '2.0.0' -and -not $_.PrivateData.PSData.Prerelease) } | + Where-Object {{ $_.Version -gt '2.0.0' -or ($_.Version -eq '2.0.0' -and -not $_.PrivateData.PSData.Prerelease) }} | Sort-Object -Descending Version | Select-Object -First 1 - if (-not $module) { - return - } + if (-not $module) {{ + Import-Module {_psReadLineModulePath} + return [Microsoft.PowerShell.PSConsoleReadLine] + }} Import-Module -ModuleInfo $module return [Microsoft.PowerShell.PSConsoleReadLine] - }"; + }}"; private static readonly Lazy s_lazyInvokeReadLineForEditorServicesCmdletInfo = new Lazy(() => { From 4e3daf11dcbc5ade69cb2fc300d60d4e463436a9 Mon Sep 17 00:00:00 2001 From: "Tyler Leonhardt (POWERSHELL)" Date: Fri, 15 May 2020 09:09:20 -0700 Subject: [PATCH 2/4] update PSRL version check for CLM --- modules.json | 2 +- .../PowerShellContext/Session/PSReadLinePromptContext.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules.json b/modules.json index 2aa3c8157..cb9076b20 100644 --- a/modules.json +++ b/modules.json @@ -8,6 +8,6 @@ "MaximumVersion":"1.99" }, "PSReadLine":{ - "MinimumVersion":"2.0.0" + "MinimumVersion":"2.0.2" } } diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs index abc2be886..5e40886f7 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs @@ -32,7 +32,7 @@ internal class PSReadLinePromptContext : IPromptContext param() end {{ $module = Get-Module -ListAvailable PSReadLine | - Where-Object {{ $_.Version -gt '2.0.0' -or ($_.Version -eq '2.0.0' -and -not $_.PrivateData.PSData.Prerelease) }} | + Where-Object {{ $_.Version -ge '2.0.2' }} | Sort-Object -Descending Version | Select-Object -First 1 if (-not $module) {{ From 4ce2a2cbf1f52af76179f14391ff262ee0bbdbe4 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 5 Jun 2020 15:38:57 -0700 Subject: [PATCH 3/4] feedback --- .../Session/Host/EditorServicesPSHostUserInterface.cs | 2 +- .../PowerShellContext/Session/PSReadLinePromptContext.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs index adbf1bb0d..1e28fec49 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs @@ -729,7 +729,7 @@ private async Task WritePromptStringToHostAsync(CancellationToken cancellationTo { } - PSCommand promptCommand = new PSCommand().AddScript("prompt", useLocalScope: true); + PSCommand promptCommand = new PSCommand().AddCommand("prompt"); cancellationToken.ThrowIfCancellationRequested(); string promptString = diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs index 5e40886f7..c28494faf 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs @@ -36,7 +36,7 @@ internal class PSReadLinePromptContext : IPromptContext Sort-Object -Descending Version | Select-Object -First 1 if (-not $module) {{ - Import-Module {_psReadLineModulePath} + Import-Module '{_psReadLineModulePath.Replace("'", "''")}' return [Microsoft.PowerShell.PSConsoleReadLine] }} From c402e65b7bda9226828c2453d9853ef8973aa5e1 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 5 Jun 2020 19:40:19 -0700 Subject: [PATCH 4/4] add workaround for PSGet behavior --- PowerShellEditorServices.build.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index 66a17c569..89c44260f 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -406,13 +406,18 @@ task RestorePsesModules -After Build { $splatParameters = @{ Name = $moduleName - MinimumVersion = $moduleInstallDetails.MinimumVersion - MaximumVersion = $moduleInstallDetails.MaximumVersion AllowPrerelease = $moduleInstallDetails.AllowPrerelease Repository = if ($moduleInstallDetails.Repository) { $moduleInstallDetails.Repository } else { $DefaultModuleRepository } Path = $submodulePath } + # Only add Min and Max version if we're doing a stable release. + # This is due to a PSGet issue with AllowPrerelease not installing the latest preview. + if (!$moduleInstallDetails.AllowPrerelease) { + $splatParameters.MinimumVersion = $moduleInstallDetails.MinimumVersion + $splatParameters.MaximumVersion = $moduleInstallDetails.MaximumVersion + } + Write-Host "`tInstalling module: ${moduleName} with arguments $(ConvertTo-Json $splatParameters)" Save-Module @splatParameters