From fafc11e36a800e934aeae26422711ee626938c19 Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Tue, 17 Dec 2019 10:49:02 -0800 Subject: [PATCH] Add command line parsing and handling for output and log files --- eng/Versions.props | 4 +- src/Features/Lsif/Generator/.editorconfig | 5 ++ ...crosoft.CodeAnalysis.Lsif.Generator.csproj | 7 +++ src/Features/Lsif/Generator/Program.cs | 47 ++++++++++++++++++- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/Features/Lsif/Generator/.editorconfig diff --git a/eng/Versions.props b/eng/Versions.props index 2fe0eb0c0c8..6fa546d6179 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -41,7 +41,6 @@ 0.9.3 0.11.4 - 2.0.273-beta 1.4.4 17.144.28413-buildid7983345 8.0.2 @@ -179,8 +178,7 @@ 0.0.4 1.0.21 4.5.0 - 0.1.0-alpha-63729-01 - 0.1.0-alpha-63729-01 + 0.3.0-alpha.19577.1 4.5.0 4.5.0 4.3.0 diff --git a/src/Features/Lsif/Generator/.editorconfig b/src/Features/Lsif/Generator/.editorconfig new file mode 100644 index 00000000000..b79fad3bd5c --- /dev/null +++ b/src/Features/Lsif/Generator/.editorconfig @@ -0,0 +1,5 @@ +# This .editorconfig customizes some diagnostic rules that are specific to this project. + +[*.cs] +dotnet_diagnostic.CA2007.severity = none # Disable warnings about ConfigureAwait, since this is a console app +dotnet_diagnostic.VSTHRD111.severity = none # Disable warnings about ConfigureAwait, since this is a console app \ No newline at end of file diff --git a/src/Features/Lsif/Generator/Microsoft.CodeAnalysis.Lsif.Generator.csproj b/src/Features/Lsif/Generator/Microsoft.CodeAnalysis.Lsif.Generator.csproj index 0c6c2747215..291d121a5d9 100644 --- a/src/Features/Lsif/Generator/Microsoft.CodeAnalysis.Lsif.Generator.csproj +++ b/src/Features/Lsif/Generator/Microsoft.CodeAnalysis.Lsif.Generator.csproj @@ -12,4 +12,11 @@ $(RoslynPortableRuntimeIdentifiers) enable + + + + + + + \ No newline at end of file diff --git a/src/Features/Lsif/Generator/Program.cs b/src/Features/Lsif/Generator/Program.cs index 060ef91bd31..fbdcac40d4e 100644 --- a/src/Features/Lsif/Generator/Program.cs +++ b/src/Features/Lsif/Generator/Program.cs @@ -1,11 +1,56 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using System.CommandLine; +using System.CommandLine.Builder; +using System.CommandLine.Invocation; +using System.IO; +using System.Threading.Tasks; + namespace Microsoft.CodeAnalysis.Lsif.Generator { internal static class Program { - public static void Main(string[] args) + public static Task Main(string[] args) + { + var generateCommand = new RootCommand("generates an LSIF file") + { + new Option("--output", "file to write the LSIF output to, instead of the console") { Argument = new Argument(defaultValue: () => null).LegalFilePathsOnly() }, + new Option("--log", "file to write a log to") { Argument = new Argument(defaultValue: () => null).LegalFilePathsOnly() } + }; + + generateCommand.Handler = CommandHandler.Create((Func)GenerateAsync); + + return generateCommand.InvokeAsync(args); + } + + private static async Task GenerateAsync(string? output, string? log) + { + // If we have an output file, we'll write to that, else we'll use Console.Out + using StreamWriter? outputFile = output != null ? new StreamWriter(output) : null; + TextWriter outputWriter = outputFile ?? Console.Out; + + using TextWriter logFile = log != null ? new StreamWriter(log) : TextWriter.Null; + + try + { + await GenerateAsync(outputWriter, logFile); + } + catch (Exception e) + { + // If it failed, write out to the logs and error, but propagate the error too + var message = "Unhandled exception: " + e.ToString(); + await logFile.WriteLineAsync(message); + Console.Error.WriteLine(message); + throw; + } + + await logFile.WriteLineAsync("Generation complete."); + } + + private static Task GenerateAsync(TextWriter outputWriter, TextWriter logFile) { + return Task.CompletedTask; } } } -- GitLab