Skip to content

Commit

Permalink
[dotnet tool] Add ability to check if a tool is installed (#28333)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaqiWang18 committed Oct 25, 2022
1 parent d1b3d6b commit d6c515a
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@
<data name="ListToolCommandInvalidGlobalAndLocalAndToolPath" xml:space="preserve">
<value>The local option(--local), the global option (--global), the tool path option (--tool-path), can only have one at a time. Specify only one of the options: {0}.</value>
</data>
<data name="PackageIdArgumentDescription" xml:space="preserve">
<value>The NuGet Package Id of the tool to list</value>
</data>
<data name="PackageIdArgumentName" xml:space="preserve">
<value>PACKAGE_ID</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Microsoft.DotNet.Cli
{
internal static class ToolListCommandParser
{
public static readonly Argument<string> PackageIdArgument = new Argument<string>(LocalizableStrings.PackageIdArgumentName)
{
Description = LocalizableStrings.PackageIdArgumentDescription,
Arity = ArgumentArity.ZeroOrOne,
};

public static readonly Option<bool> GlobalOption = ToolAppliedOption.GlobalOption;

public static readonly Option<bool> LocalOption = ToolAppliedOption.LocalOption;
Expand All @@ -30,6 +36,7 @@ private static Command ConstructCommand()
{
var command = new Command("list", LocalizableStrings.CommandDescription);

command.AddArgument(PackageIdArgument);
command.AddOption(GlobalOption.WithHelpDescription(command, LocalizableStrings.GlobalOptionDescription));
command.AddOption(LocalOption.WithHelpDescription(command, LocalizableStrings.LocalOptionDescription));
command.AddOption(ToolPathOption.WithHelpDescription(command, LocalizableStrings.ToolPathOptionDescription));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public ToolListGlobalOrToolPathCommand(
public override int Execute()
{
var toolPathOption = _parseResult.GetValueForOption(ToolListCommandParser.ToolPathOption);
var packageIdArgument = _parseResult.GetValueForArgument(ToolListCommandParser.PackageIdArgument);

PackageId? packageId = null;
if (!string.IsNullOrWhiteSpace(packageIdArgument))
{
packageId = new PackageId(packageIdArgument);
}

DirectoryPath? toolPath = null;
if (!string.IsNullOrWhiteSpace(toolPathOption))
Expand Down Expand Up @@ -64,18 +71,29 @@ public override int Execute()
LocalizableStrings.CommandsColumn,
p => string.Join(CommandDelimiter, p.Commands.Select(c => c.Name)));

table.PrintRows(GetPackages(toolPath), l => _reporter.WriteLine(l));
var packageEnumerable = GetPackages(toolPath, packageId);
table.PrintRows(packageEnumerable, l => _reporter.WriteLine(l));
if (packageId.HasValue && !packageEnumerable.Any())
{
// return 1 if target package was not found
return 1;
}
return 0;
}

private IEnumerable<IToolPackage> GetPackages(DirectoryPath? toolPath)
private IEnumerable<IToolPackage> GetPackages(DirectoryPath? toolPath, PackageId? packageId)
{
return _createToolPackageStore(toolPath).EnumeratePackages()
.Where(PackageHasCommands)
.Where((p) => PackageHasCommands(p) && PackageIdMatches(p, packageId))
.OrderBy(p => p.Id)
.ToArray();
}

internal static bool PackageIdMatches(IToolPackage package, PackageId? packageId)
{
return !packageId.HasValue || package.Id.Equals(packageId);
}

private bool PackageHasCommands(IToolPackage package)
{
try
Expand Down
23 changes: 22 additions & 1 deletion src/Cli/dotnet/commands/dotnet-tool/list/ToolListLocalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolManifest;
using Microsoft.DotNet.ToolPackage;
using Microsoft.Extensions.EnvironmentAbstractions;

namespace Microsoft.DotNet.Tools.Tool.List
Expand All @@ -33,6 +34,12 @@ public ToolListLocalCommand(
public override int Execute()
{
var table = new PrintableTable<(ToolManifestPackage toolManifestPackage, FilePath SourceManifest)>();
var packageIdArgument = _parseResult.GetValueForArgument(ToolListCommandParser.PackageIdArgument);
PackageId? packageId = null;
if (!string.IsNullOrWhiteSpace(packageIdArgument))
{
packageId = new PackageId(packageIdArgument);
}

table.AddColumn(
LocalizableStrings.PackageIdColumn,
Expand All @@ -47,8 +54,22 @@ public override int Execute()
LocalizableStrings.ManifestFileColumn,
p => p.SourceManifest.Value);

table.PrintRows(_toolManifestInspector.Inspect(), l => _reporter.WriteLine(l));
var packageEnumerable = _toolManifestInspector.Inspect().Where(
(t) => PackageIdMatches(t.toolManifestPackage, packageId)
);
table.PrintRows(packageEnumerable, l => _reporter.WriteLine(l));

if (packageId.HasValue && !packageEnumerable.Any())
{
// return 1 if target package was not found
return 1;
}
return 0;
}

private bool PackageIdMatches(ToolManifestPackage package, PackageId? packageId)
{
return !packageId.HasValue || package.PackageId.Equals(packageId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifest</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Verze</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifest</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Version</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifiesto</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Versión</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifeste</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Version</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifesto</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Versione</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">マニフェスト</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">バージョン</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">매니페스트</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">버전</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifest</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Wersja</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Manifesto</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Versão</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Манифест</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Версия</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">Bildirim</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">Sürüm</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">清单</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">版本</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<target state="translated">資訊清單</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentDescription">
<source>The NuGet Package Id of the tool to list</source>
<target state="new">The NuGet Package Id of the tool to list</target>
<note />
</trans-unit>
<trans-unit id="PackageIdArgumentName">
<source>PACKAGE_ID</source>
<target state="new">PACKAGE_ID</target>
<note />
</trans-unit>
<trans-unit id="VersionColumn">
<source>Version</source>
<target state="translated">版本</target>
Expand Down
Loading

0 comments on commit d6c515a

Please sign in to comment.