Skip to content

Commit a5cf548

Browse files
committed
Merge pull request #117 from PowerShell/spike-dcf-psgallery
Add support for finding and installing PowerShell Gallery modules
2 parents 8636d56 + 479d635 commit a5cf548

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
using System.Collections.Generic;
8+
9+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
10+
{
11+
public class FindModuleRequest
12+
{
13+
public static readonly
14+
RequestType<List<PSModuleMessage>, object> Type =
15+
RequestType<List<PSModuleMessage>, object>.Create("powerShell/findModule");
16+
}
17+
18+
19+
public class PSModuleMessage
20+
{
21+
public string Name { get; set; }
22+
public string Description { get; set; }
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
9+
{
10+
class InstallModuleRequest
11+
{
12+
public static readonly
13+
RequestType<string, object> Type =
14+
RequestType<string, object>.Create("powerShell/installModule");
15+
}
16+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
<Compile Include="DebugAdapter\AttachRequest.cs" />
5353
<Compile Include="DebugAdapter\Breakpoint.cs" />
5454
<Compile Include="DebugAdapter\ContinueRequest.cs" />
55+
<Compile Include="LanguageServer\FindModuleRequest.cs" />
56+
<Compile Include="LanguageServer\InstallModuleRequest.cs" />
5557
<Compile Include="Messages\PromptEvents.cs" />
5658
<Compile Include="Server\DebugAdapter.cs" />
5759
<Compile Include="Server\DebugAdapterBase.cs" />

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ protected override void Initialize()
6767

6868
this.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
6969
this.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
70+
71+
this.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
72+
this.SetRequestHandler(InstallModuleRequest.Type, this.HandleInstallModuleRequest);
73+
7074
this.SetEventHandler(CompleteChoicePromptNotification.Type, this.HandleCompleteChoicePromptNotification);
7175

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

131-
await editorSession.PowerShellContext.ExecuteCommand<object>(
132-
psCommand);
135+
await editorSession.PowerShellContext.ExecuteCommand<object>(psCommand);
136+
137+
await requestContext.SendResult(null);
138+
}
139+
140+
private async Task HandleInstallModuleRequest(
141+
string moduleName,
142+
RequestContext<object> requestContext
143+
)
144+
{
145+
var script = string.Format("Install-Module -Name {0} -Scope CurrentUser", moduleName);
146+
147+
var executeTask =
148+
editorSession.PowerShellContext.ExecuteScriptString(
149+
script,
150+
true,
151+
true).ConfigureAwait(false);
133152

134153
await requestContext.SendResult(null);
135154
}
136155

156+
137157
private async Task HandleExpandAliasRequest(
138158
string content,
139159
RequestContext<string> requestContext)
@@ -172,6 +192,28 @@ Sort Start -Descending
172192
await requestContext.SendResult(result.First().ToString());
173193
}
174194

195+
private async Task HandleFindModuleRequest(
196+
object param,
197+
RequestContext<object> requestContext)
198+
{
199+
var psCommand = new PSCommand();
200+
psCommand.AddScript("Find-Module | Select Name, Description");
201+
202+
var modules = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);
203+
204+
var moduleList = new List<PSModuleMessage>();
205+
206+
if (modules != null)
207+
{
208+
foreach (dynamic m in modules)
209+
{
210+
moduleList.Add(new PSModuleMessage { Name = m.Name, Description = m.Description });
211+
}
212+
}
213+
214+
await requestContext.SendResult(moduleList);
215+
}
216+
175217
protected Task HandleCompleteChoicePromptNotification(
176218
CompleteChoicePromptNotification completeChoicePromptParams,
177219
EventContext eventContext)

0 commit comments

Comments
 (0)