Skip to content

Commit b11e3b8

Browse files
committed
tool-run: --from-source
1 parent 87d8208 commit b11e3b8

File tree

6 files changed

+40
-99
lines changed

6 files changed

+40
-99
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.CommandFactory;
66
using Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
7+
using Microsoft.DotNet.Cli.Commands.Tool.Install;
78
using Microsoft.DotNet.Cli.ToolManifest;
9+
using Microsoft.DotNet.Cli.ToolPackage;
810
using Microsoft.DotNet.Cli.Utils;
911
using Microsoft.Extensions.EnvironmentAbstractions;
1012

@@ -20,7 +22,11 @@ internal class ToolRunCommand(
2022
private readonly IEnumerable<string> _forwardArgument = result.GetValue(ToolRunCommandParser.CommandArgument);
2123
public bool _allowRollForward = result.GetValue(ToolRunCommandParser.RollForwardOption);
2224
private readonly ToolManifestFinder _toolManifest = toolManifest ?? new ToolManifestFinder(new DirectoryPath(Directory.GetCurrentDirectory()));
25+
private readonly bool _fromSource = result.GetValue(ToolRunCommandParser.FromSourceOption);
2326

27+
private readonly ToolInstallLocalInstaller _toolInstaller = new(result);
28+
private readonly IToolManifestEditor _toolManifestEditor = new ToolManifestEditor();
29+
private readonly ILocalToolsResolverCache _localToolsResolverCache = new LocalToolsResolverCache();
2430
public override int Execute()
2531
{
2632
CommandSpec commandspec = _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments()
@@ -31,6 +37,11 @@ public override int Execute()
3137

3238
}, _allowRollForward);
3339

40+
if (commandspec == null && _fromSource)
41+
{
42+
commandspec = GetRemoteCommandSpec();
43+
}
44+
3445
if (commandspec == null)
3546
{
3647
throw new GracefulException([string.Format(CliCommandStrings.CannotFindCommandName, _toolCommandName)], isUserError: false);
@@ -39,4 +50,29 @@ public override int Execute()
3950
var result = CommandFactoryUsingResolver.Create(commandspec).Execute();
4051
return result.ExitCode;
4152
}
53+
54+
public CommandSpec? GetRemoteCommandSpec()
55+
{
56+
FilePath manifestFile = _toolManifest.FindFirst(true);
57+
PackageId packageId = new(_toolCommandName);
58+
59+
IToolPackage toolPackage = _toolInstaller.Install(manifestFile, packageId);
60+
61+
_toolManifestEditor.Add(
62+
manifestFile,
63+
toolPackage.Id,
64+
toolPackage.Version,
65+
[toolPackage.Command.Name],
66+
_allowRollForward);
67+
68+
_localToolsResolverCache.SaveToolPackage(
69+
toolPackage,
70+
_toolInstaller.TargetFrameworkToInstall);
71+
72+
return _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments()
73+
{
74+
CommandName = $"dotnet-{toolPackage.Command.Name}",
75+
CommandArguments = _forwardArgument,
76+
}, _allowRollForward);
77+
}
4278
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal static class ToolRunCommandParser
2424
Arity = ArgumentArity.Zero
2525
};
2626

27+
public static readonly CliOption<bool> FromSourceOption = new("--from-source");
28+
2729
private static readonly CliCommand Command = ConstructCommand();
2830

2931
public static CliCommand GetCommand()
@@ -38,6 +40,7 @@ private static CliCommand ConstructCommand()
3840
command.Arguments.Add(CommandNameArgument);
3941
command.Arguments.Add(CommandArgument);
4042
command.Options.Add(RollForwardOption);
43+
command.Options.Add(FromSourceOption);
4144

4245
command.SetAction((parseResult) => new ToolRunCommand(parseResult).Execute());
4346

src/Cli/dotnet/Commands/Tool/Runx/ToolRunxCommand.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Cli/dotnet/Commands/Tool/Runx/ToolRunxCommandParser.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.DotNet.Cli.Commands.Tool.List;
77
using Microsoft.DotNet.Cli.Commands.Tool.Restore;
88
using Microsoft.DotNet.Cli.Commands.Tool.Run;
9-
using Microsoft.DotNet.Cli.Commands.Tool.Runx;
109
using Microsoft.DotNet.Cli.Commands.Tool.Search;
1110
using Microsoft.DotNet.Cli.Commands.Tool.Uninstall;
1211
using Microsoft.DotNet.Cli.Commands.Tool.Update;
@@ -36,7 +35,6 @@ private static CliCommand ConstructCommand()
3635
command.Subcommands.Add(ToolRunCommandParser.GetCommand());
3736
command.Subcommands.Add(ToolSearchCommandParser.GetCommand());
3837
command.Subcommands.Add(ToolRestoreCommandParser.GetCommand());
39-
command.Subcommands.Add(ToolRunxCommandParser.GetCommand());
4038

4139
command.SetAction((parseResult) => parseResult.HandleMissingCommand());
4240

src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public FilePath FindFirst(bool createIfNotFound = false)
189189

190190
/*
191191
The --create-manifest-if-needed will use the following priority to choose the folder where the tool manifest goes:
192-
1. Walk up the directory tree searching for one that has a.git subfolder
192+
1. Walk up the directory tree searching for one that has a .git subfolder
193193
2. Walk up the directory tree searching for one that has a .sln(x)/git file in it
194194
3. Use the current working directory
195195
*/

0 commit comments

Comments
 (0)