Skip to content

Commit 587ac7b

Browse files
committed
Use runspace pool for rule helper for manifests as well
1 parent b00ac0b commit 587ac7b

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

Engine/CommandInfoCache.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1515
internal class CommandInfoCache
1616
{
1717
private readonly ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>> _commandInfoCache;
18-
private readonly RunspacePool _runspacePool;
1918
private readonly Helper _helperInstance;
19+
private readonly RunspacePool _runspacePool;
2020

2121
/// <summary>
2222
/// Create a fresh command info cache instance.
2323
/// </summary>
24-
public CommandInfoCache(Helper pssaHelperInstance)
24+
public CommandInfoCache(Helper pssaHelperInstance, RunspacePool runspacePool)
2525
{
2626
_commandInfoCache = new ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>>();
2727
_helperInstance = pssaHelperInstance;
28-
// There are only 4 rules that use the CommandInfo cache and each rule does not request more than one concurrent command info request
29-
_runspacePool = RunspaceFactory.CreateRunspacePool(1, 5);
30-
_runspacePool.Open();
28+
_runspacePool = runspacePool;
3129
}
3230

3331
/// <summary>

Engine/Helper.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using System.Management.Automation;
1313
using System.Management.Automation.Language;
14+
using System.Management.Automation.Runspaces;
1415

1516
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1617
{
@@ -29,6 +30,7 @@ public class Helper
2930
private PSVersionTable psVersionTable;
3031

3132
private readonly Lazy<CommandInfoCache> _commandInfoCacheLazy;
33+
private readonly RunspacePool _runSpacePool;
3234

3335
#endregion
3436

@@ -113,7 +115,10 @@ internal set
113115
/// </summary>
114116
private Helper()
115117
{
116-
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache(pssaHelperInstance: this));
118+
// There are 5 rules that use the CommandInfo cache and each rule does not request more than one concurrent command info request
119+
_runSpacePool = RunspaceFactory.CreateRunspacePool(1, 6);
120+
_runSpacePool.Open();
121+
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache(pssaHelperInstance: this, runspacePool: _runSpacePool));
117122
}
118123

119124
/// <summary>
@@ -299,11 +304,12 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
299304
Collection<PSObject> psObj = null;
300305
using (var ps = System.Management.Automation.PowerShell.Create())
301306
{
307+
ps.RunspacePool = _runSpacePool;
308+
ps.AddCommand("Test-ModuleManifest");
309+
ps.AddParameter("Path", filePath);
310+
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
302311
try
303312
{
304-
ps.AddCommand("Test-ModuleManifest");
305-
ps.AddParameter("Path", filePath);
306-
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
307313
psObj = ps.Invoke();
308314
}
309315
catch (CmdletInvocationException e)
@@ -653,31 +659,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona
653659
return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0;
654660
}
655661

656-
657-
/// <summary>
658-
/// Get a CommandInfo object of the given command name
659-
/// </summary>
660-
/// <returns>Returns null if command does not exists</returns>
661-
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
662-
{
663-
using (var ps = System.Management.Automation.PowerShell.Create())
664-
{
665-
var psCommand = ps.AddCommand("Get-Command")
666-
.AddParameter("Name", cmdName)
667-
.AddParameter("ErrorAction", "SilentlyContinue");
668-
669-
if(commandType!=null)
670-
{
671-
psCommand.AddParameter("CommandType", commandType);
672-
}
673-
674-
var commandInfo = psCommand.Invoke<CommandInfo>()
675-
.FirstOrDefault();
676-
677-
return commandInfo;
678-
}
679-
}
680-
681662
/// <summary>
682663
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
683664
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.

0 commit comments

Comments
 (0)