Skip to content
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

Remove RegisterWithDotnetSuggest #2099

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ System.CommandLine
public CommandLineConfiguration Build()
public CommandLineBuilder CancelOnProcessTermination(System.Nullable<System.TimeSpan> timeout = null)
public CommandLineBuilder EnablePosixBundling(System.Boolean value = True)
public CommandLineBuilder RegisterWithDotnetSuggest()
public CommandLineBuilder UseDefaults()
public CommandLineBuilder UseEnvironmentVariableDirective()
public CommandLineBuilder UseExceptionHandler(System.Func<System.Exception,System.CommandLine.Invocation.InvocationContext,System.Int32> onException = null, System.Int32 errorExitCode = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public void Test_app_supplies_suggestions()
[ReleaseBuildOnlyFact]
public void Dotnet_suggest_provides_suggestions_for_app()
{
// run once to trigger a call to dotnet-suggest register
// run "dotnet-suggest register" in explicit way
Process.RunToCompletion(
_endToEndTestApp.FullName,
"-h",
_dotnetSuggest.FullName,
$"register --command-path \"{_endToEndTestApp.FullName}\"",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables: _environmentVariables).Should().Be(0);
Expand Down Expand Up @@ -125,10 +125,10 @@ public void Dotnet_suggest_provides_suggestions_for_app()
[ReleaseBuildOnlyFact]
public void Dotnet_suggest_provides_suggestions_for_app_with_only_commandname()
{
// run once to trigger a call to dotnet-suggest register
// run "dotnet-suggest register" in explicit way
Process.RunToCompletion(
_endToEndTestApp.FullName,
"-h",
_dotnetSuggest.FullName,
$"register --command-path \"{_endToEndTestApp.FullName}\"",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables: _environmentVariables).Should().Be(0);
Expand Down
57 changes: 0 additions & 57 deletions src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.IO;
using System.Threading;
using Process = System.CommandLine.Invocation.Process;

namespace System.CommandLine
{
Expand Down Expand Up @@ -60,59 +57,6 @@ public CommandLineBuilder EnablePosixBundling(bool value = true)
return this;
}

/// <summary>
/// Ensures that the application is registered with the <c>dotnet-suggest</c> tool to enable command line completions.
/// </summary>
/// <remarks>For command line completions to work, users must install the <c>dotnet-suggest</c> tool as well as the appropriate shim script for their shell.</remarks>
/// <returns>The reference to this <see cref="CommandLineBuilder"/> instance.</returns>
public CommandLineBuilder RegisterWithDotnetSuggest()
{
AddMiddleware(async (context, cancellationToken, next) =>
{
var feature = new FeatureRegistration("dotnet-suggest-registration");

await feature.EnsureRegistered(async () =>
{
var stdOut = StringBuilderPool.Default.Rent();
var stdErr = StringBuilderPool.Default.Rent();

try
{
var currentProcessFullPath = Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
var currentProcessFileNameWithoutExtension = Path.GetFileNameWithoutExtension(currentProcessFullPath);

var dotnetSuggestProcess = Process.StartProcess(
command: "dotnet-suggest",
args: $"register --command-path \"{currentProcessFullPath}\" --suggestion-command \"{currentProcessFileNameWithoutExtension}\"",
stdOut: value => stdOut.Append(value),
stdErr: value => stdOut.Append(value));

await dotnetSuggestProcess.CompleteAsync(cancellationToken);

return $@"{dotnetSuggestProcess.StartInfo.FileName} exited with code {dotnetSuggestProcess.ExitCode}
OUT:
{stdOut}
ERR:
{stdErr}";
}
catch (Exception exception)
{
return $@"Exception during registration:
{exception}";
}
finally
{
StringBuilderPool.Default.ReturnToPool(stdOut);
StringBuilderPool.Default.ReturnToPool(stdErr);
}
});

await next(context, cancellationToken);
}, MiddlewareOrderInternal.RegisterWithDotnetSuggest);

return this;
}

/// <inheritdoc cref="EnvironmentVariablesDirective"/>
/// <returns>The reference to this <see cref="CommandLineBuilder"/> instance.</returns>
public CommandLineBuilder UseEnvironmentVariableDirective()
Expand Down Expand Up @@ -148,7 +92,6 @@ public CommandLineBuilder UseDefaults()
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseTypoCorrections()
.UseParseErrorReporting()
.UseExceptionHandler()
Expand Down
74 changes: 34 additions & 40 deletions src/System.CommandLine/Help/HelpBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace System.CommandLine.Help
{
Expand Down Expand Up @@ -265,59 +266,52 @@ public void WriteColumns(IReadOnlyList<TwoColumnHelpRow> items, HelpContext cont

private string FormatArgumentUsage(IList<Argument> arguments)
{
var sb = StringBuilderPool.Default.Rent();
var sb = new StringBuilder(arguments.Count * 100);

try
{
var end = default(Stack<char>);
var end = default(Stack<char>);

for (var i = 0; i < arguments.Count; i++)
for (var i = 0; i < arguments.Count; i++)
{
var argument = arguments[i];
if (argument.IsHidden)
{
var argument = arguments[i];
if (argument.IsHidden)
{
continue;
}
continue;
}

var arityIndicator =
argument.Arity.MaximumNumberOfValues > 1
? "..."
: "";
var arityIndicator =
argument.Arity.MaximumNumberOfValues > 1
? "..."
: "";

var isOptional = IsOptional(argument);
var isOptional = IsOptional(argument);

if (isOptional)
{
sb.Append($"[<{argument.Name}>{arityIndicator}");
(end ??= new Stack<char>()).Push(']');
}
else
{
sb.Append($"<{argument.Name}>{arityIndicator}");
}

sb.Append(' ');
if (isOptional)
{
sb.Append($"[<{argument.Name}>{arityIndicator}");
(end ??= new Stack<char>()).Push(']');
}

if (sb.Length > 0)
else
{
sb.Length--;

if (end is { })
{
while (end.Count > 0)
{
sb.Append(end.Pop());
}
}
sb.Append($"<{argument.Name}>{arityIndicator}");
}

return sb.ToString();
sb.Append(' ');
}
finally

if (sb.Length > 0)
{
StringBuilderPool.Default.ReturnToPool(sb);
sb.Length--;

if (end is { })
{
while (end.Count > 0)
{
sb.Append(end.Pop());
}
}
}

return sb.ToString();

bool IsOptional(Argument argument) =>
argument.Arity.MinimumNumberOfValues == 0;
Expand Down
48 changes: 0 additions & 48 deletions src/System.CommandLine/Invocation/FeatureRegistration.cs

This file was deleted.

88 changes: 0 additions & 88 deletions src/System.CommandLine/Invocation/Process.cs

This file was deleted.

Loading