Skip to content

Enforce Get-Help offline dropback when page or internet is down #752

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

Merged
merged 7 commits into from
Sep 28, 2018
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 @@ -3,8 +3,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using System;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;

namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{
Expand All @@ -16,6 +16,7 @@ public static readonly
RequestType<string, object, object, object> Type =
RequestType<string, object, object, object>.Create("powerShell/showOnlineHelp");
}

public class ShowHelpRequest
{
public static readonly
Expand Down
27 changes: 22 additions & 5 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,39 @@ protected async Task HandleShowHelpRequest(
[String]$CommandName
)
try {
$null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
$command = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
} catch [System.Management.Automation.CommandNotFoundException] {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
try {
$null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online
} catch [System.Management.Automation.PSInvalidOperationException] {
Microsoft.PowerShell.Core\Get-Help $CommandName -Full
}";
$helpUri = [Microsoft.PowerShell.Commands.GetHelpCodeMethods]::GetHelpUri($command)

$oldSslVersion = [System.Net.ServicePointManager]::SecurityProtocol
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

# HEAD means we don't need the content itself back, just the response header
$status = (Microsoft.PowerShell.Utility\Invoke-WebRequest -Method Head -Uri $helpUri -TimeoutSec 5 -ErrorAction Stop).StatusCode
if ($status -lt 400) {
$null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online
return
}
} catch {
# Ignore - we want to drop out to Get-Help -Full
} finally {
[System.Net.ServicePointManager]::SecurityProtocol = $oldSslVersion
}

return Microsoft.PowerShell.Core\Get-Help $CommandName -Full
";

if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; }

PSCommand checkHelpPSCommand = new PSCommand()
.AddScript(CheckHelpScript, useLocalScope: true)
.AddArgument(helpParams);

// TODO: Rather than print the help in the console, we should send the string back
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually fits nicely into the changes that PowerShell/vscode-powershell#884 morphed into.

// to VSCode to display in a help pop-up (or similar)
await editorSession.PowerShellContext.ExecuteCommand<PSObject>(checkHelpPSCommand, sendOutputToHost: true);
await requestContext.SendResult(null);
}
Expand Down