25
25
using System . Threading . Tasks ;
26
26
using System . Collections . Concurrent ;
27
27
using System . Threading ;
28
+ using System . Management . Automation . Runspaces ;
28
29
29
30
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
30
31
{
@@ -44,7 +45,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
44
45
[ Parameter ( Position = 0 ,
45
46
ParameterSetName = "File" ,
46
47
Mandatory = true ,
47
- ValueFromPipeline = true ,
48
+ ValueFromPipeline = true ,
48
49
ValueFromPipelineByPropertyName = true ) ]
49
50
[ ValidateNotNull ]
50
51
[ Alias ( "PSPath" ) ]
@@ -190,7 +191,7 @@ public object Settings
190
191
191
192
/// <summary>
192
193
/// Resolve DSC resoure dependency
193
- /// </summary>
194
+ /// </summary>
194
195
[ Parameter ( Mandatory = false ) ]
195
196
public SwitchParameter ResolveDscResourceDependency
196
197
{
@@ -223,8 +224,7 @@ protected override void BeginProcessing()
223
224
this . excludeRule ,
224
225
this . severity ,
225
226
null == rulePaths ? true : this . includeDefaultRules ,
226
- this . suppressedOnly ,
227
- resolveDscResourceDependency : resolveDscResourceDependency ) ;
227
+ this . suppressedOnly ) ;
228
228
}
229
229
230
230
/// <summary>
@@ -238,18 +238,50 @@ protected override void ProcessRecord()
238
238
return ;
239
239
}
240
240
241
+ // TODO Support dependency resolution for analyzing script definitions
242
+ if ( resolveDscResourceDependency )
243
+ {
244
+ using ( var rsp = RunspaceFactory . CreateRunspace ( ) )
245
+ {
246
+ rsp . Open ( ) ;
247
+ using ( var moduleHandler = new ModuleDependencyHandler ( rsp ) )
248
+ {
249
+ ScriptAnalyzer . Instance . ModuleHandler = moduleHandler ;
250
+ ProcessInput ( ) ;
251
+ }
252
+ }
253
+ }
254
+ else
255
+ {
256
+ ProcessInput ( ) ;
257
+ }
258
+ }
259
+
260
+ private void ProcessInput ( )
261
+ {
262
+ IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
241
263
if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
242
264
{
243
- // throws Item Not Found Exception
265
+ // throws Item Not Found Exception
244
266
Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
245
267
foreach ( PathInfo p in paths )
246
268
{
247
- ProcessPathOrScriptDefinition ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
269
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath (
270
+ this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ,
271
+ this . recurse ) ;
248
272
}
249
273
}
250
274
else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
251
275
{
252
- ProcessPathOrScriptDefinition ( scriptDefinition ) ;
276
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( scriptDefinition ) ;
277
+ }
278
+
279
+ foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
280
+ {
281
+ foreach ( DiagnosticRecord diagnostic in diagnosticsList )
282
+ {
283
+ logger . LogObject ( diagnostic , this ) ;
284
+ }
253
285
}
254
286
}
255
287
@@ -269,28 +301,28 @@ protected override void StopProcessing()
269
301
270
302
#region Methods
271
303
272
- private void ProcessPathOrScriptDefinition ( string pathOrScriptDefinition )
273
- {
274
- IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
304
+ // private void ProcessPathOrScriptDefinition(string pathOrScriptDefinition)
305
+ // {
306
+ // IEnumerable<DiagnosticRecord> diagnosticsList = Enumerable.Empty<DiagnosticRecord>();
275
307
276
- if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
277
- {
278
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath ( pathOrScriptDefinition , this . recurse ) ;
279
- }
280
- else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
281
- {
282
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( pathOrScriptDefinition ) ;
283
- }
308
+ // if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
309
+ // {
310
+ // diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(pathOrScriptDefinition, this.recurse);
311
+ // }
312
+ // else if (String.Equals(this.ParameterSetName, "ScriptDefinition", StringComparison.OrdinalIgnoreCase))
313
+ // {
314
+ // diagnosticsList = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(pathOrScriptDefinition);
315
+ // }
284
316
285
- //Output through loggers
286
- foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
287
- {
288
- foreach ( DiagnosticRecord diagnostic in diagnosticsList )
289
- {
290
- logger . LogObject ( diagnostic , this ) ;
291
- }
292
- }
293
- }
317
+ // //Output through loggers
318
+ // foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
319
+ // {
320
+ // foreach (DiagnosticRecord diagnostic in diagnosticsList)
321
+ // {
322
+ // logger.LogObject(diagnostic, this);
323
+ // }
324
+ // }
325
+ // }
294
326
295
327
#endregion
296
328
}
0 commit comments