Skip to content

Commit 1beb4bb

Browse files
edvilmedsplaisted
authored andcommitted
tool-exec
1 parent fdc0161 commit 1beb4bb

File tree

6 files changed

+86
-49
lines changed

6 files changed

+86
-49
lines changed

src/Cli/dotnet/Commands/Tool/Run/ToolRunFromSourceCommand.cs renamed to src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66
using Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
77
using Microsoft.DotNet.Cli.Commands.Tool.Install;
88
using Microsoft.DotNet.Cli.ToolPackage;
9+
using NuGet.Packaging.Core;
910
using NuGet.Versioning;
1011

11-
namespace Microsoft.DotNet.Cli.Commands.Tool.Run
12+
namespace Microsoft.DotNet.Cli.Commands.Tool.Execute
1213
{
13-
internal class ToolRunFromSourceCommand(ParseResult result) : CommandBase(result)
14+
internal class ToolExecuteCommand(ParseResult result) : CommandBase(result)
1415
{
15-
private readonly string? _toolCommandName = result.GetValue(ToolRunCommandParser.CommandNameArgument);
16-
private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolRunCommandParser.CommandArgument) ?? Enumerable.Empty<string>();
17-
private readonly bool _allowRollForward = result.GetValue(ToolRunCommandParser.RollForwardOption);
18-
private readonly string? _configFile = result.GetValue(ToolRunCommandParser.FromSourceConfigFile);
19-
private readonly string[] _sources = result.GetValue(ToolRunCommandParser.FromSourceSourceOption) ?? [];
20-
private readonly string[] _addSource = result.GetValue(ToolRunCommandParser.FromSourceAddSourceOption) ?? [];
16+
private readonly PackageIdentity? _packageToolIdentityArgument = result.GetValue(ToolExecuteCommandParser.PackageIdentityArgument);
17+
private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolExecuteCommandParser.CommandArgument) ?? Enumerable.Empty<string>();
18+
private readonly bool _allowRollForward = result.GetValue(ToolExecuteCommandParser.RollForwardOption);
19+
private readonly string? _configFile = result.GetValue(ToolExecuteCommandParser.ConfigOption);
20+
private readonly string[] _sources = result.GetValue(ToolExecuteCommandParser.SourceOption) ?? [];
21+
private readonly string[] _addSource = result.GetValue(ToolExecuteCommandParser.AddSourceOption) ?? [];
2122
private readonly bool _ignoreFailedSources = result.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption);
22-
private readonly bool _interactive = result.GetValue(ToolRunCommandParser.FromSourceInteractiveOption);
23-
private readonly VerbosityOptions _verbosity = result.GetValue(ToolRunCommandParser.FromSourceVerbosityOption);
24-
private readonly bool _yes = result.GetValue(ToolRunCommandParser.FromSourceYesOption);
23+
private readonly bool _interactive = result.GetValue(ToolExecuteCommandParser.InteractiveOption);
24+
private readonly VerbosityOptions _verbosity = result.GetValue(ToolExecuteCommandParser.VerbosityOption);
25+
private readonly bool _yes = result.GetValue(ToolExecuteCommandParser.YesOption);
2526

2627
public override int Execute()
2728
{
28-
if (!UserAgreedToRunFromSource())
29+
if (!UserAgreedToRunFromSource() || _packageToolIdentityArgument is null)
2930
{
3031
return 1;
3132
}
@@ -35,7 +36,8 @@ public override int Execute()
3536
_forwardArguments.Append("--allow-roll-forward");
3637
}
3738

38-
PackageId packageId = new(_toolCommandName);
39+
PackageId packageId = new PackageId(_packageToolIdentityArgument.Id);
40+
3941
VersionRange versionRange = _parseResult.GetVersionRange();
4042

