Skip to content

[WIP] dotnet run: --from-source #48443

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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions src/Cli/dotnet/Commands/CliCommandStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2472,4 +2472,7 @@ To display a value, specify the corresponding command-line option without provid
<data name="ZeroTestsRan" xml:space="preserve">
<value>Zero tests ran</value>
</data>
<data name="ToolRunFromSourceOptionDescription" xml:space="preserve">
<value>Executes a tool from source without permanently installing it. </value>
</data>
</root>
51 changes: 51 additions & 0 deletions src/Cli/dotnet/Commands/Tool/Run/ToolRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.CommandLine;
using Microsoft.DotNet.Cli.CommandFactory;
using Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
using Microsoft.DotNet.Cli.Commands.Tool.Install;
using Microsoft.DotNet.Cli.ToolManifest;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.EnvironmentAbstractions;

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

private readonly IToolManifestEditor _toolManifestEditor = new ToolManifestEditor();
private readonly ILocalToolsResolverCache _localToolsResolverCache = new LocalToolsResolverCache();
public override int Execute()
{
CommandSpec commandspec = _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments()
Expand All @@ -31,6 +36,11 @@ public override int Execute()

}, _allowRollForward);

if (commandspec == null && _fromSource)
{
return ExecuteFromSource();
}

if (commandspec == null)
{
throw new GracefulException([string.Format(CliCommandStrings.CannotFindCommandName, _toolCommandName)], isUserError: false);
Expand All @@ -39,4 +49,45 @@ public override int Execute()
var result = CommandFactoryUsingResolver.Create(commandspec).Execute();
return result.ExitCode;
}

public int ExecuteFromSource()
{
string tempDirectory = PathUtilities.CreateTempSubdirectory();
FilePath manifestFile = _toolManifest.FindFirst(true);
PackageId packageId = new(_toolCommandName);

ToolInstallLocalInstaller _toolInstaller = new(_parseResult, new ToolPackageDownloader(
localToolDownloadDir: tempDirectory,
store: new ToolPackageStoreAndQuery(new DirectoryPath(tempDirectory))));

IToolPackage toolPackage = _toolInstaller.Install(manifestFile, packageId);

_toolManifestEditor.Add(
manifestFile,
toolPackage.Id,
toolPackage.Version,
[toolPackage.Command.Name],
_allowRollForward);

_localToolsResolverCache.SaveToolPackage(
toolPackage,
_toolInstaller.TargetFrameworkToInstall);

CommandSpec commandSpec = _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments()
{
CommandName = $"dotnet-{toolPackage.Command.Name}",
CommandArguments = _forwardArgument,
}, _allowRollForward);

if (commandSpec == null)
{
throw new GracefulException([string.Format(CliCommandStrings.CannotFindCommandName, _toolCommandName)], isUserError: false);
}

var result = CommandFactoryUsingResolver.Create(commandSpec).Execute();

_toolManifestEditor.Remove(manifestFile, toolPackage.Id);

return result.ExitCode;
}
}
7 changes: 7 additions & 0 deletions src/Cli/dotnet/Commands/Tool/Run/ToolRunCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ internal static class ToolRunCommandParser
Arity = ArgumentArity.Zero
};

public static readonly CliOption<bool> FromSourceOption = new("--from-source")
{
Description = CliCommandStrings.ToolRunFromSourceOptionDescription,
Arity = ArgumentArity.Zero
};

private static readonly CliCommand Command = ConstructCommand();

public static CliCommand GetCommand()
Expand All @@ -38,6 +44,7 @@ private static CliCommand ConstructCommand()
command.Arguments.Add(CommandNameArgument);
command.Arguments.Add(CommandArgument);
command.Options.Add(RollForwardOption);
command.Options.Add(FromSourceOption);

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

Expand Down
5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Cli/dotnet/ToolManifest/ToolManifestFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public FilePath FindFirst(bool createIfNotFound = false)

/*
The --create-manifest-if-needed will use the following priority to choose the folder where the tool manifest goes:
1. Walk up the directory tree searching for one that has a.git subfolder
1. Walk up the directory tree searching for one that has a .git subfolder
2. Walk up the directory tree searching for one that has a .sln(x)/git file in it
3. Use the current working directory
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ internal class ToolPackageDownloader : IToolPackageDownloader
public ToolPackageDownloader(
IToolPackageStore store,
string runtimeJsonPathForTests = null,
string currentWorkingDirectory = null
string currentWorkingDirectory = null,
string localToolDownloadDir = null
)
{
_toolPackageStore = store ?? throw new ArgumentNullException(nameof(store));
_globalToolStageDir = _toolPackageStore.GetRandomStagingDirectory();
ISettings settings = Settings.LoadDefaultSettings(currentWorkingDirectory ?? Directory.GetCurrentDirectory());
_localToolDownloadDir = new DirectoryPath(SettingsUtility.GetGlobalPackagesFolder(settings));
_localToolDownloadDir = new DirectoryPath(localToolDownloadDir ?? SettingsUtility.GetGlobalPackagesFolder(settings));
_currentWorkingDirectory = currentWorkingDirectory;

_localToolAssetDir = new DirectoryPath(PathUtilities.CreateTempSubdirectory());
Expand Down
Loading