Repository navigation
Print canonical docs shortlinks in --help, api show, and plugin errors #2098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
waldekmastykarz
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
waldekmastykarz:print-canonical-docs-shortlinks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d49bb51
Print canonical docs shortlinks in --help, api show, and plugin errors
waldekmastykarz b53d974
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 03bb250
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 75312ef
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 1a0bf4b
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 4e0e78a
Show docs link when built-in plugin assembly fails to load
waldekmastykarz 338d45f
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 0de596c
Merge branch 'main' into print-canonical-docs-shortlinks
waldekmastykarz 88aab9b
Print docs footer in standalone plugin command help
waldekmastykarz 270c37b
Rename xUnit collection definition to satisfy CA1711
waldekmastykarz 1a882ac
Merge main into print-canonical-docs-shortlinks
waldekmastykarz 5990ff7
Skip docs link for blank plugin names in load error handlers
waldekmastykarz 98bc0b9
Merge main into print-canonical-docs-shortlinks
waldekmastykarz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Reflection; | ||
|
|
||
| namespace DevProxy.Abstractions.Utils; | ||
|
|
||
| /// <summary> | ||
| /// Builds stable documentation shortlinks (https://go.devproxy.net?go=<code>). | ||
| /// The shortlink service redirects codes to the actual documentation pages, | ||
| /// so the CLI never hard-codes documentation paths. | ||
| /// </summary> | ||
| public static class DocsLinks | ||
| { | ||
| private const string BaseUrl = "https://go.devproxy.net?go="; | ||
| private const string BuiltInPluginsAssemblyName = "DevProxy.Plugins"; | ||
|
|
||
| public static string Api { get; } = Get("api"); | ||
| public static string Cli { get; } = Get("cli"); | ||
|
|
||
| public static string Get(string code) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(code); | ||
| return BaseUrl + Uri.EscapeDataString(code.ToLowerInvariant()); | ||
| } | ||
|
|
||
| public static bool IsBuiltInPluginAssembly(Assembly assembly) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(assembly); | ||
| return string.Equals(assembly.GetName().Name, BuiltInPluginsAssemblyName, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the documentation link for a plugin, or null if the plugin | ||
| /// isn't shipped with Dev Proxy and has no canonical documentation page. | ||
| /// </summary> | ||
| public static string? ForPlugin(Type pluginType) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(pluginType); | ||
| return IsBuiltInPluginAssembly(pluginType.Assembly) ? Get(pluginType.Name) : null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Abstractions.Utils; | ||
| using DevProxy.Commands; | ||
| using System.CommandLine; | ||
| using Xunit; | ||
|
|
||
| namespace DevProxy.Tests; | ||
|
|
||
| public sealed class HelpDocsTests | ||
| { | ||
| private static RootCommand CreateRootCommand(out Command pluginCommand) | ||
| { | ||
| var root = new RootCommand(); | ||
| var api = new Command("api"); | ||
| var show = new Command("show"); | ||
| show.SetAction(_ => { }); | ||
| api.Subcommands.Add(show); | ||
| root.Subcommands.Add(api); | ||
|
|
||
| pluginCommand = new Command("mocks"); | ||
| var pluginSubcommand = new Command("from-http-responses"); | ||
| pluginSubcommand.SetAction(_ => { }); | ||
| pluginCommand.Subcommands.Add(pluginSubcommand); | ||
| root.Subcommands.Add(pluginCommand); | ||
|
|
||
| return root; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetLink_RootCommand_ReturnsCliLink() | ||
| { | ||
| var root = CreateRootCommand(out _); | ||
|
|
||
| Assert.Equal("https://go.devproxy.net?go=cli", HelpDocs.GetLink(root.Parse(""))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetLink_LeafCommand_ReturnsCommandPathLink() | ||
| { | ||
| var root = CreateRootCommand(out _); | ||
|
|
||
| Assert.Equal("https://go.devproxy.net?go=cli-api-show", HelpDocs.GetLink(root.Parse("api show"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetLink_GroupCommand_ReturnsCliLink() | ||
| { | ||
| var root = CreateRootCommand(out _); | ||
|
|
||
| Assert.Equal("https://go.devproxy.net?go=cli", HelpDocs.GetLink(root.Parse("api"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetLink_CommandWithExplicitLink_AppliesToSubcommands() | ||
| { | ||
| var root = CreateRootCommand(out var pluginCommand); | ||
| HelpDocs.Set(pluginCommand, DocsLinks.Get("MockResponsePlugin")); | ||
|
|
||
| Assert.Equal("https://go.devproxy.net?go=mockresponseplugin", HelpDocs.GetLink(root.Parse("mocks"))); | ||
| Assert.Equal("https://go.devproxy.net?go=mockresponseplugin", HelpDocs.GetLink(root.Parse("mocks from-http-responses"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetLink_CommandWithoutDocs_ReturnsNull() | ||
| { | ||
| var root = CreateRootCommand(out var pluginCommand); | ||
| HelpDocs.Set(pluginCommand, null); | ||
|
|
||
| Assert.Null(HelpDocs.GetLink(root.Parse("mocks from-http-responses"))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForPlugin_ThirdPartyPlugin_ReturnsNull() | ||
| { | ||
| Assert.Null(DocsLinks.ForPlugin(typeof(HelpDocsTests))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Commands; | ||
| using Xunit; | ||
|
|
||
| namespace DevProxy.Tests; | ||
|
|
||
| [Collection("console-output")] | ||
| public sealed class PluginCommandHelpTests | ||
| { | ||
| [Theory] | ||
| [InlineData(new[] { "plugin", "--help" }, "https://go.devproxy.net?go=cli")] | ||
| [InlineData(new[] { "plugin", "list", "--help" }, "https://go.devproxy.net?go=cli-plugin-list")] | ||
| public async Task RunStandaloneAsync_Help_PrintsDocsFooterAfterExamples(string[] args, string expectedLink) | ||
| { | ||
| var originalOut = Console.Out; | ||
| using var output = new StringWriter(); | ||
| Console.SetOut(output); | ||
| int exitCode; | ||
| try | ||
| { | ||
| exitCode = await PluginCommand.RunStandaloneAsync([.. args, "--log-level", "None"]); | ||
| } | ||
| finally | ||
| { | ||
| Console.SetOut(originalOut); | ||
| } | ||
|
|
||
| Assert.Equal(0, exitCode); | ||
| var text = output.ToString(); | ||
| var lines = text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); | ||
| Assert.Equal($"Docs: {expectedLink}", lines[^1]); | ||
| if (args.Contains("list")) | ||
| { | ||
| Assert.True(text.IndexOf("Examples:", StringComparison.Ordinal) < text.IndexOf("Docs:", StringComparison.Ordinal)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Abstractions.Utils; | ||
| using System.CommandLine; | ||
| using System.CommandLine.Parsing; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace DevProxy.Commands; | ||
|
|
||
| static class HelpDocs | ||
| { | ||
| // Commands with an explicit docs link (for example contributed by plugins). | ||
| // A null value means the command has no canonical docs page. | ||
| private static readonly ConditionalWeakTable<Command, StrongBox<string?>> _links = new(); | ||
|
|
||
| public static void Set(Command command, string? link, bool recursive = true) | ||
| { | ||
| _links.AddOrUpdate(command, new(link)); | ||
| if (!recursive) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var subcommand in command.Subcommands) | ||
| { | ||
| Set(subcommand, link, recursive); | ||
| } | ||
| } | ||
|
|
||
| public static string? GetLink(ParseResult parseResult) | ||
| { | ||
| var command = parseResult.CommandResult.Command; | ||
| if (_links.TryGetValue(command, out var link)) | ||
| { | ||
| return link.Value; | ||
| } | ||
|
|
||
| if (command is RootCommand || command.Action is null) | ||
| { | ||
| return DocsLinks.Cli; | ||
| } | ||
|
|
||
| var path = new List<string>(); | ||
| for (var result = parseResult.CommandResult; result is not null; result = result.Parent as CommandResult) | ||
| { | ||
| if (result.Command is RootCommand) | ||
| { | ||
| break; | ||
| } | ||
| path.Insert(0, result.Command.Name); | ||
| } | ||
|
|
||
| return DocsLinks.Get($"cli-{string.Join('-', path)}"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.