4143
string tempDirectory = PathUtilities.CreateTempSubdirectory();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.CommandLine;
7+
using System.Text;
8+
using Microsoft.DotNet.Cli.Commands.Tool.Install;
9+
using NuGet.Packaging.Core;
10+
11+
namespace Microsoft.DotNet.Cli.Commands.Tool.Execute
12+
{
13+
internal static class ToolExecuteCommandParser
14+
15+
{
16+
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = ToolInstallCommandParser.PackageIdentityArgument;
17+
18+
public static readonly Argument<IEnumerable<string>> CommandArgument = new("commandArguments")
19+
{
20+
Description = "arguments forwarded to the tool"
21+
};
22+
23+
public static readonly Option<bool> RollForwardOption = ToolInstallCommandParser.RollForwardOption;
24+
public static readonly Option<bool> PrereleaseOption = ToolInstallCommandParser.PrereleaseOption;
25+
public static readonly Option<string> ConfigOption = ToolInstallCommandParser.ConfigOption;
26+
public static readonly Option<string[]> SourceOption = ToolInstallCommandParser.SourceOption;
27+
public static readonly Option<string[]> AddSourceOption = ToolInstallCommandParser.AddSourceOption;
28+
public static readonly Option<bool> IgnoreFailedSourcesOption = ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption;
29+
public static readonly Option<bool> InteractiveOption = CommonOptions.InteractiveOption();
30+
public static readonly Option<bool> YesOption = CommonOptions.YesOption;
31+
public static readonly Option<VerbosityOptions> VerbosityOption = ToolInstallCommandParser.VerbosityOption;
32+
33+
34+
public static readonly Command Command = ConstructCommand();
35+
public static Command GetCommand()
36+
{
37+
return Command;
38+
}
39+
40+
private static Command ConstructCommand()
41+
{
42+
Command command = new("execute", "Execute a tool command from source");
43+
44+
command.Aliases.Add("exec");
45+
46+
command.Arguments.Add(PackageIdentityArgument);
47+
command.Arguments.Add(CommandArgument);
48+
49+
command.Options.Add(RollForwardOption);
50+
command.Options.Add(PrereleaseOption);
51+
command.Options.Add(ConfigOption);
52+
command.Options.Add(SourceOption);
53+
command.Options.Add(AddSourceOption);
54+
command.Options.Add(IgnoreFailedSourcesOption);
55+
command.Options.Add(InteractiveOption);
56+
command.Options.Add(YesOption);
57+
command.Options.Add(VerbosityOption);
58+
59+
command.SetAction((parseResult) => new ToolExecuteCommand(parseResult).Execute());
60+
61+
return command;
62+
}
63+
}
64+
}

