Skip to content

Commit 1322829

Browse files
edvilmedsplaisted
authored andcommitted
tool-run: --from-source
1 parent 9cc3c78 commit 1322829

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
@@ -6,7 +6,9 @@
66
using System.CommandLine;
77
using Microsoft.DotNet.Cli.CommandFactory;
88
using Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
9+
using Microsoft.DotNet.Cli.Commands.Tool.Install;
910
using Microsoft.DotNet.Cli.ToolManifest;
11+
using Microsoft.DotNet.Cli.ToolPackage;
1012
using Microsoft.DotNet.Cli.Utils;
1113
using Microsoft.Extensions.EnvironmentAbstractions;
1214

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

29+
private readonly ToolInstallLocalInstaller _toolInstaller = new(result);
30+
private readonly IToolManifestEditor _toolManifestEditor = new ToolManifestEditor();
31+
private readonly ILocalToolsResolverCache _localToolsResolverCache = new LocalToolsResolverCache();
2632
public override int Execute()
2733
{
2834
CommandSpec commandspec = _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments()
@@ -33,6 +39,11 @@ public override int Execute()
3339

3440
}, _allowRollForward);
3541

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

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

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

29+
public static readonly Option<bool> FromSourceOption = new("--from-source");
30+
2931
private static readonly Command Command = ConstructCommand();
3032

3133
public static Command GetCommand()
@@ -40,6 +42,7 @@ private static Command ConstructCommand()
4042
command.Arguments.Add(CommandNameArgument);
4143
command.Arguments.Add(CommandArgument);
4244
command.Options.Add(RollForwardOption);
45+
command.Options.Add(FromSourceOption);
4346

4447
command.SetAction((parseResult) => new ToolRunCommand(parseResult).Execute());
4548

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
@@ -8,7 +8,6 @@
88
using Microsoft.DotNet.Cli.Commands.Tool.List;
99
using Microsoft.DotNet.Cli.Commands.Tool.Restore;
1010
using Microsoft.DotNet.Cli.Commands.Tool.Run;
11-
using Microsoft.DotNet.Cli.Commands.Tool.Runx;
1211
using Microsoft.DotNet.Cli.Commands.Tool.Search;
1312
using Microsoft.DotNet.Cli.Commands.Tool.Uninstall;
1413
using Microsoft.DotNet.Cli.Commands.Tool.Update;
@@ -38,7 +37,6 @@ private static Command ConstructCommand()
3837
command.Subcommands.Add(ToolRunCommandParser.GetCommand());
3938
command.Subcommands.Add(ToolSearchCommandParser.GetCommand());
4039
command.Subcommands.Add(ToolRestoreCommandParser.GetCommand());
41-
command.Subcommands.Add(ToolRunxCommandParser.GetCommand());
4240

4341
command.SetAction((parseResult) => parseResult.HandleMissingCommand());
4442

src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs

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

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

0 commit comments

Comments
 (0)