3
3
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4
4
//
5
5
6
+ using Microsoft . PowerShell . EditorServices . Utility ;
6
7
using Microsoft . Windows . PowerShell . ScriptAnalyzer ;
7
8
using System ;
9
+ using System . IO ;
8
10
using System . Linq ;
9
11
using System . Management . Automation . Runspaces ;
10
12
using System . Threading ;
@@ -47,17 +49,30 @@ public class AnalysisService : IDisposable
47
49
/// </summary>
48
50
public AnalysisService ( )
49
51
{
50
- this . analysisRunspace = RunspaceFactory . CreateRunspace ( InitialSessionState . CreateDefault2 ( ) ) ;
51
- this . analysisRunspace . ApartmentState = ApartmentState . STA ;
52
- this . analysisRunspace . ThreadOptions = PSThreadOptions . ReuseThread ;
53
- this . analysisRunspace . Open ( ) ;
52
+ try
53
+ {
54
+ // Attempt to create a ScriptAnalyzer instance first
55
+ // just in case the assembly can't be found and we
56
+ // can skip creating an extra runspace.
57
+ this . scriptAnalyzer = new ScriptAnalyzer ( ) ;
58
+
59
+ this . analysisRunspace = RunspaceFactory . CreateRunspace ( InitialSessionState . CreateDefault2 ( ) ) ;
60
+ this . analysisRunspace . ApartmentState = ApartmentState . STA ;
61
+ this . analysisRunspace . ThreadOptions = PSThreadOptions . ReuseThread ;
62
+ this . analysisRunspace . Open ( ) ;
54
63
55
- this . scriptAnalyzer = new ScriptAnalyzer ( ) ;
56
- this . scriptAnalyzer . Initialize (
57
- this . analysisRunspace ,
58
- new AnalysisOutputWriter ( ) ,
59
- null ,
60
- IncludedRules ) ;
64
+ this . scriptAnalyzer . Initialize (
65
+ this . analysisRunspace ,
66
+ new AnalysisOutputWriter ( ) ,
67
+ includeRuleNames : IncludedRules ,
68
+ includeDefaultRules : true ) ;
69
+ }
70
+ catch ( FileNotFoundException )
71
+ {
72
+ Logger . Write (
73
+ LogLevel . Warning ,
74
+ "Script Analyzer binaries not found, AnalysisService will be disabled." ) ;
75
+ }
61
76
}
62
77
63
78
#endregion
@@ -72,7 +87,7 @@ public AnalysisService()
72
87
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
73
88
public ScriptFileMarker [ ] GetSemanticMarkers ( ScriptFile file )
74
89
{
75
- if ( file . IsAnalysisEnabled )
90
+ if ( this . scriptAnalyzer != null && file . IsAnalysisEnabled )
76
91
{
77
92
// TODO: This is a temporary fix until we can change how
78
93
// ScriptAnalyzer invokes their async tasks.
0 commit comments