src/Cli/dotnet/Commands/Tool/Install/ParseResultExtension.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ internal static class ParseResultExtension
1313
{
1414
public static VersionRange GetVersionRange(this ParseResult parseResult)
1515
{
16-
string packageVersion = parseResult.GetValue(ToolInstallCommandParser.PackageIdentityArgument)?.Version?.ToString() ??
16+
string packageVersion =
17+
parseResult.GetValue(CommonArguments.PackageIdentityArgument(false))?.Version?.ToString() ??
18+
parseResult.GetValue(CommonArguments.PackageIdentityArgument(true))?.Version?.ToString() ??
1719
parseResult.GetValue(ToolInstallCommandParser.VersionOption);
20+
1821
bool prerelease = parseResult.GetValue(ToolInstallCommandParser.PrereleaseOption);
1922

2023
if (!string.IsNullOrEmpty(packageVersion) && prerelease)

src/Cli/dotnet/Commands/Tool/Run/ToolRunCommand.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ internal class ToolRunCommand(
2121
{
2222
private readonly string _toolCommandName = result.GetValue(ToolRunCommandParser.CommandNameArgument);
2323
private readonly IEnumerable<string> _forwardArgument = result.GetValue(ToolRunCommandParser.CommandArgument);
24-
private readonly bool _fromSource = result.GetValue(ToolRunCommandParser.FromSourceOption);
2524

2625
private readonly LocalToolsCommandResolver _localToolsCommandResolver = localToolsCommandResolver ?? new LocalToolsCommandResolver();
2726
public bool _allowRollForward = result.GetValue(ToolRunCommandParser.RollForwardOption);
@@ -36,12 +35,6 @@ public override int Execute()
3635

3736
}, _allowRollForward);
3837

39-
if (commandSpec == null && _fromSource)
40-
{
41-
// Reroute to ToolRunFromSourceCommand
42-
return new ToolRunFromSourceCommand(_parseResult).Execute();
43-
}
44-
4538
if (commandSpec == null)
4639
{
4740
throw new GracefulException([string.Format(CliCommandStrings.CannotFindCommandName, _toolCommandName)], isUserError: false);

src/Cli/dotnet/Commands/Tool/Run/ToolRunCommandParser.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@ internal static class ToolRunCommandParser
2929
Arity = ArgumentArity.Zero
3030
};
3131

32-
public static readonly Option<bool> FromSourceOption = new("--from-source")
33-
{
34-
Description = CliCommandStrings.ToolRunFromSourceOptionDescription,
35-
Arity = ArgumentArity.Zero
36-
};
37-
38-
public static readonly Option<string> FromSourceConfigFile = ToolInstallCommandParser.ConfigOption;
39-
40-
public static readonly Option<string[]> FromSourceSourceOption = ToolInstallCommandParser.SourceOption;
41-
42-
public static readonly Option<string[]> FromSourceAddSourceOption = ToolInstallCommandParser.AddSourceOption;
43-
44-
public static readonly Option<VerbosityOptions> FromSourceVerbosityOption = CommonOptions.VerbosityOption;
45-
46-
public static readonly Option<bool> FromSourceInteractiveOption = CommonOptions.InteractiveOption();
47-
48-
public static readonly Option<bool> FromSourceYesOption = CommonOptions.YesOption;
49-
5032
private static readonly Command Command = ConstructCommand();
5133

5234
public static Command GetCommand()
@@ -60,17 +42,8 @@ private static Command ConstructCommand()
6042

6143
command.Arguments.Add(CommandNameArgument);
6244
command.Arguments.Add(CommandArgument);
63-
command.Options.Add(RollForwardOption);
64-
65-
command.Options.Add(FromSourceOption);
66-
command.Options.Add(FromSourceConfigFile);
67-
command.Options.Add(FromSourceSourceOption);
68-
command.Options.Add(FromSourceAddSourceOption);
69-
command.Options.Add(FromSourceVerbosityOption);
70-
command.Options.Add(FromSourceInteractiveOption);
71-
command.Options.Add(FromSourceYesOption);
7245

73-
command.Options.Add(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption);
46+
command.Options.Add(RollForwardOption);
7447

7548
command.SetAction((parseResult) => new ToolRunCommand(parseResult).Execute());
7649

src/Cli/dotnet/Commands/Tool/ToolCommandParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.CommandLine;
7+
using Microsoft.DotNet.Cli.Commands.Tool.Execute;
78
using Microsoft.DotNet.Cli.Commands.Tool.Install;
89
using Microsoft.DotNet.Cli.Commands.Tool.List;
910
using Microsoft.DotNet.Cli.Commands.Tool.Restore;
@@ -37,6 +38,7 @@ private static Command ConstructCommand()
3738
command.Subcommands.Add(ToolRunCommandParser.GetCommand());
3839
command.Subcommands.Add(ToolSearchCommandParser.GetCommand());
3940
command.Subcommands.Add(ToolRestoreCommandParser.GetCommand());
41+
command.Subcommands.Add(ToolExecuteCommandParser.GetCommand());
4042

4143
command.SetAction((parseResult) => parseResult.HandleMissingCommand());
4244

0 commit comments

Comments
 (0)