5
5
using System . Collections ;
6
6
using System . Collections . Generic ;
7
7
using System . Collections . ObjectModel ;
8
- using System . IO ;
9
8
using System . Linq ;
10
9
using System . Text ;
11
10
using System . Threading . Tasks ;
@@ -67,35 +66,27 @@ public Builder WithIncludedRules(string[] rules)
67
66
/// If PSScriptAnalyzer cannot be found, this will return null.
68
67
/// </summary>
69
68
/// <returns>A newly configured PssaCmdletAnalysisEngine, or null if PSScriptAnalyzer cannot be found.</returns>
70
- public PssaCmdletAnalysisEngine Build ( )
69
+ public PssaCmdletAnalysisEngine Build ( string pssaModulePath )
71
70
{
72
71
// RunspacePool takes care of queuing commands for us so we do not
73
72
// need to worry about executing concurrent commands
74
73
ILogger logger = _loggerFactory . CreateLogger < PssaCmdletAnalysisEngine > ( ) ;
75
74
try
76
75
{
77
- RunspacePool pssaRunspacePool = CreatePssaRunspacePool ( ) ;
78
-
79
- PssaCmdletAnalysisEngine cmdletAnalysisEngine = _settingsParameter is not null
80
- ? new PssaCmdletAnalysisEngine ( logger , pssaRunspacePool , _settingsParameter )
81
- : new PssaCmdletAnalysisEngine ( logger , pssaRunspacePool , _rules ) ;
82
-
76
+ logger . LogDebug ( "Creating PSScriptAnalyzer runspace with module at: '{Path}'" , pssaModulePath ) ;
77
+ RunspacePool pssaRunspacePool = CreatePssaRunspacePool ( pssaModulePath ) ;
78
+ PssaCmdletAnalysisEngine cmdletAnalysisEngine = new ( logger , pssaRunspacePool , _settingsParameter ?? _rules ) ;
83
79
cmdletAnalysisEngine . LogAvailablePssaFeatures ( ) ;
84
80
return cmdletAnalysisEngine ;
85
81
}
86
- catch ( FileNotFoundException e )
82
+ catch ( Exception ex )
87
83
{
88
- logger . LogError ( e , $ "Unable to find PSScriptAnalyzer. Disabling script analysis. PSModulePath: ' { Environment . GetEnvironmentVariable ( "PSModulePath" ) } ' ") ;
84
+ logger . LogError ( ex , "Unable to load PSScriptAnalyzer, disabling script analysis! " ) ;
89
85
return null ;
90
86
}
91
87
}
92
88
}
93
89
94
- // This is a default that can be overriden at runtime by the user or tests.
95
- // TODO: Deduplicate this logic with PsesInternalHost.
96
- private static readonly string s_pssaModulePath = Path . GetFullPath ( Path . Combine (
97
- Path . GetDirectoryName ( typeof ( PssaCmdletAnalysisEngine ) . Assembly . Location ) , ".." , ".." , ".." , "PSScriptAnalyzer" ) ) ;
98
-
99
90
/// <summary>
100
91
/// The indentation to add when the logger lists errors.
101
92
/// </summary>
@@ -365,7 +356,7 @@ private IEnumerable<string> GetPSScriptAnalyzerRules()
365
356
/// This looks for the latest version of PSScriptAnalyzer on the path and loads that.
366
357
/// </summary>
367
358
/// <returns>A runspace pool with PSScriptAnalyzer loaded for running script analysis tasks.</returns>
368
- private static RunspacePool CreatePssaRunspacePool ( )
359
+ private static RunspacePool CreatePssaRunspacePool ( string pssaModulePath )
369
360
{
370
361
using PowerShell pwsh = PowerShell . Create ( RunspaceMode . NewRunspace ) ;
371
362
@@ -375,10 +366,7 @@ private static RunspacePool CreatePssaRunspacePool()
375
366
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core`
376
367
// only, which is a more minimal and therefore safer state.
377
368
InitialSessionState sessionState = InitialSessionState . CreateDefault2 ( ) ;
378
-
379
- sessionState . ImportPSModulesFromPath ( s_pssaModulePath ) ;
380
- // pwsh.ImportModule(s_pssaModulePath);
381
- // sessionState.ImportPSModule(new[] { pssaModuleInfo.ModuleBase });
369
+ sessionState . ImportPSModulesFromPath ( pssaModulePath ) ;
382
370
383
371
RunspacePool runspacePool = RunspaceFactory . CreateRunspacePool ( sessionState ) ;
384
372
0 commit comments