44using System . CommandLine ;
55using Microsoft . DotNet . Cli . CommandFactory ;
66using Microsoft . DotNet . Cli . CommandFactory . CommandResolution ;
7+ using Microsoft . DotNet . Cli . Commands . Tool . Install ;
78using Microsoft . DotNet . Cli . ToolManifest ;
9+ using Microsoft . DotNet . Cli . ToolPackage ;
810using Microsoft . DotNet . Cli . Utils ;
911using 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}
0 commit comments