Skip to content

Commit a7a014c

Browse files
author
Kapil Borle
committed
Merge pull request #524 from PowerShell/FixParseError
Fix parse errors caused by missing dsc resouces
2 parents 64bda70 + 2458eae commit a7a014c

7 files changed

+835
-87
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

+39-16
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")]
@@ -188,6 +189,16 @@ public object Settings
188189

189190
private bool stopProcessing;
190191

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;
191202
#endregion Parameters
192203

193204
#region Overrides
@@ -227,18 +238,22 @@ protected override void ProcessRecord()
227238
return;
228239
}
229240

230-
if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
241+
// TODO Support dependency resolution for analyzing script definitions
242+
if (saveDscResourceDependency)
231243
{
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())
235245
{
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+
}
237252
}
238253
}
239-
else if (String.Equals(this.ParameterSetName, "ScriptDefinition", StringComparison.OrdinalIgnoreCase))
254+
else
240255
{
241-
ProcessPathOrScriptDefinition(scriptDefinition);
256+
ProcessInput();
242257
}
243258
}
244259

@@ -257,30 +272,38 @@ protected override void StopProcessing()
257272
#endregion
258273

259274
#region Methods
260-
261-
private void ProcessPathOrScriptDefinition(string pathOrScriptDefinition)
275+
private void ProcessInput()
262276
{
263277
IEnumerable<DiagnosticRecord> diagnosticsList = Enumerable.Empty<DiagnosticRecord>();
264-
265278
if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
266279
{
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+
}
268289
}
269290
else if (String.Equals(this.ParameterSetName, "ScriptDefinition", StringComparison.OrdinalIgnoreCase))
270291
{
271-
diagnosticsList = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(pathOrScriptDefinition);
292+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(scriptDefinition);
293+
WriteToOutput(diagnosticsList);
272294
}
295+
}
273296

274-
//Output through loggers
297+
private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
298+
{
275299
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
276300
{
277-
foreach (DiagnosticRecord diagnostic in diagnosticsList)
301+
foreach (DiagnosticRecord diagnostic in diagnosticRecords)
278302
{
279303
logger.LogObject(diagnostic, this);
280304
}
281305
}
282306
}
283-
284307
#endregion
285308
}
286309
}

0 commit comments

Comments
 (0)