@@ -27,11 +27,12 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
27
27
var threads = EnvironmentVariables . GetDefaultNumberOfThreads ( ) ;
28
28
29
29
using var references = new BlockingCollection < ( MetadataReference Reference , string Path ) > ( ) ;
30
- var referenceResolveTasks = GetResolvedReferenceTasks ( referencesPaths , references ) ;
31
30
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
+ } ) ;
35
36
36
37
logger . Log ( Severity . Info , $ "Generating stubs for { references . Count } assemblies.") ;
37
38
@@ -41,43 +42,33 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
41
42
references . Select ( tuple => tuple . Item1 ) ,
42
43
new CSharpCompilationOptions ( OutputKind . ConsoleApplication , allowUnsafe : true ) ) ;
43
44
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
+ } ) ;
48
49
49
50
stopWatch . Stop ( ) ;
50
51
logger . Log ( Severity . Info , $ "Stub generation took { stopWatch . Elapsed } .") ;
51
52
}
52
53
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 )
54
55
{
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 ;
61
58
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 ) ) ;
69
61
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 } `.") ;
72
64
73
- var visitor = new StubVisitor ( assembly , writer ) ;
65
+ var visitor = new StubVisitor ( assembly , writer ) ;
74
66
75
- visitor . StubAttributes ( assembly . GetAttributes ( ) , "assembly: " ) ;
67
+ visitor . StubAttributes ( assembly . GetAttributes ( ) , "assembly: " ) ;
76
68
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 ) ;
81
72
}
82
73
}
83
74
}
0 commit comments