Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Linq;
using NuGet.ProjectModel;

namespace NuGet.CommandLine.XPlat
namespace NuGet.CommandLine.XPlat.Commands.Why
{
internal static class DependencyGraphFinder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using NuGet.Shared;

namespace NuGet.CommandLine.XPlat
namespace NuGet.CommandLine.XPlat.Commands.Why
{
internal static class DependencyGraphPrinter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Collections.Generic;
using NuGet.Shared;

namespace NuGet.CommandLine.XPlat
namespace NuGet.CommandLine.XPlat.Commands.Why
{
/// <summary>
/// Represents a node in the package dependency graph.
Expand Down
102 changes: 102 additions & 0 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Why/WhyCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.CommandLine;
using System.CommandLine.Help;
using System.IO;
using Microsoft.Extensions.CommandLineUtils;

namespace NuGet.CommandLine.XPlat.Commands.Why
{
internal static class WhyCommand
{
internal static void Register(CommandLineApplication app)
{
app.Command("why", whyCmd =>
{
whyCmd.Description = Strings.WhyCommand_Description;
});
}

internal static void Register(CliCommand rootCommand, Func<ILoggerWithColor> getLogger)
{
Register(rootCommand, getLogger, WhyCommandRunner.ExecuteCommand);
}

internal static void Register(CliCommand rootCommand, Func<ILoggerWithColor> getLogger, Func<WhyCommandArgs, int> action)
{
var whyCommand = new CliCommand("why", Strings.WhyCommand_Description);

CliArgument<string> path = new CliArgument<string>("PROJECT|SOLUTION")
{
Description = Strings.WhyCommand_PathArgument_Description,
// We really want this to be zero or one, however, because this is the first argument, it doesn't work.
// Instead, we need to use a CustomParser to choose if the argument is the path, or the package.
// In order for the parser to tell us there's more than 1 argument available, we need to tell CliArgument
// that it supports more than one, but then in the custom parser we'll make sure we only take at most 1.
Arity = ArgumentArity.ZeroOrMore,
CustomParser = ar =>
{
if (ar.Tokens.Count > 1)
{
var value = ar.Tokens[0];
ar.OnlyTake(1);
return value.Value;
}

ar.OnlyTake(0);
var currentDirectory = Directory.GetCurrentDirectory();
return currentDirectory;
}
};

CliArgument<string> package = new CliArgument<string>("PACKAGE")
{
Description = Strings.WhyCommand_PackageArgument_Description,
Arity = ArgumentArity.ExactlyOne
};

CliOption<List<string>> frameworks = new CliOption<List<string>>("--framework", "-f")
{
Description = Strings.WhyCommand_FrameworksOption_Description,
Arity = ArgumentArity.OneOrMore
};

HelpOption help = new HelpOption()
{
Arity = ArgumentArity.Zero
};

whyCommand.Arguments.Add(path);
whyCommand.Arguments.Add(package);
whyCommand.Options.Add(frameworks);
whyCommand.Options.Add(help);

whyCommand.SetAction((parseResult) =>
{
ILoggerWithColor logger = getLogger();

try
{
var whyCommandArgs = new WhyCommandArgs(
parseResult.GetValue(path),
parseResult.GetValue(package),
parseResult.GetValue(frameworks),
logger);

int exitCode = action(whyCommandArgs);
return exitCode;
}
catch (ArgumentException ex)
{
logger.LogError(ex.Message);
return ExitCodes.InvalidArguments;
}
});

rootCommand.Subcommands.Add(whyCommand);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.Collections.Generic;

namespace NuGet.CommandLine.XPlat
namespace NuGet.CommandLine.XPlat.Commands.Why
{
internal class WhyCommandArgs
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.Build.Evaluation;
using NuGet.ProjectModel;

namespace NuGet.CommandLine.XPlat
namespace NuGet.CommandLine.XPlat.Commands.Why
{
internal static class WhyCommandRunner
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public static int ExecuteCommand(WhyCommandArgs whyCommandArgs)
if (dependencyGraphPerFramework != null)
{
whyCommandArgs.Logger.LogMinimal(
string.Format(
string.Format(CultureInfo.CurrentCulture,
Strings.WhyCommand_Message_DependencyGraphsFoundInProject,
assetsFile.PackageSpec.Name,
targetPackage));
Expand All @@ -73,7 +73,7 @@ public static int ExecuteCommand(WhyCommandArgs whyCommandArgs)
else
{
whyCommandArgs.Logger.LogMinimal(
string.Format(
string.Format(CultureInfo.CurrentCulture,
Strings.WhyCommand_Message_NoDependencyGraphsFoundInProject,
assetsFile.PackageSpec.Name,
targetPackage));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@
[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.CommandLine.XPlat.ListPackage.ListPackageConsoleRenderer.GetProjectHeader(System.String,NuGet.CommandLine.XPlat.ListPackageArgs)~System.String")]
[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.CommandLine.XPlat.ListPackageCommand.GetOutputType(System.String,System.String)~NuGet.CommandLine.XPlat.ListPackage.IReportRenderer")]
[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.CommandLine.XPlat.UILanguageOverride.SetIfNotAlreadySet(System.String,System.Int32)")]
[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.CommandLine.XPlat.WhyCommandRunner.ExecuteCommand(NuGet.CommandLine.XPlat.WhyCommandArgs)~System.Int32")]
10 changes: 5 additions & 5 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.Commands;
using NuGet.Common;
using System.CommandLine;
using System.Threading;
using System.CommandLine.Parsing;

namespace NuGet.CommandLine.XPlat
{
Expand Down Expand Up @@ -101,7 +101,7 @@ public static int MainInternal(string[] args, CommandOutputLogger log)
rootCommand = new CliCommand("nuget");

ConfigCommand.Register(rootCommand, getHidePrefixLogger);
WhyCommand.Register(rootCommand, getHidePrefixLogger);
Commands.Why.WhyCommand.Register(rootCommand, getHidePrefixLogger);
}

CancellationTokenSource tokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -277,7 +277,7 @@ private static CommandLineApplication InitializeApp(string[] args, CommandOutput
SignCommand.Register(app, getHidePrefixLogger, setLogLevel, () => new SignCommandRunner());
// The commands below are implemented with System.CommandLine, and are here only for `dotnet nuget --help`
ConfigCommand.Register(app);
WhyCommand.Register(app);
Commands.Why.WhyCommand.Register(app);
}

app.FullName = Strings.App_FullName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading.Tasks;
using NuGet.CommandLine.XPlat;
using NuGet.CommandLine.XPlat.Commands.Why;
using NuGet.Packaging;
using NuGet.Test.Utility;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
using System.Collections.Generic;
using System.Linq;
using NuGet.CommandLine.XPlat;
using NuGet.CommandLine.XPlat.Commands.Why;
using NuGet.ProjectModel;
using NuGet.Test.Utility;
using Test.Utility;
using Xunit;

namespace NuGet.CommandLine.Xplat.Tests
namespace NuGet.CommandLine.Xplat.Tests.Commands.Why
{
public class XPlatWhyUnitTests
public class DependencyGraphFinderTests
{
[Fact]
public void WhyCommand_DependencyGraphFinder_MultipleDependencyPathsForTargetPackage_AllPathsFound()
public void MultipleDependencyPathsForTargetPackage_AllPathsFound()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand Down Expand Up @@ -45,7 +46,7 @@ public void WhyCommand_DependencyGraphFinder_MultipleDependencyPathsForTargetPac
}

[Fact]
public void WhyCommand_DependencyGraphFinder_DependencyOnTargetProject_AllPathsFound()
public void DependencyOnTargetProject_AllPathsFound()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand Down Expand Up @@ -73,7 +74,7 @@ public void WhyCommand_DependencyGraphFinder_DependencyOnTargetProject_AllPathsF
}

[Fact]
public void WhyCommand_DependencyGraphFinder_NoDependencyOnTargetPackage_ReturnsNullGraph()
public void NoDependencyOnTargetPackage_ReturnsNullGraph()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand All @@ -98,7 +99,7 @@ public void WhyCommand_DependencyGraphFinder_NoDependencyOnTargetPackage_Returns
}

[Fact]
public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackageForOnlyOneFramework_ReturnsCorrectGraphs()
public void DependencyOnTargetPackageForOnlyOneFramework_ReturnsCorrectGraphs()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand Down Expand Up @@ -126,7 +127,7 @@ public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackageForOnlyOne
}

[Fact]
public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackage_FrameworkOptionSpecified_PathIsFound()
public void DependencyOnTargetPackage_FrameworkOptionSpecified_PathIsFound()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand All @@ -151,7 +152,7 @@ public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackage_Framework
}

[Fact]
public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackageForOnlyOneFramework_DifferentFrameworkSpecified_ReturnsNullGraph()
public void DependencyOnTargetPackageForOnlyOneFramework_DifferentFrameworkSpecified_ReturnsNullGraph()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand All @@ -176,7 +177,7 @@ public void WhyCommand_DependencyGraphFinder_DependencyOnTargetPackageForOnlyOne
}

[Fact]
public void WhyCommand_DependencyGraphFinder_DifferentCaseUsedForTargetPackageId_MatchesAreCaseInsensitive_AllPathsFound()
public void DifferentCaseUsedForTargetPackageId_MatchesAreCaseInsensitive_AllPathsFound()
{
// Arrange
var lockFileFormat = new LockFileFormat();
Expand Down
Loading