@@ -1290,6 +1290,39 @@ private void BuildScriptPathList(
1290
1290
}
1291
1291
}
1292
1292
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
+ }
1293
1326
1294
1327
private IEnumerable < DiagnosticRecord > AnalyzeFile ( string filePath )
1295
1328
{
@@ -1315,23 +1348,8 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1315
1348
return null ;
1316
1349
}
1317
1350
#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
-
1331
1351
//try parsing again
1332
- //var oldDefault = Runspace.DefaultRunspace;
1333
- //Runspace.DefaultRunspace = moduleHandler.Runspace;
1334
- if ( parseAgain )
1352
+ if ( TrySaveModules ( errors , scriptAst ) )
1335
1353
{
1336
1354
scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
1337
1355
}
0 commit comments