-
Notifications
You must be signed in to change notification settings - Fork 690
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
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
953959d
Adding base list code
3a15724
cleaning up usage'
73aa031
adding working code
7bba36a
adding code for generating result in dictionary format
916db49
cleaning up prints
af4ac3d
moving strings to resource files
14dd2ba
Adding tests for package filter
b743e81
Adding more test coverage
5273e28
Adding collection tag
cc12cda
Fixing add package registration
75f3b43
fixing add package registration in tests
9815f6c
adding comment
546f810
fixing rebase conflicts
8cabbec
addressing some PR comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackageReferenceCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
{ | ||
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, | ||
Strings.Error_PkgMissingOrInvalidProjectFile, | ||
commandName, | ||
projectPath.Value())); | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
....CommandLine.XPlat/Commands/PackageReferenceCommands/ListPackageReferenceCommandRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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($"--------------------------------------------------"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.