@@ -27,11 +27,12 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
2727 var threads = EnvironmentVariables . GetDefaultNumberOfThreads ( ) ;
2828
2929 using var references = new BlockingCollection < ( MetadataReference Reference , string Path ) > ( ) ;
30- var referenceResolveTasks = GetResolvedReferenceTasks ( referencesPaths , references ) ;
3130
32- Parallel . Invoke (
33- new ParallelOptions { MaxDegreeOfParallelism = threads } ,
34- referenceResolveTasks . ToArray ( ) ) ;
31+ Parallel . ForEach ( referencesPaths , new ParallelOptions { MaxDegreeOfParallelism = threads } , path =>
32+ {
33+ var reference = MetadataReference . CreateFromFile ( path ) ;
34+ references . Add ( ( reference , path ) ) ;
35+ } ) ;
3536
3637 logger . Log ( Severity . Info , $ "Generating stubs for { references . Count } assemblies.") ;
3738
@@ -41,43 +42,33 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
4142 references . Select ( tuple => tuple . Item1 ) ,
4243 new CSharpCompilationOptions ( OutputKind . ConsoleApplication , allowUnsafe : true ) ) ;
4344
44- var referenceStubTasks = references . Select ( @ref => ( Action ) ( ( ) => StubReference ( compilation , outputPath , @ref . Reference , @ref . Path ) ) ) ;
45- Parallel . Invoke (
46- new ParallelOptions { MaxDegreeOfParallelism = threads } ,
47- referenceStubTasks . ToArray ( ) ) ;
45+ Parallel . ForEach ( references , new ParallelOptions { MaxDegreeOfParallelism = threads } , @ref =>
46+ {
47+ StubReference ( logger , compilation , outputPath , @ref . Reference , @ref . Path ) ;
48+ } ) ;
4849
4950 stopWatch . Stop ( ) ;
5051 logger . Log ( Severity . Info , $ "Stub generation took { stopWatch . Elapsed } .") ;
5152 }
5253
53- private static IEnumerable < Action > GetResolvedReferenceTasks ( IEnumerable < string > referencePaths , BlockingCollection < ( MetadataReference , string ) > references )
54+ private static void StubReference ( ILogger logger , CSharpCompilation compilation , string outputPath , MetadataReference reference , string path )
5455 {
55- return referencePaths . Select < string , Action > ( path => ( ) =>
56- {
57- var reference = MetadataReference . CreateFromFile ( path ) ;
58- references . Add ( ( reference , path ) ) ;
59- } ) ;
60- }
56+ if ( compilation . GetAssemblyOrModuleSymbol ( reference ) is not IAssemblySymbol assembly )
57+ return ;
6158
62- private static void StubReference ( CSharpCompilation compilation , string outputPath , MetadataReference reference , string path )
63- {
64- if ( compilation . GetAssemblyOrModuleSymbol ( reference ) is IAssemblySymbol assembly )
65- {
66- var logger = new ConsoleLogger ( Verbosity . Info ) ;
67- using var fileStream = new FileStream ( FileUtils . NestPaths ( logger , outputPath , path . Replace ( ".dll" , ".cs" ) ) , FileMode . Create , FileAccess . Write ) ;
68- using var writer = new StreamWriter ( fileStream , new UTF8Encoding ( false ) ) ;
59+ using var fileStream = new FileStream ( FileUtils . NestPaths ( logger , outputPath , path . Replace ( ".dll" , ".cs" ) ) , FileMode . Create , FileAccess . Write ) ;
60+ using var writer = new StreamWriter ( fileStream , new UTF8Encoding ( false ) ) ;
6961
70- writer . WriteLine ( "// This file contains auto-generated code." ) ;
71- writer . WriteLine ( $ "// Generated from `{ assembly . Identity } `.") ;
62+ writer . WriteLine ( "// This file contains auto-generated code." ) ;
63+ writer . WriteLine ( $ "// Generated from `{ assembly . Identity } `.") ;
7264
73- var visitor = new StubVisitor ( assembly , writer ) ;
65+ var visitor = new StubVisitor ( assembly , writer ) ;
7466
75- visitor . StubAttributes ( assembly . GetAttributes ( ) , "assembly: " ) ;
67+ visitor . StubAttributes ( assembly . GetAttributes ( ) , "assembly: " ) ;
7668
77- foreach ( var module in assembly . Modules )
78- {
79- module . GlobalNamespace . Accept ( new StubVisitor ( assembly , writer ) ) ;
80- }
69+ foreach ( var module in assembly . Modules )
70+ {
71+ module . GlobalNamespace . Accept ( visitor ) ;
8172 }
8273 }
8374}
0 commit comments