public async Task <EmitResult> EmitAsync(OpenApiDocument document, YardarmGenerationSettings settings, CancellationToken cancellationToken = default) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var serviceProvider = settings.BuildServiceProvider(document); var compilation = CSharpCompilation.Create(settings.AssemblyName) .WithOptions(settings.CompilationOptions); var enrichers = serviceProvider.GetRequiredService <IEnumerable <ICompilationEnricher> >(); compilation = await compilation.EnrichAsync(enrichers, cancellationToken); var compilationResult = compilation.Emit(settings.DllOutput, pdbStream: settings.PdbOutput, xmlDocumentationStream: settings.XmlDocumentationOutput, options: new EmitOptions() .WithDebugInformationFormat(DebugInformationFormat.PortablePdb) .WithHighEntropyVirtualAddressSpace(true)); if (!compilationResult.Success) { return(compilationResult); } if (settings.NuGetOutput != null) { PackNuGet(serviceProvider, settings); } return(compilationResult); }
public async Task <YardarmGenerationResult> EmitAsync(OpenApiDocument document, YardarmGenerationSettings settings, CancellationToken cancellationToken = default) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var toDispose = new List <IDisposable>(); try { var serviceProvider = settings.BuildServiceProvider(document); var context = serviceProvider.GetRequiredService <GenerationContext>(); // Perform the NuGet restore var restoreProcessor = serviceProvider.GetRequiredService <NuGetRestoreProcessor>(); context.NuGetRestoreInfo = await restoreProcessor.ExecuteAsync(cancellationToken).ConfigureAwait(false); if (settings.TargetFrameworkMonikers.Length == 0) { throw new InvalidOperationException("No target framework monikers provided."); } var compilationResults = new List <YardarmCompilationResult>(); if (settings.TargetFrameworkMonikers.Length == 1) { var targetFramework = NuGetFramework.Parse(settings.TargetFrameworkMonikers[0]); // Perform the compilation var(emitResult, additionalDiagnostics) = await BuildForTargetFrameworkAsync( context, settings, targetFramework, settings.DllOutput, settings.PdbOutput, settings.XmlDocumentationOutput, cancellationToken).ConfigureAwait(false); compilationResults.Add(new(targetFramework, emitResult, settings.DllOutput, settings.PdbOutput, settings.XmlDocumentationOutput, additionalDiagnostics)); } else { if (settings.NuGetOutput is null) { throw new InvalidOperationException( "Targeting multiple frameworks is only supported with NuGet output."); } foreach (var targetFramework in settings.TargetFrameworkMonikers.Select(NuGetFramework.Parse)) { var dllOutput = new MemoryStream(); toDispose.Add(dllOutput); var pdbOutput = new MemoryStream(); toDispose.Add(pdbOutput); var xmlDocumentationOutput = new MemoryStream(); toDispose.Add(xmlDocumentationOutput); // Perform the compilation var(emitResult, additionalDiagnostics) = await BuildForTargetFrameworkAsync( context, settings, targetFramework, dllOutput, pdbOutput, xmlDocumentationOutput, cancellationToken).ConfigureAwait(false); compilationResults.Add(new(targetFramework, emitResult, dllOutput, pdbOutput, xmlDocumentationOutput, additionalDiagnostics)); } } if (compilationResults.All(p => p.EmitResult.Success)) { if (settings.NuGetOutput != null) { PackNuGet(serviceProvider, settings, compilationResults); } } return(new YardarmGenerationResult(serviceProvider.GetRequiredService <GenerationContext>(), compilationResults)); } finally { foreach (var disposable in toDispose) { disposable.Dispose(); } } }