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

Add support for dotnet list package command #1320

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public async Task<int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs,
packageReferenceArgs.PackageDependency.Id,
packageReferenceArgs.ProjectPath));

var compatibleOriginalFrameworks = originalPackageSpec.RestoreMetadata
var compatibleOriginalFrameworks = originalPackageSpec
.RestoreMetadata
.OriginalTargetFrameworks
.Where(s => compatibleFrameworks.Contains(NuGetFramework.Parse(s)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.Versioning;

namespace NuGet.CommandLine.XPlat
{
public class ListPackageReferenceCommand
{
public static void Register(CommandLineApplication app, Func<ILogger> getLogger,
Func<IPackageReferenceCommandRunner> getCommandRunner)
{
app.Command("list", listPkg =>
{
listPkg.Description = Strings.ListPkg_Description;
listPkg.HelpOption(XPlatUtility.HelpOption);

listPkg.Option(
CommandConstants.ForceEnglishOutputOption,
Strings.ForceEnglishOutput_Description,
CommandOptionType.NoValue);

var id = listPkg.Option(
"--package",
Strings.ListPkg_PackageIdDescription,
CommandOptionType.SingleValue);

var projectPath = listPkg.Option(
"-p|--project",
Strings.ListPkg_ProjectPathDescription,
CommandOptionType.SingleValue);

var frameworks = listPkg.Option(
"-f|--framework",
Strings.ListPkg_FrameworksDescription,
CommandOptionType.MultipleValue);


listPkg.OnExecute(() =>
{
ValidateArgument(projectPath, listPkg.Name);
ValidateProjectPath(projectPath, listPkg.Name);

var logger = getLogger();

var packageReferenceArgs = new PackageReferenceArgs(projectPath.Value(), logger)
{
PackageDependency = id.HasValue() ? new PackageDependency(id.Value(), VersionRange.Parse("*")) : null,
Frameworks = CommandLineUtility.SplitAndJoinAcrossMultipleValues(frameworks.Values)
};

var msBuild = new MSBuildAPIUtility(logger);
var listPackageCommandRunner = getCommandRunner();
return listPackageCommandRunner.ExecuteCommand(packageReferenceArgs, msBuild);
});
});
}

private static void ValidateArgument(CommandOption arg, string commandName)
{
if (arg.Values.Count < 1)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_PkgMissingArgument,
commandName,
arg.Template));
}
}

private static void ValidateProjectPath(CommandOption projectPath, string commandName)
{
if (!File.Exists(projectPath.Value()) || !projectPath.Value().EndsWith("proj", StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

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

what does this return if the option was not given? will this be a null ref?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

projectPath cannot be null, otherwise the command will throw help.

{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
Strings.Error_PkgMissingOrInvalidProjectFile,
commandName,
projectPath.Value()));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Evaluation;
using NuGet.Common;

namespace NuGet.CommandLine.XPlat
{
class ListPackageReferenceCommandRunner : IPackageReferenceCommandRunner
{
public Task<int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs, MSBuildAPIUtility msBuild)
{
var packagReferences = msBuild.ListPackageReference(packageReferenceArgs.ProjectPath,
packageReferenceArgs.PackageDependency, packageReferenceArgs.Frameworks);

PrintPackageReferences(packagReferences, packageReferenceArgs);

return Task.FromResult(0);
}

public void PrintPackageReferences(Dictionary<string, IEnumerable<Tuple<string, string>>> packageReferences, PackageReferenceArgs packageReferenceArgs)
{
var logger = packageReferenceArgs.Logger;
var projectPath = packageReferenceArgs.ProjectPath;
var packageDependency = packageReferenceArgs.PackageDependency;
if(packageReferences.Keys.Count == 0)
{
logger.LogInformation(string.Format(Strings.ListPkg_NoPackageRefsForProject, projectPath));
}
else
{
logger.LogInformation(string.Format(Strings.ListPkg_References, projectPath));

foreach (var framework in packageReferences.Keys)
{
logger.LogInformation(string.Format(Strings.ListPkg_Framework, framework));
logger.LogInformation("--------------------------------------------------");

if (packageReferences[framework] == null)
{
logger.LogInformation(string.Format(Strings.ListPkg_NonTargetedFramework, framework));
}
else if(!packageReferences[framework].Any())
{
if(packageDependency == null)
{
logger.LogInformation(string.Format(Strings.ListPkg_NoPackageRefsForFramework, framework));
}
else
{
logger.LogInformation(string.Format(Strings.ListPkg_PackageNotReferencedForFramework,
packageDependency.Id,
framework));
}
}
else
{
foreach (var packageReference in packageReferences[framework])
{
logger.LogInformation(string.Format(Strings.ListPkg_PackageAndVersion, packageReference.Item1, packageReference.Item2));
}
}
logger.LogInformation($"--------------------------------------------------");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public class PackageReferenceArgs
public string PackageDirectory { get; set; }
public bool NoRestore { get; set; }

public PackageReferenceArgs(string projectPath, ILogger logger)
{
ValidateArgument(projectPath);
ValidateArgument(logger);

ProjectPath = projectPath;
Logger = logger;
}

public PackageReferenceArgs(string projectPath, PackageDependency packageDependency, ILogger logger, bool noVersion)
{
ValidateArgument(projectPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.0.1" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
<PackageReference Include="Microsoft.Build.Runtime" Version="15.1.1012" />
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
</ItemGroup>

<ItemGroup>
<Compile Update="Strings.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
5 changes: 3 additions & 2 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static int MainInternal(string[] args, CommandOutputLogger log)

log.LogVerbose(string.Format(CultureInfo.CurrentCulture, Strings.OutputNuGetVersion, app.FullName, app.LongVersionGetter()));

int exitCode = 0;
var exitCode = 0;

try
{
Expand Down Expand Up @@ -143,6 +143,7 @@ private static void RegisterCommands(CommandLineApplication app, CommandOutputLo
{
AddPackageReferenceCommand.Register(app, () => log, () => new AddPackageReferenceCommandRunner());
RemovePackageReferenceCommand.Register(app, () => log, () => new RemovePackageReferenceCommandRunner());
ListPackageReferenceCommand.Register(app, () => log, () => new ListPackageReferenceCommandRunner());
}
else
{
Expand All @@ -159,7 +160,7 @@ private static void RegisterCommands(CommandLineApplication app, CommandOutputLo
/// </summary>
private static bool TryParseVerbosity(string[] args, CommandOption verbosity, out LogLevel logLevel)
{
bool found = false;
var found = false;

for (var index = 0; index < args.Length; index++)
{
Expand Down
108 changes: 108 additions & 0 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.Designer.cs

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

45 changes: 45 additions & 0 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,49 @@ For more information, visit http://docs.nuget.org/docs/reference/command-line-re
<comment>{0} - Package Id
{1} - Project path</comment>
</data>
<data name="ListPkg_Description" xml:space="preserve">
<value>Lists package references for a project.</value>
</data>
<data name="ListPkg_PackageIdDescription" xml:space="preserve">
<value>Id of the package for listing references.</value>
</data>
<data name="ListPkg_FrameworksDescription" xml:space="preserve">
<value>Frameworks for which the package reference should be listed.</value>
</data>
<data name="ListPkg_ProjectPathDescription" xml:space="preserve">
<value>Path to the project file.</value>
</data>
<data name="ListPkg_AllFrameworks" xml:space="preserve">
<value>All Frameworks</value>
</data>
<data name="ListPkg_Framework" xml:space="preserve">
<value>Framework '{0}' -</value>
<comment>{0} - Framework Identifier</comment>
</data>
<data name="ListPkg_NonTargetedFramework" xml:space="preserve">
<value>This poject does not target framework '{0}'.</value>
<comment>{0} - Framework Identifier</comment>
</data>
<data name="ListPkg_NoPackageRefsForFramework" xml:space="preserve">
<value>This poject does not reference any package for framework '{0}'.</value>
<comment>{0} - Framework Identifier</comment>
</data>
<data name="ListPkg_NoPackageRefsForProject" xml:space="preserve">
<value>Project '{0}' has no package references.</value>
<comment>{0} - Path to the project file.</comment>
</data>
<data name="ListPkg_PackageAndVersion" xml:space="preserve">
<value>Package '{0}' Version '{1}'.</value>
<comment>{0} - Package Identifier
{1} - Package Version</comment>
</data>
<data name="ListPkg_PackageNotReferencedForFramework" xml:space="preserve">
<value>This poject does not reference package '{0}' for framework '{1}'.</value>
<comment>{0} - Package Identifier
{1} - Framework Identifier</comment>
</data>
<data name="ListPkg_References" xml:space="preserve">
<value>Project '{0}' has following package references per framework -</value>
<comment>{0} - Path to the project file.</comment>
</data>
</root>
Loading