diff --git a/NuGet.config b/NuGet.config index ed5441c3..1873d27f 100644 --- a/NuGet.config +++ b/NuGet.config @@ -7,6 +7,8 @@ + + diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index d38fdd59..607014c4 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -1,21 +1,21 @@ - + https://github.com/dotnet/command-line-api - 209b724a3c843253d3071e8348c353b297b0b8b5 + 02fe27cd6a9b001c8feb7938e6ef4b3799745759 - + https://github.com/dotnet/command-line-api - 209b724a3c843253d3071e8348c353b297b0b8b5 + 02fe27cd6a9b001c8feb7938e6ef4b3799745759 - + https://github.com/dotnet/command-line-api - 209b724a3c843253d3071e8348c353b297b0b8b5 + 02fe27cd6a9b001c8feb7938e6ef4b3799745759 - + https://github.com/dotnet/command-line-api - 209b724a3c843253d3071e8348c353b297b0b8b5 + 02fe27cd6a9b001c8feb7938e6ef4b3799745759 diff --git a/eng/Versions.props b/eng/Versions.props index 994e10ad..65ed6eed 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -10,9 +10,9 @@ - 2.0.0-beta4.22272.1 - 2.0.0-beta4.22272.1 - 0.4.0-alpha.22272.1 + 2.0.0-beta4.23307.1 + 2.0.0-beta4.23307.1 + 0.4.0-alpha.23307.1 17.3.2 17.3.2 diff --git a/src/dotnet-sourcelink/Program.cs b/src/dotnet-sourcelink/Program.cs index 08037344..eba8e56d 100644 --- a/src/dotnet-sourcelink/Program.cs +++ b/src/dotnet-sourcelink/Program.cs @@ -42,18 +42,18 @@ private record DocumentInfo( ImmutableArray Hash, Guid HashAlgorithm); - private readonly IConsole _console; + private readonly ParseResult _parseResult; private bool _errorReported; - public Program(IConsole console) + public Program(ParseResult parseResult) { - _console = console; + _parseResult = parseResult; } public static async Task Main(string[] args) { var rootCommand = GetRootCommand(); - return await rootCommand.InvokeAsync(args); + return await rootCommand.Parse(args).InvokeAsync(); } private static string GetSourceLinkVersion() @@ -62,41 +62,77 @@ private static string GetSourceLinkVersion() return attribute.InformationalVersion.Split('+').First(); } - private static RootCommand GetRootCommand() + private static CliRootCommand GetRootCommand() { - var authArg = new Option(new[] { "--auth", "-a" }, "Authentication method").FromAmong(AuthenticationMethod.Basic); - var userArg = new Option(new[] { "--user", "-u" }, "Username to use to authenticate") { Arity = ArgumentArity.ExactlyOne }; - var passwordArg = new Option(new[] { "--password", "-p" }, "Password to use to authenticate") { Arity = ArgumentArity.ExactlyOne }; + var authArg = new CliOption("--auth", "-a") + { + Description = "Authentication method" + }; + authArg.AcceptOnlyFromAmong(AuthenticationMethod.Basic); + + var userArg = new CliOption("--user", "-u") + { + Description = "Username to use to authenticate", + Arity = ArgumentArity.ExactlyOne + }; + + var passwordArg = new CliOption("--password", "-p") + { + Description = "Password to use to authenticate", + Arity = ArgumentArity.ExactlyOne + }; - var test = new Command("test", "TODO") + var offlineArg = new CliOption("--offline") { - new Argument("path", "Path to an assembly or .pdb"), + Description = "Offline mode - skip validation of sourcelink URL targets" + }; + + var test = new CliCommand("test", "TODO") + { + new CliArgument("path") + { + Description = "Path to an assembly or .pdb" + }, authArg, - new Option(new[] { "--auth-encoding", "-e" }, (arg) => Encoding.GetEncoding(arg.Tokens.Single().Value), false, "Encoding to use for authentication value"), + new CliOption("--auth-encoding", "-e") + { + CustomParser = arg => Encoding.GetEncoding(arg.Tokens.Single().Value), + Description = "Encoding to use for authentication value" + }, userArg, passwordArg, + offlineArg, }; - test.Handler = CommandHandler.Create(TestAsync); + test.Action = CommandHandler.Create(TestAsync); - var printJson = new Command("print-json", "Print Source Link JSON stored in the PDB") + var printJson = new CliCommand("print-json", "Print Source Link JSON stored in the PDB") { - new Argument("path", "Path to an assembly or .pdb"), + new CliArgument("path") + { + Description = "Path to an assembly or .pdb" + } }; - printJson.Handler = CommandHandler.Create(PrintJsonAsync); + printJson.Action = CommandHandler.Create(PrintJsonAsync); - var printDocuments = new Command("print-documents", "TODO") + var printDocuments = new CliCommand("print-documents", "TODO") { - new Argument("path", "Path to an assembly or .pdb"), + new CliArgument("path") + { + Description = "Path to an assembly or .pdb" + } }; - printDocuments.Handler = CommandHandler.Create(PrintDocumentsAsync); + printDocuments.Action = CommandHandler.Create(PrintDocumentsAsync); - var printUrls = new Command("print-urls", "TODO") + var printUrls = new CliCommand("print-urls", "TODO") { - new Argument("path", "Path to an assembly or .pdb"), + new CliArgument("path") + { + Description = "Path to an assembly or .pdb" + } }; - printUrls.Handler = CommandHandler.Create(PrintUrlsAsync); + printUrls.Action = CommandHandler.Create(PrintUrlsAsync); - var root = new RootCommand() + var root = new CliRootCommand() { test, printJson, @@ -106,13 +142,13 @@ private static RootCommand GetRootCommand() root.Description = "dotnet-sourcelink"; - root.AddValidator(commandResult => + root.Validators.Add(commandResult => { - if (commandResult.FindResultFor(authArg) != null) + if (commandResult.GetResult(authArg) != null) { - if (commandResult.FindResultFor(userArg) == null || commandResult.FindResultFor(passwordArg) == null) + if (commandResult.GetResult(userArg) == null || commandResult.GetResult(passwordArg) == null) { - commandResult.ErrorMessage = "Specify --user and --password options"; + commandResult.AddError("Specify --user and --password options"); } } }); @@ -122,15 +158,15 @@ private static RootCommand GetRootCommand() private void ReportError(string message) { - _console.Error.Write(message); - _console.Error.Write(Environment.NewLine); + _parseResult.Configuration.Error.Write(message); + _parseResult.Configuration.Error.Write(Environment.NewLine); _errorReported = true; } private void WriteOutputLine(string message) { - _console.Out.Write(message); - _console.Out.Write(Environment.NewLine); + _parseResult.Configuration.Output.Write(message); + _parseResult.Configuration.Output.Write(Environment.NewLine); } private static async Task TestAsync( @@ -139,7 +175,8 @@ private static async Task TestAsync( Encoding? authEncoding, string? user, string? password, - IConsole console) + bool offline, + ParseResult parseResult) { var authenticationHeader = (authMethod != null) ? GetAuthenticationHeader(authMethod, authEncoding ?? Encoding.ASCII, user!, password!) : null; @@ -152,17 +189,17 @@ private static async Task TestAsync( try { - return await new Program(console).TestAsync(path, authenticationHeader, cancellationSource.Token).ConfigureAwait(false); + return await new Program(parseResult).TestAsync(path, authenticationHeader, offline, cancellationSource.Token).ConfigureAwait(false); } catch (OperationCanceledException) { - console.Error.Write("Operation canceled."); - console.Error.Write(Environment.NewLine); + parseResult.Configuration.Error.Write("Operation canceled."); + parseResult.Configuration.Error.Write(Environment.NewLine); return -1; } } - private async Task TestAsync(string path, AuthenticationHeaderValue? authenticationHeader, CancellationToken cancellationToken) + private async Task TestAsync(string path, AuthenticationHeaderValue? authenticationHeader, bool offline, CancellationToken cancellationToken) { var documents = new List(); ReadAndResolveDocuments(path, documents); @@ -172,31 +209,34 @@ private async Task TestAsync(string path, AuthenticationHeaderValue? authen return _errorReported ? 1 : 0; } - var handler = new HttpClientHandler(); - if (handler.SupportsAutomaticDecompression) - handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + if (!offline) + { + var handler = new HttpClientHandler(); + if (handler.SupportsAutomaticDecompression) + handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; - using var client = new HttpClient(handler); - client.DefaultRequestHeaders.UserAgent.Add(s_sourceLinkProductHeaderValue); - client.DefaultRequestHeaders.Authorization = authenticationHeader; + using var client = new HttpClient(handler); + client.DefaultRequestHeaders.UserAgent.Add(s_sourceLinkProductHeaderValue); + client.DefaultRequestHeaders.Authorization = authenticationHeader; - var outputLock = new object(); + var outputLock = new object(); - var errorReporter = new Action(message => - { - lock (outputLock) + var errorReporter = new Action(message => { - ReportError(message); - } - }); + lock (outputLock) + { + ReportError(message); + } + }); - var tasks = documents.Where(document => document.Uri != null).Select(document => DownloadAndValidateDocumentAsync(client, document, errorReporter, cancellationToken)); - - _ = await Task.WhenAll(tasks).ConfigureAwait(false); + var tasks = documents.Where(document => document.Uri != null).Select(document => DownloadAndValidateDocumentAsync(client, document, errorReporter, cancellationToken)); - if (_errorReported) - { - return 1; + _ = await Task.WhenAll(tasks).ConfigureAwait(false); + + if (_errorReported) + { + return 1; + } } WriteOutputLine($"File '{path}' validated."); @@ -277,8 +317,8 @@ private static async Task DownloadAndValidateDocumentAsync(HttpClient clie } } - private static Task PrintJsonAsync(string path, IConsole console) - => Task.FromResult(new Program(console).PrintJson(path)); + private static Task PrintJsonAsync(string path, ParseResult parseResult) + => Task.FromResult(new Program(parseResult).PrintJson(path)); private int PrintJson(string path) { @@ -299,8 +339,8 @@ private int PrintJson(string path) return _errorReported ? 1 : 0; } - private static Task PrintDocumentsAsync(string path, IConsole console) - => Task.FromResult(new Program(console).PrintDocuments(path)); + private static Task PrintDocumentsAsync(string path, ParseResult parseResult) + => Task.FromResult(new Program(parseResult).PrintDocuments(path)); public static string ToHex(byte[] bytes) => BitConverter.ToString(bytes).Replace("-", "").ToLowerInvariant(); @@ -324,8 +364,8 @@ private int PrintDocuments(string path) return _errorReported ? 1 : 0; } - private static Task PrintUrlsAsync(string path,IConsole console) - => Task.FromResult(new Program(console).PrintUrls(path)); + private static Task PrintUrlsAsync(string path, ParseResult parseResult) + => Task.FromResult(new Program(parseResult).PrintUrls(path)); private int PrintUrls(string path) { diff --git a/src/dotnet-sourcelink/dotnet-sourcelink.csproj b/src/dotnet-sourcelink/dotnet-sourcelink.csproj index 6e0bd5f0..ac179f1f 100644 --- a/src/dotnet-sourcelink/dotnet-sourcelink.csproj +++ b/src/dotnet-sourcelink/dotnet-sourcelink.csproj @@ -4,8 +4,6 @@ $(NetCurrent) Major - - true true @@ -13,7 +11,7 @@ sourcelink Command line tool for SourceLink testing. true - win-x64;win-x86;osx-x64 + win-x64;win-x86;osx-x64