Skip to content

Commit 60fe6c9

Browse files
author
Kapil Borle
committed
Move module handler initialization outside scriptanalyzer class
1 parent 750f8a4 commit 60fe6c9

File tree

2 files changed

+148
-120
lines changed

2 files changed

+148
-120
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Threading.Tasks;
2626
using System.Collections.Concurrent;
2727
using System.Threading;
28+
using System.Management.Automation.Runspaces;
2829

2930
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
3031
{
@@ -44,7 +45,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
4445
[Parameter(Position = 0,
4546
ParameterSetName = "File",
4647
Mandatory = true,
47-
ValueFromPipeline = true,
48+
ValueFromPipeline = true,
4849
ValueFromPipelineByPropertyName = true)]
4950
[ValidateNotNull]
5051
[Alias("PSPath")]
@@ -190,7 +191,7 @@ public object Settings
190191

191192
/// <summary>
192193
/// Resolve DSC resoure dependency
193-
/// </summary>
194+
/// </summary>
194195
[Parameter(Mandatory = false)]
195196
public SwitchParameter ResolveDscResourceDependency
196197
{
@@ -223,8 +224,7 @@ protected override void BeginProcessing()
223224
this.excludeRule,
224225
this.severity,
225226
null == rulePaths ? true : this.includeDefaultRules,
226-
this.suppressedOnly,
227-
resolveDscResourceDependency: resolveDscResourceDependency);
227+
this.suppressedOnly);
228228
}
229229

230230
/// <summary>
@@ -238,18 +238,50 @@ protected override void ProcessRecord()
238238
return;
239239
}
240240

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>();
241263
if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
242264
{
243-
// throws Item Not Found Exception
265+
// throws Item Not Found Exception
244266
Collection<PathInfo> paths = this.SessionState.Path.GetResolvedPSPathFromPSPath(path);
245267
foreach (PathInfo p in paths)
246268
{
247-
ProcessPathOrScriptDefinition(this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(p.Path));
269+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(
270+
this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(p.Path),
271+
this.recurse);
248272
}
249273
}
250274
else if (String.Equals(this.ParameterSetName, "ScriptDefinition", StringComparison.OrdinalIgnoreCase))
251275
{
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+
}
253285
}
254286
}
255287

@@ -269,28 +301,28 @@ protected override void StopProcessing()
269301

270302
#region Methods
271303

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>();
275307

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+
// }
284316

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+
//}
294326

295327
#endregion
296328
}

0 commit comments

Comments
 (0)