Skip to content

Commit ce31b43

Browse files
author
Kapil Borle
committed
Add a switch to resolve dsc module dependency
Adds a switch named "ResolveDSCResourceDependency" to Invoke-ScriptAnalyzer (isa). If the switch is given, then isa with try to resolve the dependency, whenever parse error is thrown due to modulenotfound exception, through the ModuleDependencyHandler class. If the switch is not provided, isa will not try to resolve the dependency.
1 parent 6e8097f commit ce31b43

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ public object Settings
188188

189189
private bool stopProcessing;
190190

191+
/// <summary>
192+
/// Resolve DSC resoure dependency
193+
/// </summary>
194+
[Parameter(Mandatory = false)]
195+
public SwitchParameter ResolveDSCResourceDependency
196+
{
197+
get { return resolveDSCResourceDependency; }
198+
set { resolveDSCResourceDependency = value; }
199+
}
200+
private bool resolveDSCResourceDependency;
191201
#endregion Parameters
192202

193203
#region Overrides
@@ -213,7 +223,8 @@ protected override void BeginProcessing()
213223
this.excludeRule,
214224
this.severity,
215225
null == rulePaths ? true : this.includeDefaultRules,
216-
this.suppressedOnly);
226+
this.suppressedOnly,
227+
resolveDSCResourceDependency: resolveDSCResourceDependency);
217228
}
218229

219230
/// <summary>

Engine/ScriptAnalyzer.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ internal void Initialize<TCmdlet>(
105105
string[] excludeRuleNames = null,
106106
string[] severity = null,
107107
bool includeDefaultRules = false,
108-
bool suppressedOnly = false)
108+
bool suppressedOnly = false,
109+
bool resolveDSCResourceDependency = false)
109110
where TCmdlet : PSCmdlet, IOutputWriter
110111
{
111112
if (cmdlet == null)
@@ -122,7 +123,8 @@ internal void Initialize<TCmdlet>(
122123
excludeRuleNames,
123124
severity,
124125
includeDefaultRules,
125-
suppressedOnly);
126+
suppressedOnly,
127+
resolveDSCResourceDependency: resolveDSCResourceDependency);
126128
}
127129

128130
/// <summary>
@@ -167,8 +169,11 @@ public void CleanUp()
167169
severity = null;
168170
includeRegexList = null;
169171
excludeRegexList = null;
170-
suppressedOnly = false;
171-
moduleHandler.Dispose();
172+
suppressedOnly = false;
173+
if (moduleHandler != null)
174+
{
175+
moduleHandler.Dispose();
176+
}
172177
}
173178

174179
internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWriter writer)
@@ -462,16 +467,17 @@ private bool ParseProfileString(string profile, PathIntrinsics path, IOutputWrit
462467
}
463468

464469
private void Initialize(
465-
IOutputWriter outputWriter,
466-
PathIntrinsics path,
467-
CommandInvocationIntrinsics invokeCommand,
468-
string[] customizedRulePath,
470+
IOutputWriter outputWriter,
471+
PathIntrinsics path,
472+
CommandInvocationIntrinsics invokeCommand,
473+
string[] customizedRulePath,
469474
string[] includeRuleNames,
470475
string[] excludeRuleNames,
471476
string[] severity,
472477
bool includeDefaultRules = false,
473478
bool suppressedOnly = false,
474-
string profile = null)
479+
string profile = null,
480+
bool resolveDSCResourceDependency = false)
475481
{
476482
if (outputWriter == null)
477483
{
@@ -481,8 +487,8 @@ private void Initialize(
481487
this.outputWriter = outputWriter;
482488

483489
// TODO Create a runspace pool
484-
runspace = RunspaceFactory.CreateRunspace();
485-
moduleHandler = new ModuleDependencyHandler(runspace);
490+
runspace = RunspaceFactory.CreateRunspace();
491+
moduleHandler = resolveDSCResourceDependency ? new ModuleDependencyHandler(runspace) : null;
486492

487493
#region Verifies rule extensions and loggers path
488494

@@ -1318,8 +1324,8 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
13181324
// - OR
13191325
// - swallow the these errors
13201326

1321-
1322-
if (errors != null && errors.Length > 0)
1327+
bool parseAgain = false;
1328+
if (moduleHandler != null && errors != null && errors.Length > 0)
13231329
{
13241330
foreach (ParseError error in errors)
13251331
{
@@ -1329,6 +1335,8 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
13291335
if (moduleName != null)
13301336
{
13311337
moduleHandler.SaveModule(moduleName);
1338+
// if successfully saved
1339+
parseAgain = true;
13321340
}
13331341
}
13341342
}
@@ -1337,9 +1345,12 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
13371345
//try parsing again
13381346
//var oldDefault = Runspace.DefaultRunspace;
13391347
//Runspace.DefaultRunspace = moduleHandler.Runspace;
1340-
scriptAst = Parser.ParseFile(filePath, out scriptTokens, out errors);
1341-
//Runspace.DefaultRunspace = oldDefault;
1348+
if (parseAgain)
1349+
{
1350+
scriptAst = Parser.ParseFile(filePath, out scriptTokens, out errors);
1351+
}
13421352

1353+
//Runspace.DefaultRunspace = oldDefault;
13431354
if (errors != null && errors.Length > 0)
13441355
{
13451356
foreach (ParseError error in errors)

0 commit comments

Comments
 (0)