Skip to content

Commit 4466548

Browse files
author
Kapil Borle
committed
Fix module saving for SaveDscDependency switch
1 parent 1342d9f commit 4466548

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,39 @@ private void BuildScriptPathList(
12901290
}
12911291
}
12921292

1293+
private bool TrySaveModules(ParseError[] errors, ScriptBlockAst scriptAst)
1294+
{
1295+
bool modulesSaved = false;
1296+
if (moduleHandler == null || errors == null || errors.Length == 0)
1297+
{
1298+
return modulesSaved;
1299+
}
1300+
foreach (var error in errors.Where(IsModuleNotFoundError))
1301+
{
1302+
var moduleNames = moduleHandler.GetUnavailableModuleNameFromErrorExtent(error, scriptAst);
1303+
if (moduleNames == null)
1304+
{
1305+
continue;
1306+
}
1307+
foreach(var moduleName in moduleNames)
1308+
{
1309+
this.outputWriter.WriteVerbose(
1310+
String.Format(
1311+
"Saving module {0} from PSGallery",
1312+
moduleName));
1313+
var moduleSaved = moduleHandler.TrySaveModule(moduleName);
1314+
if (!moduleSaved)
1315+
{
1316+
this.outputWriter.WriteVerbose(
1317+
String.Format(
1318+
"Cannot download {0} from PSGallery",
1319+
moduleName));
1320+
}
1321+
modulesSaved |= moduleSaved;
1322+
}
1323+
}
1324+
return modulesSaved;
1325+
}
12931326

12941327
private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
12951328
{
@@ -1315,23 +1348,8 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
13151348
return null;
13161349
}
13171350
#if !PSV3
1318-
bool parseAgain = false;
1319-
if (moduleHandler != null && errors != null && errors.Length > 0)
1320-
{
1321-
foreach (ParseError error in errors.Where(IsModuleNotFoundError))
1322-
{
1323-
var moduleNames = moduleHandler.GetUnavailableModuleNameFromErrorExtent(error, scriptAst);
1324-
if (moduleNames != null)
1325-
{
1326-
parseAgain |= moduleNames.Any(x => moduleHandler.TrySaveModule(x));
1327-
}
1328-
}
1329-
}
1330-
13311351
//try parsing again
1332-
//var oldDefault = Runspace.DefaultRunspace;
1333-
//Runspace.DefaultRunspace = moduleHandler.Runspace;
1334-
if (parseAgain)
1352+
if (TrySaveModules(errors, scriptAst))
13351353
{
13361354
scriptAst = Parser.ParseFile(filePath, out scriptTokens, out errors);
13371355
}

0 commit comments

Comments
 (0)