Skip to content

Commit f02d281

Browse files
authored
Merge pull request github#14309 from hvitved/csharp/stub-generator-output
C#: Make `GenerateStubs` return list of generated output
2 parents c9976cf + 4bfd677 commit f02d281

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubGenerator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ public static class StubGenerator
2020
/// </summary>
2121
/// <param name="referencesPaths">The paths of the assemblies to generate stubs for.</param>
2222
/// <param name="outputPath">The path in which to store the stubs.</param>
23-
public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesPaths, string outputPath)
23+
public static string[] GenerateStubs(ILogger logger, IEnumerable<string> referencesPaths, string outputPath)
2424
{
2525
var stopWatch = new System.Diagnostics.Stopwatch();
2626
stopWatch.Start();
2727

2828
var threads = EnvironmentVariables.GetDefaultNumberOfThreads();
2929

30+
using var stubPaths = new BlockingCollection<string>();
3031
using var references = new BlockingCollection<(MetadataReference Reference, string Path)>();
3132

3233
Parallel.ForEach(referencesPaths, new ParallelOptions { MaxDegreeOfParallelism = threads }, path =>
@@ -45,14 +46,16 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
4546

4647
Parallel.ForEach(references, new ParallelOptions { MaxDegreeOfParallelism = threads }, @ref =>
4748
{
48-
StubReference(logger, compilation, outputPath, @ref.Reference, @ref.Path);
49+
StubReference(logger, compilation, outputPath, @ref.Reference, @ref.Path, stubPaths);
4950
});
5051

5152
stopWatch.Stop();
5253
logger.Log(Severity.Info, $"Stub generation took {stopWatch.Elapsed}.");
54+
55+
return stubPaths.ToArray();
5356
}
5457

55-
private static void StubReference(ILogger logger, CSharpCompilation compilation, string outputPath, MetadataReference reference, string path)
58+
private static void StubReference(ILogger logger, CSharpCompilation compilation, string outputPath, MetadataReference reference, string path, BlockingCollection<string> stubPaths)
5659
{
5760
if (compilation.GetAssemblyOrModuleSymbol(reference) is not IAssemblySymbol assembly)
5861
return;
@@ -62,7 +65,9 @@ private static void StubReference(ILogger logger, CSharpCompilation compilation,
6265
if (!assembly.Modules.Any(m => relevantSymbol.IsRelevantNamespace(m.GlobalNamespace)))
6366
return;
6467

65-
using var fileStream = new FileStream(FileUtils.NestPaths(logger, outputPath, path.Replace(".dll", ".cs")), FileMode.Create, FileAccess.Write);
68+
var stubPath = FileUtils.NestPaths(logger, outputPath, path.Replace(".dll", ".cs"));
69+
stubPaths.Add(stubPath);
70+
using var fileStream = new FileStream(stubPath, FileMode.Create, FileAccess.Write);
6671
using var writer = new StreamWriter(fileStream, new UTF8Encoding(false));
6772

6873
var visitor = new StubVisitor(writer, relevantSymbol);

0 commit comments

Comments
 (0)