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" ) ]
@@ -188,6 +189,16 @@ public object Settings
188
189
189
190
private bool stopProcessing ;
190
191
192
+ /// <summary>
193
+ /// Resolve DSC resource dependency
194
+ /// </summary>
195
+ [ Parameter ( Mandatory = false ) ]
196
+ public SwitchParameter SaveDscResourceDependency
197
+ {
198
+ get { return saveDscResourceDependency ; }
199
+ set { saveDscResourceDependency = value ; }
200
+ }
201
+ private bool saveDscResourceDependency ;
191
202
#endregion Parameters
192
203
193
204
#region Overrides
@@ -227,18 +238,22 @@ protected override void ProcessRecord()
227
238
return ;
228
239
}
229
240
230
- if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
241
+ // TODO Support dependency resolution for analyzing script definitions
242
+ if ( saveDscResourceDependency )
231
243
{
232
- // throws Item Not Found Exception
233
- Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
234
- foreach ( PathInfo p in paths )
244
+ using ( var rsp = RunspaceFactory . CreateRunspace ( ) )
235
245
{
236
- ProcessPathOrScriptDefinition ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
246
+ rsp . Open ( ) ;
247
+ using ( var moduleHandler = new ModuleDependencyHandler ( rsp ) )
248
+ {
249
+ ScriptAnalyzer . Instance . ModuleHandler = moduleHandler ;
250
+ ProcessInput ( ) ;
251
+ }
237
252
}
238
253
}
239
- else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
254
+ else
240
255
{
241
- ProcessPathOrScriptDefinition ( scriptDefinition ) ;
256
+ ProcessInput ( ) ;
242
257
}
243
258
}
244
259
@@ -257,30 +272,38 @@ protected override void StopProcessing()
257
272
#endregion
258
273
259
274
#region Methods
260
-
261
- private void ProcessPathOrScriptDefinition ( string pathOrScriptDefinition )
275
+ private void ProcessInput ( )
262
276
{
263
277
IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
264
-
265
278
if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
266
279
{
267
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath ( pathOrScriptDefinition , this . recurse ) ;
280
+ // throws Item Not Found Exception
281
+ Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
282
+ foreach ( PathInfo p in paths )
283
+ {
284
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath (
285
+ this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ,
286
+ this . recurse ) ;
287
+ WriteToOutput ( diagnosticsList ) ;
288
+ }
268
289
}
269
290
else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
270
291
{
271
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( pathOrScriptDefinition ) ;
292
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( scriptDefinition ) ;
293
+ WriteToOutput ( diagnosticsList ) ;
272
294
}
295
+ }
273
296
274
- //Output through loggers
297
+ private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
298
+ {
275
299
foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
276
300
{
277
- foreach ( DiagnosticRecord diagnostic in diagnosticsList )
301
+ foreach ( DiagnosticRecord diagnostic in diagnosticRecords )
278
302
{
279
303
logger . LogObject ( diagnostic , this ) ;
280
304
}
281
305
}
282
306
}
283
-
284
307
#endregion
285
308
}
286
309
}
0 commit comments