@@ -110,7 +110,8 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
110
110
// need to worry about executing concurrent commands
111
111
this . analysisRunspacePool = RunspaceFactory . CreateRunspacePool ( sessionState ) ;
112
112
113
- // one runspace for code formatting and the other for markers
113
+ // having more than one runspace doesn't block code formatting if one
114
+ // runspace is occupied for diagnostics
114
115
this . analysisRunspacePool . SetMaxRunspaces ( 2 ) ;
115
116
this . analysisRunspacePool . ThreadOptions = PSThreadOptions . ReuseThread ;
116
117
this . analysisRunspacePool . Open ( ) ;
@@ -136,9 +137,9 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
136
137
/// </summary>
137
138
/// <param name="file">The ScriptFile which will be analyzed for semantic markers.</param>
138
139
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
139
- public ScriptFileMarker [ ] GetSemanticMarkers ( ScriptFile file )
140
+ public async Task < ScriptFileMarker [ ] > GetSemanticMarkersAsync ( ScriptFile file )
140
141
{
141
- return GetSemanticMarkers ( file , activeRules , settingsPath ) ;
142
+ return await GetSemanticMarkersAsync ( file , activeRules , settingsPath ) ;
142
143
}
143
144
144
145
/// <summary>
@@ -147,9 +148,9 @@ public ScriptFileMarker[] GetSemanticMarkers(ScriptFile file)
147
148
/// <param name="file">The ScriptFile to be analyzed.</param>
148
149
/// <param name="settings">ScriptAnalyzer settings</param>
149
150
/// <returns></returns>
150
- public ScriptFileMarker [ ] GetSemanticMarkers ( ScriptFile file , Hashtable settings )
151
+ public async Task < ScriptFileMarker [ ] > GetSemanticMarkersAsync ( ScriptFile file , Hashtable settings )
151
152
{
152
- return GetSemanticMarkers < Hashtable > ( file , null , settings ) ;
153
+ return await GetSemanticMarkersAsync < Hashtable > ( file , null , settings ) ;
153
154
}
154
155
155
156
/// <summary>
@@ -208,7 +209,7 @@ public void Dispose()
208
209
209
210
#region Private Methods
210
211
211
- private ScriptFileMarker [ ] GetSemanticMarkers < TSettings > (
212
+ private async Task < ScriptFileMarker [ ] > GetSemanticMarkersAsync < TSettings > (
212
213
ScriptFile file ,
213
214
string [ ] rules ,
214
215
TSettings settings ) where TSettings : class
@@ -218,23 +219,8 @@ private ScriptFileMarker[] GetSemanticMarkers<TSettings>(
218
219
&& ( typeof ( TSettings ) == typeof ( string ) || typeof ( TSettings ) == typeof ( Hashtable ) )
219
220
&& ( rules != null || settings != null ) )
220
221
{
221
- // TODO: This is a temporary fix until we can change how
222
- // ScriptAnalyzer invokes their async tasks.
223
- // TODO: Make this async
224
- Task < ScriptFileMarker [ ] > analysisTask =
225
- Task . Factory . StartNew < ScriptFileMarker [ ] > (
226
- ( ) =>
227
- {
228
- return
229
- GetDiagnosticRecords ( file , rules , settings )
230
- . Select ( ScriptFileMarker . FromDiagnosticRecord )
231
- . ToArray ( ) ;
232
- } ,
233
- CancellationToken . None ,
234
- TaskCreationOptions . None ,
235
- TaskScheduler . Default ) ;
236
- analysisTask . Wait ( ) ;
237
- return analysisTask . Result ;
222
+ var scriptFileMarkers = await GetDiagnosticRecordsAsync ( file , rules , settings ) ;
223
+ return scriptFileMarkers . Select ( ScriptFileMarker . FromDiagnosticRecord ) . ToArray ( ) ;
238
224
}
239
225
else
240
226
{
@@ -324,23 +310,6 @@ private void InitializePSScriptAnalyzer()
324
310
EnumeratePSScriptAnalyzerRules ( ) ;
325
311
}
326
312
327
- private IEnumerable < PSObject > GetDiagnosticRecords ( ScriptFile file )
328
- {
329
- return GetDiagnosticRecords ( file , this . activeRules , this . settingsPath ) ;
330
- }
331
-
332
- // TSettings can either be of type Hashtable or string
333
- // as scriptanalyzer settings parameter takes either a hashtable or string
334
- private IEnumerable < PSObject > GetDiagnosticRecords < TSettings > (
335
- ScriptFile file ,
336
- string [ ] rules ,
337
- TSettings settings ) where TSettings : class
338
- {
339
- var task = GetDiagnosticRecordsAsync ( file , rules , settings ) ;
340
- task . Wait ( ) ;
341
- return task . Result ;
342
- }
343
-
344
313
private async Task < IEnumerable < PSObject > > GetDiagnosticRecordsAsync < TSettings > (
345
314
ScriptFile file ,
346
315
string [ ] rules ,
@@ -366,7 +335,6 @@ private async Task<IEnumerable<PSObject>> GetDiagnosticRecordsAsync<TSettings>(
366
335
settingArgument = rules ;
367
336
}
368
337
369
-
370
338
diagnosticRecords = await InvokePowerShellAsync (
371
339
"Invoke-ScriptAnalyzer" ,
372
340
new Dictionary < string , object >
@@ -392,31 +360,23 @@ private IEnumerable<PSObject> InvokePowerShell(string command, IDictionary<strin
392
360
393
361
private async Task < IEnumerable < PSObject > > InvokePowerShellAsync ( string command , IDictionary < string , object > paramArgMap )
394
362
{
395
- var task = Task . Run ( ( ) =>
363
+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
396
364
{
397
- using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
365
+ powerShell . RunspacePool = this . analysisRunspacePool ;
366
+ powerShell . AddCommand ( command ) ;
367
+ foreach ( var kvp in paramArgMap )
398
368
{
399
- powerShell . RunspacePool = this . analysisRunspacePool ;
400
- powerShell . AddCommand ( command ) ;
401
- foreach ( var kvp in paramArgMap )
402
- {
403
- powerShell . AddParameter ( kvp . Key , kvp . Value ) ;
404
- }
405
-
406
- var powerShellCommandResult = powerShell . BeginInvoke ( ) ;
407
- var result = powerShell . EndInvoke ( powerShellCommandResult ) ;
408
-
409
- if ( result == null )
410
- {
411
- return Enumerable . Empty < PSObject > ( ) ;
412
- }
369
+ powerShell . AddParameter ( kvp . Key , kvp . Value ) ;
370
+ }
413
371
414
- return result ;
372
+ var result = await Task . Factory . FromAsync ( powerShell . BeginInvoke ( ) , powerShell . EndInvoke ) ;
373
+ if ( result == null )
374
+ {
375
+ return Enumerable . Empty < PSObject > ( ) ;
415
376
}
416
- } ) ;
417
377
418
- await task ;
419
- return task . Result ;
378
+ return result ;
379
+ }
420
380
}
421
381
422
382
#endregion //private methods
0 commit comments