Skip to content
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

[Issue #37237 #19650] Dotnet Cli Feature: dotnet tool update --all Updating all tools with one command #38996

Merged
merged 49 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1b9cb84
add --all option to parser
JL03-Yue Jan 31, 2024
1b654a3
add option to toolupdate; create new file
JL03-Yue Jan 31, 2024
674d99a
skeleton 1
JL03-Yue Jan 31, 2024
440f1fd
PoC for list
JL03-Yue Feb 17, 2024
e8e112f
separating global and local
JL03-Yue Feb 23, 2024
e302c95
Framework for subcommand --all
JL03-Yue Feb 23, 2024
7516b38
initial commit
Markliniubility Feb 25, 2024
4b61bd7
remove scratch
Markliniubility Feb 25, 2024
49c1602
tbd for subcommand parser
JL03-Yue Feb 26, 2024
28bc88b
subcommand: update all
Markliniubility Mar 2, 2024
4c1b34e
order change
Markliniubility Mar 2, 2024
a4d1dff
update command options
Markliniubility Mar 2, 2024
7e9c94c
Code clean up and refactors
Markliniubility Mar 2, 2024
818ed64
Update ToolUpdateAllCommand.cs
Markliniubility Mar 2, 2024
a6624d1
Unit test: ensure input integrity
Markliniubility Mar 3, 2024
2d942b0
Update ToolUpdateAllCommandTests.cs
Markliniubility Mar 3, 2024
e839ab9
Merge commit 'refs/pull/38996/head' of https://github.com/dotnet/sdk …
JL03-Yue Mar 6, 2024
d6a36c0
Unit Test: Mock File System
Markliniubility Mar 23, 2024
632d6e1
Unit-Test Global or Tool Path
Markliniubility Mar 23, 2024
42f6767
Update All Local Test
Markliniubility Mar 24, 2024
ac43ebd
Merge branch 'update-all' of https://github.com/Markliniubility/sdk i…
JL03-Yue Mar 29, 2024
2446678
Testing optional argument for tool update all
dsplaisted Apr 2, 2024
4b0a271
separate PackageId for tool install and update
JL03-Yue Apr 3, 2024
8557b53
Merge branch 'release/8.0.3xx' into update-all
JL03-Yue Apr 3, 2024
24f9a15
Merge branch 'update-all' of https://github.com/Markliniubility/sdk i…
JL03-Yue Apr 3, 2024
0eb78b3
fix build error
JL03-Yue Apr 4, 2024
17f4deb
parse --all in tool install
JL03-Yue Apr 4, 2024
3f9162c
work on build error
JL03-Yue Apr 4, 2024
62a8710
add --all condition to tool install
JL03-Yue Apr 4, 2024
05fae91
update --all description
JL03-Yue Apr 4, 2024
d51be5e
pass packageId to ToolInstallLocalInstaller
JL03-Yue Apr 8, 2024
c8b4d91
add conflicting logic options; fix tests;
JL03-Yue Apr 8, 2024
5921cfe
create command based on parse result trial
JL03-Yue Apr 8, 2024
81b45f9
create command using buildUpdateCommandArgument method
JL03-Yue Apr 10, 2024
16a103b
fix conflict option testing
Markliniubility Apr 10, 2024
72e6d57
change global install --all logic
JL03-Yue Apr 10, 2024
461dcad
Global Update all unit test
Markliniubility Apr 10, 2024
c2c8b67
Local Update all unit testing
Markliniubility Apr 11, 2024
74f18ac
Clean up
Markliniubility Apr 11, 2024
1f6a2cd
Update src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCo…
Markliniubility Apr 11, 2024
f8b977a
Update src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalCo…
Markliniubility Apr 11, 2024
ce60b24
Unify Install API
Markliniubility Apr 11, 2024
274f83b
Clean up unused functions
Markliniubility Apr 11, 2024
22ce11e
clean up
JL03-Yue Apr 12, 2024
df59c00
Update src/Tests/dotnet.Tests/CommandTests/ToolUpdateLocalCommandTest…
Markliniubility Apr 19, 2024
84a9fc2
Nit Fixes
Markliniubility Apr 19, 2024
779ad38
Update ToolInstallLocalCommand.cs
Markliniubility Apr 20, 2024
daabd3f
Update src/Cli/dotnet/commands/dotnet-tool/install/ToolInstallLocalIn…
Markliniubility Apr 24, 2024
c2d36f8
Nit: remove unnecessary package id in ToolInstallLocalInstaller
Markliniubility Apr 25, 2024
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
32 changes: 32 additions & 0 deletions src/Cli/dotnet/commands/dotnet-tool/common/ToolAppliedOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using UpdateToolsLocalizableStrings = Microsoft.DotNet.Tools.Tool.Update.LocalizableStrings;

namespace Microsoft.DotNet.Tools.Tool.Common
{
Expand All @@ -12,6 +14,13 @@ internal class ToolAppliedOption

public static CliOption<bool> LocalOption = new("--local");

public static CliOption<bool> UpdateAllOption = new("--all")
{
Description = UpdateToolsLocalizableStrings.UpdateAllOptionDescription
};

public static readonly CliOption<string> VersionOption = ToolInstallCommandParser.VersionOption;

public static CliOption<string> ToolPathOption = new("--tool-path")
{
HelpName = Install.LocalizableStrings.ToolPathOptionName
Expand Down Expand Up @@ -45,7 +54,30 @@ internal static void EnsureNoConflictGlobalLocalToolPathOption(

if (options.Count > 1)
{
throw new GracefulException(
string.Format(
message,
string.Join(" ", options)));
}
}

internal static void EnsureNoConflictUpdateAllVersionOption(
ParseResult parseResult,
string message)
{
List<string> options = new List<string>();
if (parseResult.GetResult(UpdateAllOption) is not null)
{
options.Add(UpdateAllOption.Name);
}

if (parseResult.GetResult(VersionOption) is not null)
{
options.Add(VersionOption.Name);
}

if (options.Count > 1)
{
throw new GracefulException(
string.Format(
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static CliCommand GetCommand()
private static CliCommand ConstructCommand()
{
CliCommand command = new("install", LocalizableStrings.CommandDescription);
command.Arguments.Add(PackageIdArgument);

AddCommandOptions(command);

Expand All @@ -100,8 +101,7 @@ private static CliCommand ConstructCommand()
}

public static CliCommand AddCommandOptions(CliCommand command)
{
command.Arguments.Add(PackageIdArgument);
{
command.Options.Add(GlobalOption.WithHelpDescription(command, LocalizableStrings.GlobalOptionDescription));
command.Options.Add(LocalOption.WithHelpDescription(command, LocalizableStrings.LocalOptionDescription));
command.Options.Add(ToolPathOption.WithHelpDescription(command, LocalizableStrings.ToolPathOptionDescription));
Expand All @@ -118,6 +118,6 @@ public static CliCommand AddCommandOptions(CliCommand command)
command.Options.Add(VerbosityOption);

return command;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.Versioning;
using Microsoft.DotNet.Tools.Tool.List;
using static System.Formats.Asn1.AsnWriter;
using System.CommandLine.Parsing;

namespace Microsoft.DotNet.Tools.Tool.Install
{
Expand All @@ -34,8 +36,9 @@ internal class ToolInstallGlobalOrToolPathCommand : CommandBase
private CreateShellShimRepository _createShellShimRepository;
private readonly CreateToolPackageStoresAndDownloaderAndUninstaller _createToolPackageStoreDownloaderUninstaller;
private readonly ShellShimTemplateFinder _shellShimTemplateFinder;
private readonly IToolPackageStoreQuery _store;

private readonly PackageId _packageId;
private readonly PackageId? _packageId;
private readonly string _packageVersion;
private readonly string _configFilePath;
private readonly string _framework;
Expand All @@ -47,18 +50,22 @@ internal class ToolInstallGlobalOrToolPathCommand : CommandBase
private IEnumerable<string> _forwardRestoreArguments;
private readonly bool _allowRollForward;
private readonly bool _allowPackageDowngrade;
private readonly bool _updateAll;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



public ToolInstallGlobalOrToolPathCommand(
ParseResult parseResult,
PackageId? packageId = null,
CreateToolPackageStoresAndDownloaderAndUninstaller createToolPackageStoreDownloaderUninstaller = null,
CreateShellShimRepository createShellShimRepository = null,
IEnvironmentPathInstruction environmentPathInstruction = null,
IReporter reporter = null,
INuGetPackageDownloader nugetPackageDownloader = null)
INuGetPackageDownloader nugetPackageDownloader = null,
IToolPackageStoreQuery store = null)
: base(parseResult)
{
_packageId = new PackageId(parseResult.GetValue(ToolInstallCommandParser.PackageIdArgument));
var packageIdArgument = parseResult.GetValue(ToolInstallCommandParser.PackageIdArgument);
_packageId = packageId ?? (packageIdArgument is not null ? new PackageId(packageIdArgument) : null);
_packageVersion = parseResult.GetValue(ToolInstallCommandParser.VersionOption);
_configFilePath = parseResult.GetValue(ToolInstallCommandParser.ConfigOption);
_framework = parseResult.GetValue(ToolInstallCommandParser.FrameworkOption);
Expand All @@ -83,12 +90,14 @@ public ToolInstallGlobalOrToolPathCommand(
Interactive: parseResult.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));
nugetPackageDownloader ??= new NuGetPackageDownloader(tempDir, verboseLogger: new NullLogger(), restoreActionConfig: restoreAction, verbosityOptions: _verbosity);
_shellShimTemplateFinder = new ShellShimTemplateFinder(nugetPackageDownloader, tempDir, packageSourceLocation);
_store = store;

_allowRollForward = parseResult.GetValue(ToolInstallCommandParser.RollForwardOption);

_allowPackageDowngrade = parseResult.GetValue(ToolInstallCommandParser.AllowPackageDowngradeOption);
_createToolPackageStoreDownloaderUninstaller = createToolPackageStoreDownloaderUninstaller ??
ToolPackageFactory.CreateToolPackageStoresAndDownloaderAndUninstaller;
_updateAll = parseResult.GetValue(ToolUpdateCommandParser.UpdateAllOption);

_reporter = (reporter ?? Reporter.Output);
_errorReporter = (reporter ?? Reporter.Error);
Expand All @@ -106,6 +115,27 @@ public static T GetValueOrDefault<T>(CliOption<T> option, T defaultOption, Parse
}

public override int Execute()
{
if (_updateAll)
{
var toolListCommand = new ToolListGlobalOrToolPathCommand(
_parseResult
, toolPath => { return _store; }
JL03-Yue marked this conversation as resolved.
Show resolved Hide resolved
);
var toolIds = toolListCommand.GetPackages(null, null);
foreach (var toolId in toolIds)
{
ExecuteInstallCommand(new PackageId(toolId.Id.ToString()));
}
return 0;
}
else
{
return ExecuteInstallCommand((PackageId)_packageId);
}
}

private int ExecuteInstallCommand(PackageId packageId)
{
ValidateArguments();

Expand All @@ -125,7 +155,7 @@ public override int Execute()
var appHostSourceDirectory = ShellShimTemplateFinder.GetDefaultAppHostSourceDirectory();
IShellShimRepository shellShimRepository = _createShellShimRepository(appHostSourceDirectory, toolPath);

IToolPackage oldPackageNullable = GetOldPackage(toolPackageStoreQuery);
IToolPackage oldPackageNullable = GetOldPackage(toolPackageStoreQuery, packageId);

using (var scope = new TransactionScope(
TransactionScopeOption.Required,
Expand All @@ -141,14 +171,14 @@ public override int Execute()
}

toolPackageUninstaller.Uninstall(oldPackageNullable.PackageDirectory);
});
}, packageId);
}

RunWithHandlingInstallError(() =>
{
IToolPackage newInstalledPackage = toolPackageDownloader.InstallPackage(
new PackageLocation(nugetConfig: GetConfigFile(), additionalFeeds: _source),
packageId: _packageId,
packageId: packageId,
versionRange: versionRange,
targetFramework: _framework,
verbosity: _verbosity,
Expand Down Expand Up @@ -188,11 +218,11 @@ public override int Execute()
}

PrintSuccessMessage(oldPackageNullable, newInstalledPackage);
});
}, packageId);

scope.Complete();
}

}
return 0;
}

Expand Down Expand Up @@ -222,7 +252,7 @@ private void ValidateArguments()
}
}

