|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.CSharp; |
| 10 | +using Semmle.Util; |
| 11 | +using Semmle.Util.Logging; |
| 12 | + |
| 13 | +namespace Semmle.Extraction.CSharp.StubGenerator; |
| 14 | + |
| 15 | +public static class StubGenerator |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Generates stubs for all the provided assembly paths. |
| 19 | + /// </summary> |
| 20 | + /// <param name="referencesPaths">The paths of the assemblies to generate stubs for.</param> |
| 21 | + /// <param name="outputPath">The path in which to store the stubs.</param> |
| 22 | + public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesPaths, string outputPath) |
| 23 | + { |
| 24 | + var stopWatch = new System.Diagnostics.Stopwatch(); |
| 25 | + stopWatch.Start(); |
| 26 | + |
| 27 | + var threads = EnvironmentVariables.GetDefaultNumberOfThreads(); |
| 28 | + |
| 29 | + using var references = new BlockingCollection<(MetadataReference Reference, string Path)>(); |
| 30 | + var referenceResolveTasks = GetResolvedReferenceTasks(referencesPaths, references); |
| 31 | + |
| 32 | + Parallel.Invoke( |
| 33 | + new ParallelOptions { MaxDegreeOfParallelism = threads }, |
| 34 | + referenceResolveTasks.ToArray()); |
| 35 | + |
| 36 | + logger.Log(Severity.Info, $"Generating stubs for {references.Count} assemblies."); |
| 37 | + |
| 38 | + var compilation = CSharpCompilation.Create( |
| 39 | + "stubgenerator.dll", |
| 40 | + null, |
| 41 | + references.Select(tuple => tuple.Item1), |
| 42 | + new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true)); |
| 43 | + |
| 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()); |
| 48 | + |
| 49 | + stopWatch.Stop(); |
| 50 | + logger.Log(Severity.Info, $"Stub generation took {stopWatch.Elapsed}."); |
| 51 | + } |
| 52 | + |
| 53 | + private static IEnumerable<Action> GetResolvedReferenceTasks(IEnumerable<string> referencePaths, BlockingCollection<(MetadataReference, string)> references) |
| 54 | + { |
| 55 | + return referencePaths.Select<string, Action>(path => () => |
| 56 | + { |
| 57 | + var reference = MetadataReference.CreateFromFile(path); |
| 58 | + references.Add((reference, path)); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 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)); |
| 69 | + |
| 70 | + writer.WriteLine("// This file contains auto-generated code."); |
| 71 | + writer.WriteLine($"// Generated from `{assembly.Identity}`."); |
| 72 | + |
| 73 | + var visitor = new StubVisitor(assembly, writer); |
| 74 | + |
| 75 | + visitor.StubAttributes(assembly.GetAttributes(), "assembly: "); |
| 76 | + |
| 77 | + foreach (var module in assembly.Modules) |
| 78 | + { |
| 79 | + module.GlobalNamespace.Accept(new StubVisitor(assembly, writer)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
0 commit comments