Skip to content

Commit c63b449

Browse files
author
Kapil Borle
committed
Add handler to return PSSA rules
1 parent d342685 commit c63b449

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed
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 GetPSSARulesRequest
11+
{
12+
public static readonly
13+
RequestType<string, object> Type =
14+
RequestType<string, object>.Create("powerShell/GetPSSARules");
15+
}
16+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<Compile Include="DebugAdapter\ContinueRequest.cs" />
5656
<Compile Include="DebugAdapter\SetFunctionBreakpointsRequest.cs" />
5757
<Compile Include="DebugAdapter\SetVariableRequest.cs" />
58+
<Compile Include="LanguageServer\GetPSSARulesRequest.cs" />
5859
<Compile Include="LanguageServer\EditorCommands.cs" />
5960
<Compile Include="LanguageServer\CodeAction.cs" />
6061
<Compile Include="LanguageServer\FindModuleRequest.cs" />

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ protected override void Initialize()
109109

110110
this.SetRequestHandler(DebugAdapterMessages.EvaluateRequest.Type, this.HandleEvaluateRequest);
111111

112+
this.SetRequestHandler(GetPSSARulesRequest.Type, this.HandleGetPSSARulesRequest);
113+
112114
// Initialize the extension service
113115
// TODO: This should be made awaited once Initialize is async!
114116
this.editorSession.ExtensionService.Initialize(
@@ -180,6 +182,22 @@ protected async Task HandleShowOnlineHelpRequest(
180182
await requestContext.SendResult(null);
181183
}
182184

185+
private async Task HandleGetPSSARulesRequest(
186+
object param,
187+
RequestContext<object> requestContext)
188+
{
189+
List<string> ruleList = new List<string>();
190+
if (editorSession.AnalysisService != null)
191+
{
192+
var ruleNames = editorSession.AnalysisService.GetPSScriptAnalyzerRules();
193+
foreach (var ruleName in ruleNames)
194+
{
195+
ruleList.Add(ruleName);
196+
}
197+
}
198+
await requestContext.SendResult(ruleList);
199+
}
200+
183201
private async Task HandleInstallModuleRequest(
184202
string moduleName,
185203
RequestContext<object> requestContext

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ public void Dispose()
142142
}
143143
}
144144

145+
public IEnumerable<string> GetPSScriptAnalyzerRules()
146+
{
147+
List<string> ruleNames = new List<string>();
148+
if (scriptAnalyzerModuleInfo != null)
149+
{
150+
using (var ps = System.Management.Automation.PowerShell.Create())
151+
{
152+
ps.Runspace = this.analysisRunspace;
153+
var ruleObjects = ps.AddCommand("Get-ScriptAnalyzerRule").Invoke();
154+
foreach(var rule in ruleObjects)
155+
{
156+
ruleNames.Add((string)rule.Members["RuleName"].Value);
157+
}
158+
}
159+
}
160+
161+
return ruleNames;
162+
}
163+
145164
#endregion // public methods
146165

147166
#region Private Methods
@@ -207,23 +226,16 @@ private void EnumeratePSScriptAnalyzerRules()
207226
{
208227
if (scriptAnalyzerModuleInfo != null)
209228
{
210-
using (var ps = System.Management.Automation.PowerShell.Create())
229+
var rules = GetPSScriptAnalyzerRules();
230+
var sb = new StringBuilder();
231+
sb.AppendLine("Available PSScriptAnalyzer Rules:");
232+
foreach (var rule in rules)
211233
{
212-
ps.Runspace = this.analysisRunspace;
213-
214-
var rules = ps.AddCommand("Get-ScriptAnalyzerRule").Invoke();
215-
var sb = new StringBuilder();
216-
sb.AppendLine("Available PSScriptAnalyzer Rules:");
217-
218-
foreach (var rule in rules)
219-
{
220-
sb.AppendLine((string)rule.Members["RuleName"].Value);
221-
}
222-
223-
Logger.Write(LogLevel.Verbose, sb.ToString());
234+
sb.AppendLine(rule);
224235
}
225-
}
226236

237+
Logger.Write(LogLevel.Verbose, sb.ToString());
238+
}
227239
}
228240

229241
private void InitializePSScriptAnalyzer()

src/PowerShellEditorServices/Nano.PowerShellEditorServices.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<DocumentationFile>bin\Release\Microsoft.PowerShell.EditorServices.XML</DocumentationFile>
3737
</PropertyGroup>
3838
<ItemGroup>
39+
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\GetPSSARulesRequest.cs">
40+
<Link>GetPSSARulesRequest.cs</Link>
41+
</Compile>
3942
<Compile Include="Analysis\AnalysisOutputWriter.cs" />
4043
<Compile Include="Analysis\AnalysisService.cs" />
4144
<Compile Include="Console\ChoicePromptHandler.cs" />

0 commit comments

Comments
 (0)