Skip to content

Add support for finding and installing PowerShell Gallery modules #117

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 1 commit into from
Jan 30, 2016
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
@@ -0,0 +1,24 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

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

namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{
public class FindModuleRequest
{
public static readonly
RequestType<List<PSModuleMessage>, object> Type =
RequestType<List<PSModuleMessage>, object>.Create("powerShell/findModule");
}


public class PSModuleMessage
{
public string Name { get; set; }
public string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;

namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{
class InstallModuleRequest
{
public static readonly
RequestType<string, object> Type =
RequestType<string, object>.Create("powerShell/installModule");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<Compile Include="DebugAdapter\AttachRequest.cs" />
<Compile Include="DebugAdapter\Breakpoint.cs" />
<Compile Include="DebugAdapter\ContinueRequest.cs" />
<Compile Include="LanguageServer\FindModuleRequest.cs" />
<Compile Include="LanguageServer\InstallModuleRequest.cs" />
<Compile Include="Messages\PromptEvents.cs" />
<Compile Include="Server\DebugAdapter.cs" />
<Compile Include="Server\DebugAdapterBase.cs" />
Expand Down
46 changes: 44 additions & 2 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ protected override void Initialize()

this.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
this.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);

this.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
this.SetRequestHandler(InstallModuleRequest.Type, this.HandleInstallModuleRequest);

this.SetEventHandler(CompleteChoicePromptNotification.Type, this.HandleCompleteChoicePromptNotification);

this.SetRequestHandler(DebugAdapterMessages.EvaluateRequest.Type, this.HandleEvaluateRequest);
Expand Down Expand Up @@ -128,12 +132,28 @@ protected async Task HandleShowOnlineHelpRequest(
psCommand.AddArgument(helpParams);
psCommand.AddParameter("Online");

await editorSession.PowerShellContext.ExecuteCommand<object>(
psCommand);
await editorSession.PowerShellContext.ExecuteCommand<object>(psCommand);

await requestContext.SendResult(null);
}

private async Task HandleInstallModuleRequest(
string moduleName,
RequestContext<object> requestContext
)
{
var script = string.Format("Install-Module -Name {0} -Scope CurrentUser", moduleName);

var executeTask =
editorSession.PowerShellContext.ExecuteScriptString(
script,
true,
true).ConfigureAwait(false);

await requestContext.SendResult(null);
}


private async Task HandleExpandAliasRequest(
string content,
RequestContext<string> requestContext)
Expand Down Expand Up @@ -172,6 +192,28 @@ Sort Start -Descending
await requestContext.SendResult(result.First().ToString());
}

private async Task HandleFindModuleRequest(
object param,
RequestContext<object> requestContext)
{
var psCommand = new PSCommand();
psCommand.AddScript("Find-Module | Select Name, Description");

var modules = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);

var moduleList = new List<PSModuleMessage>();

if (modules != null)
{
foreach (dynamic m in modules)
{
moduleList.Add(new PSModuleMessage { Name = m.Name, Description = m.Description });
}
}

await requestContext.SendResult(moduleList);
}

protected Task HandleCompleteChoicePromptNotification(
CompleteChoicePromptNotification completeChoicePromptParams,
EventContext eventContext)
Expand Down