Skip to content

Commit 3699310

Browse files
committed
Supports specifying a settings path to use with script analyzer.
Work in progress. The setting only works once when the extension is first loaded and then only for the global location. More work to do here to figure out how get workspace relative paths work.
1 parent 9130e75 commit 3699310

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
77
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
9-
using Microsoft.PowerShell.EditorServices.Protocol.Messages;
109
using Microsoft.PowerShell.EditorServices.Utility;
1110
using System;
1211
using System.Collections.Generic;
@@ -292,10 +291,20 @@ protected async Task HandleDidChangeConfigurationNotification(
292291
{
293292
bool oldScriptAnalysisEnabled =
294293
this.currentSettings.ScriptAnalysis.Enable.HasValue;
294+
string oldScriptAnalysisSettingsPath =
295+
this.currentSettings.ScriptAnalysis.SettingsPath;
295296

296297
this.currentSettings.Update(
297298
configChangeParams.Settings.Powershell);
298299

300+
string newSettingsPath = this.currentSettings.ScriptAnalysis.SettingsPath;
301+
302+
// If there is a new settings file path, restart the analyzer with the new settigs.
303+
if (!(oldScriptAnalysisSettingsPath?.Equals(newSettingsPath, StringComparison.OrdinalIgnoreCase) ?? false))
304+
{
305+
this.editorSession.RestartAnalysisService(newSettingsPath);
306+
}
307+
299308
if (oldScriptAnalysisEnabled != this.currentSettings.ScriptAnalysis.Enable)
300309
{
301310
// If the user just turned off script analysis, send a diagnostics

src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class ScriptAnalysisSettings
2727
{
2828
public bool? Enable { get; set; }
2929

30+
public string SettingsPath { get; set; }
31+
3032
public ScriptAnalysisSettings()
3133
{
3234
this.Enable = true;
@@ -37,6 +39,7 @@ public void Update(ScriptAnalysisSettings settings)
3739
if (settings != null)
3840
{
3941
this.Enable = settings.Enable;
42+
this.SettingsPath = settings.SettingsPath;
4043
}
4144
}
4245
}

src/PowerShellEditorServices/Analysis/AnalysisOutputWriter.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
6+
using System;
77
using System.Management.Automation;
8+
using Microsoft.PowerShell.EditorServices.Console;
9+
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
810

911
namespace Microsoft.PowerShell.EditorServices
1012
{
@@ -14,31 +16,36 @@ namespace Microsoft.PowerShell.EditorServices
1416
/// </summary>
1517
internal class AnalysisOutputWriter : IOutputWriter
1618
{
19+
private IConsoleHost consoleHost;
20+
21+
public AnalysisOutputWriter(IConsoleHost consoleHost)
22+
{
23+
this.consoleHost = consoleHost;
24+
}
25+
1726
#region IOutputWriter Implementation
1827

1928
void IOutputWriter.WriteError(ErrorRecord error)
2029
{
21-
// TODO: Find a way to trace out this output!
30+
this.consoleHost?.WriteOutput(error.ToString(), true, OutputType.Error, ConsoleColor.Red, ConsoleColor.Black);
2231
}
2332

2433
void IOutputWriter.WriteWarning(string message)
2534
{
26-
// TODO: Find a way to trace out this output!
35+
this.consoleHost?.WriteOutput(message, true, OutputType.Warning, ConsoleColor.Yellow, ConsoleColor.Black);
2736
}
2837

2938
void IOutputWriter.WriteVerbose(string message)
3039
{
31-
// TODO: Find a way to trace out this output!
3240
}
3341

3442
void IOutputWriter.WriteDebug(string message)
3543
{
36-
// TODO: Find a way to trace out this output!
3744
}
3845

3946
void IOutputWriter.ThrowTerminatingError(ErrorRecord record)
4047
{
41-
// TODO: Find a way to trace out this output!
48+
this.consoleHost?.WriteOutput(record.ToString(), true, OutputType.Error, ConsoleColor.Red, ConsoleColor.Black);
4249
}
4350

4451
#endregion

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Management.Automation.Runspaces;
1212
using System.Threading;
1313
using System.Threading.Tasks;
14+
using Microsoft.PowerShell.EditorServices.Console;
1415

1516
namespace Microsoft.PowerShell.EditorServices
1617
{
@@ -26,11 +27,10 @@ public class AnalysisService : IDisposable
2627
private ScriptAnalyzer scriptAnalyzer;
2728

2829
/// <summary>
29-
/// Defines the list of Script Analyzer rules to include by default.
30-
/// In the future, a default rule set from Script Analyzer may be used.
30+
/// Defines the list of Script Analyzer rules to include by default if
31+
/// no settings file can be found.
3132
/// </summary>
32-
private static readonly string[] IncludedRules = new string[]
33-
{
33+
private static readonly string[] IncludedRules = {
3434
"PSUseApprovedVerbs",
3535
"PSReservedCmdletChar",
3636
"PSReservedParams",
@@ -47,7 +47,10 @@ public class AnalysisService : IDisposable
4747
/// <summary>
4848
/// Creates an instance of the AnalysisService class.
4949
/// </summary>
50-
public AnalysisService()
50+
/// <param name="consoleHost">An object that implements IConsoleHost in which to write errors/warnings
51+
/// from analyzer.</param>
52+
/// <param name="settingsPath">Path to a PSScriptAnalyzer settings file.</param>
53+
public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
5154
{
5255
try
5356
{
@@ -63,9 +66,10 @@ public AnalysisService()
6366

6467
this.scriptAnalyzer.Initialize(
6568
this.analysisRunspace,
66-
new AnalysisOutputWriter(),
67-
includeRuleNames: IncludedRules,
68-
includeDefaultRules: true);
69+
new AnalysisOutputWriter(consoleHost),
70+
includeRuleNames: settingsPath == null ? IncludedRules : null,
71+
includeDefaultRules: true,
72+
profile: settingsPath);
6973
}
7074
catch (FileNotFoundException)
7175
{

src/PowerShellEditorServices/Session/EditorSession.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55

66
using Microsoft.PowerShell.EditorServices.Console;
77
using Microsoft.PowerShell.EditorServices.Utility;
8-
using System;
98
using System.IO;
10-
using System.Management.Automation;
11-
using System.Management.Automation.Runspaces;
12-
using System.Reflection;
13-
using System.Threading;
149

1510
namespace Microsoft.PowerShell.EditorServices
1611
{
@@ -68,6 +63,24 @@ public void StartSession()
6863
this.DebugService = new DebugService(this.PowerShellContext);
6964
this.ConsoleService = new ConsoleService(this.PowerShellContext);
7065

66+
this.InstantiateAnalysisService();
67+
68+
// Create a workspace to contain open files
69+
this.Workspace = new Workspace(this.PowerShellContext.PowerShellVersion);
70+
}
71+
72+
/// <summary>
73+
/// Restarts the AnalysisService so it can be configured with a new settings file.
74+
/// </summary>
75+
/// <param name="settingsPath">Path to the settings file.</param>
76+
public void RestartAnalysisService(string settingsPath)
77+
{
78+
this.AnalysisService?.Dispose();
79+
InstantiateAnalysisService(settingsPath);
80+
}
81+
82+
internal void InstantiateAnalysisService(string settingsPath = null)
83+
{
7184
// Only enable the AnalysisService if the machine has PowerShell
7285
// v5 installed. Script Analyzer works on earlier PowerShell
7386
// versions but our hard dependency on their binaries complicates
@@ -81,7 +94,7 @@ public void StartSession()
8194
// Script Analyzer binaries are not included.
8295
try
8396
{
84-
this.AnalysisService = new AnalysisService();
97+
this.AnalysisService = new AnalysisService(this.PowerShellContext.ConsoleHost, settingsPath);
8598
}
8699
catch (FileNotFoundException)
87100
{
@@ -97,9 +110,6 @@ public void StartSession()
97110
"Script Analyzer cannot be loaded due to unsupported PowerShell version " +
98111
this.PowerShellContext.PowerShellVersion.ToString());
99112
}
100-
101-
// Create a workspace to contain open files
102-
this.Workspace = new Workspace(this.PowerShellContext.PowerShellVersion);
103113
}
104114

105115
#endregion

0 commit comments

Comments
 (0)