예제 #1
0
        public async Task <int> ExecuteAsync()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var document = await ReadDocumentAsync();

            var generator = new YardarmGenerator();

            var settings = new YardarmGenerationSettings(_options.AssemblyName);

            ApplyVersion(settings);

            ApplyExtensions(settings);

            ApplyStrongNaming(settings);

            ApplyNuGetSettings(settings);

            List <Stream> streams = ApplyFileStreams(settings);

            try
            {
                settings
                .AddLogging(builder =>
                {
                    builder
                    .SetMinimumLevel(LogLevel.Information)
                    .AddSerilog();
                });

                YardarmGenerationResult generationResult = await generator.EmitAsync(document, settings);

                foreach (Diagnostic diagnostic in generationResult.GetAllDiagnostics()
                         .Where(p => p.Severity >= DiagnosticSeverity.Info))
                {
                    Log.Logger.Write(
                        diagnostic.Severity switch
                    {
                        DiagnosticSeverity.Error => LogEventLevel.Error,
                        DiagnosticSeverity.Warning => LogEventLevel.Warning,
                        _ => LogEventLevel.Information
                    },
                        diagnostic.GetMessageWithSource(
                            generationResult.Context.GenerationServices.GetRequiredService <IOpenApiElementRegistry>())
                        );
                }
예제 #2
0
        public async Task <int> ExecuteAsync()
        {
            var document = await ReadDocumentAsync();

            var generator = new YardarmGenerator();

            var settings = new YardarmGenerationSettings(_options.AssemblyName);

            ApplyVersion(settings);

            ApplyExtensions(settings);

            ApplyStrongNaming(settings);

            List <Stream> streams = ApplyFileStreams(settings);

            try
            {
                settings
                .AddLogging(builder =>
                {
                    builder
                    .SetMinimumLevel(LogLevel.Information)
                    .AddConsole();
                });

                EmitResult compilationResult = await generator.EmitAsync(document, settings);

                await using var stdError = new StreamWriter(Console.OpenStandardError(), new UTF8Encoding(false));
                foreach (Diagnostic diagnostic in compilationResult.Diagnostics.Where(p =>
                                                                                      p.Severity == DiagnosticSeverity.Error))
                {
                    stdError.WriteLine(diagnostic);
                }

                return(compilationResult.Success ? 0 : 1);
            }
            finally
            {
                foreach (var stream in streams)
                {
                    await stream.DisposeAsync();
                }
            }
        }