Skip to content

Commit 3a444ce

Browse files
author
Kapil Borle
committed
Add async method to invoke PS command
1 parent f8f6f32 commit 3a444ce

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -346,39 +346,45 @@ private IEnumerable<PSObject> GetDiagnosticRecords<TSettings>(
346346
ScriptFile file,
347347
string[] rules,
348348
TSettings settings) where TSettings: class
349+
{
350+
var task = GetDiagnosticRecordsAsync(file, rules, settings);
351+
task.Wait();
352+
return task.Result;
353+
}
354+
355+
private async Task<IEnumerable<PSObject>> GetDiagnosticRecordsAsync<TSettings>(
356+
ScriptFile file,
357+
string[] rules,
358+
TSettings settings) where TSettings : class
349359
{
350360
IEnumerable<PSObject> diagnosticRecords = Enumerable.Empty<PSObject>();
351361

352362
if (this.scriptAnalyzerModuleInfo != null
353363
&& (typeof(TSettings) == typeof(string)
354364
|| typeof(TSettings) == typeof(Hashtable)))
355365
{
356-
lock (runspaceLock)
366+
//Use a settings file if one is provided, otherwise use the default rule list.
367+
string settingParameter;
368+
object settingArgument;
369+
if (settings != null)
357370
{
358-
using (var powerShell = System.Management.Automation.PowerShell.Create())
359-
{
360-
powerShell.Runspace = this.analysisRunspace;
361-
Logger.Write(
362-
LogLevel.Verbose,
363-
String.Format("Running PSScriptAnalyzer against {0}", file.FilePath));
364-
365-
powerShell
366-
.AddCommand("Invoke-ScriptAnalyzer")
367-
.AddParameter("ScriptDefinition", file.Contents);
371+
settingParameter = "Settings";
372+
settingArgument = settings;
373+
}
374+
else
375+
{
376+
settingParameter = "IncludeRule";
377+
settingArgument = rules;
378+
}
368379

369-
// Use a settings file if one is provided, otherwise use the default rule list.
370-
if (settings != null)
371-
{
372-
powerShell.AddParameter("Settings", settings);
373-
}
374-
else
375-
{
376-
powerShell.AddParameter("IncludeRule", rules);
377-
}
378380

379-
diagnosticRecords = powerShell.Invoke();
380-
}
381-
}
381+
diagnosticRecords = await InvokePowerShellAsync(
382+
"Invoke-ScriptAnalyzer",
383+
new Dictionary<string, object>
384+
{
385+
{ "ScriptDefinition", file.Contents },
386+
{ settingParameter, settingArgument }
387+
});
382388
}
383389

384390
Logger.Write(
@@ -388,6 +394,35 @@ private IEnumerable<PSObject> GetDiagnosticRecords<TSettings>(
388394
return diagnosticRecords;
389395
}
390396

397+
private async Task<IEnumerable<PSObject>> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap)
398+
{
399+
var task = Task.Run(() =>
400+
{
401+
using (var powerShell = System.Management.Automation.PowerShell.Create())
402+
{
403+
powerShell.Runspace = this.analysisRunspace;
404+
powerShell.AddCommand(command);
405+
foreach (var kvp in paramArgMap)
406+
{
407+
powerShell.AddParameter(kvp.Key, kvp.Value);
408+
}
409+
410+
var powerShellCommandResult = powerShell.BeginInvoke();
411+
var result = powerShell.EndInvoke(powerShellCommandResult);
412+
413+
if (result == null)
414+
{
415+
return Enumerable.Empty<PSObject>();
416+
}
417+
418+
return result;
419+
}
420+
});
421+
422+
await task;
423+
return task.Result;
424+
}
425+
391426
#endregion //private methods
392427
}
393428
}

0 commit comments

Comments
 (0)