private void RunWithHandlingInstallError(Action installAction)
private void RunWithHandlingInstallError(Action installAction, PackageId packageId)
{
try
{
Expand All @@ -233,10 +263,10 @@ private void RunWithHandlingInstallError(Action installAction)
{
var message = new List<string>
{
string.Format(Update.LocalizableStrings.UpdateToolFailed, _packageId)
string.Format(Update.LocalizableStrings.UpdateToolFailed, packageId)
};
message.AddRange(
InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId));
InstallToolCommandLowLevelErrorConverter.GetUserFacingMessages(ex, packageId));


throw new GracefulException(
Expand All @@ -246,7 +276,7 @@ private void RunWithHandlingInstallError(Action installAction)
}
}

private void RunWithHandlingUninstallError(Action uninstallAction)
private void RunWithHandlingUninstallError(Action uninstallAction, PackageId packageId)
{
try
{
Expand All @@ -257,10 +287,10 @@ private void RunWithHandlingUninstallError(Action uninstallAction)
{
var message = new List<string>
{
string.Format(Update.LocalizableStrings.UpdateToolFailed, _packageId)
string.Format(Update.LocalizableStrings.UpdateToolFailed, packageId)
};
message.AddRange(
ToolUninstallCommandLowLevelErrorConverter.GetUserFacingMessages(ex, _packageId));
ToolUninstallCommandLowLevelErrorConverter.GetUserFacingMessages(ex, packageId));

throw new GracefulException(
messages: message,
Expand All @@ -280,12 +310,12 @@ private void RunWithHandlingUninstallError(Action uninstallAction)
return configFile;
}

private IToolPackage GetOldPackage(IToolPackageStoreQuery toolPackageStoreQuery)
private IToolPackage GetOldPackage(IToolPackageStoreQuery toolPackageStoreQuery, PackageId packageId)
{
IToolPackage oldPackageNullable;
try
{
oldPackageNullable = toolPackageStoreQuery.EnumeratePackageVersions(_packageId).SingleOrDefault();
oldPackageNullable = toolPackageStoreQuery.EnumeratePackageVersions(packageId).SingleOrDefault();
}
catch (InvalidOperationException)
{
Expand All @@ -294,7 +324,7 @@ private IToolPackage GetOldPackage(IToolPackageStoreQuery toolPackageStoreQuery)
{
string.Format(
Update.LocalizableStrings.ToolHasMultipleVersionsInstalled,
_packageId),
packageId),
},
isUserError: false);
}
Expand Down
Loading