From 8a2be5761757b0f0984ffeaa5ff5865cfc1118a6 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Wed, 15 Mar 2017 13:59:39 -0700 Subject: [PATCH] text fixes for dotnet remove --- .../commands/dotnet-add/AddCommandParser.cs | 8 ++- .../commands/dotnet-complete/ParseCommand.cs | 11 +++- .../commands/dotnet-complete/Suggest.cs | 56 ++++++++++++------- .../GivenDotnetAddReference.cs | 12 ++-- 4 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/dotnet/commands/dotnet-add/AddCommandParser.cs b/src/dotnet/commands/dotnet-add/AddCommandParser.cs index b507a88dc..781ffb450 100644 --- a/src/dotnet/commands/dotnet-add/AddCommandParser.cs +++ b/src/dotnet/commands/dotnet-add/AddCommandParser.cs @@ -55,13 +55,15 @@ namespace Microsoft.DotNet.Cli .ForwardAs(o => $"--package-directory {o.Arguments.Single()}"))), Create.Command( "reference", - "Command to add project to project reference", - Accept.OneOrMoreArguments(), + Tools.Add.ProjectToProjectReference.LocalizableStrings.AppFullName, + Accept.OneOrMoreArguments() + .With(name: "args", + description: Tools.Add.ProjectToProjectReference.LocalizableStrings.AppHelpText), CommonOptions.HelpOption(), Create.Option("-f|--framework", "Add reference only when targeting a specific framework", Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile) - .With(name: "FRAMEWORK"))), + .With(name: "FRAMEWORK"))), CommonOptions.HelpOption()); public static IEnumerable QueryNuGet(string match) diff --git a/src/dotnet/commands/dotnet-complete/ParseCommand.cs b/src/dotnet/commands/dotnet-complete/ParseCommand.cs index 3fc2b1be7..fb86c6300 100644 --- a/src/dotnet/commands/dotnet-complete/ParseCommand.cs +++ b/src/dotnet/commands/dotnet-complete/ParseCommand.cs @@ -11,9 +11,16 @@ namespace Microsoft.DotNet.Cli { DebugHelper.HandleDebugSwitch(ref args); - var result = - Parser.Instance.Parse( + ParseResult result; + try + { + result = Parser.Instance.Parse( args.Single()); + } + catch (Exception e) + { + throw new InvalidOperationException("The parser threw an exception.", e); + } Console.WriteLine(result.Diagram()); diff --git a/src/dotnet/commands/dotnet-complete/Suggest.cs b/src/dotnet/commands/dotnet-complete/Suggest.cs index e4b78e65c..e9dd0c7ef 100644 --- a/src/dotnet/commands/dotnet-complete/Suggest.cs +++ b/src/dotnet/commands/dotnet-complete/Suggest.cs @@ -1,8 +1,11 @@ +using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.Build.Evaluation; +using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools; +using static System.Array; namespace Microsoft.DotNet.Cli { @@ -10,33 +13,46 @@ namespace Microsoft.DotNet.Cli { public static IEnumerable TargetFrameworksFromProjectFile() { - var msbuildProj = MsbuildProject.FromFileOrDirectory( - new ProjectCollection(), - Directory.GetCurrentDirectory()); + var msBuildProject = GetMSBuildProject(); - foreach (var tfm in msbuildProj.GetTargetFrameworks()) + if (msBuildProject == null) + { + yield break; + } + + foreach (var tfm in msBuildProject.GetTargetFrameworks()) { yield return tfm.GetShortFolderName(); } } - public static IEnumerable RunTimesFromProjectFile() + private static void Report(Exception e) => + Reporter.Verbose.WriteLine($"Exception occurred while getting suggestions: {e}"); + + public static IEnumerable RunTimesFromProjectFile() => + GetMSBuildProject() + .GetRuntimeIdentifiers() ?? + Empty(); + + public static IEnumerable ProjectReferencesFromProjectFile() => + GetMSBuildProject() + ?.GetProjectToProjectReferences() + .Select(r => r.Include) ?? + Empty(); + + private static MsbuildProject GetMSBuildProject() { - var msbuildProj = MsbuildProject.FromFileOrDirectory( - new ProjectCollection(), - Directory.GetCurrentDirectory()); - - return msbuildProj.GetRuntimeIdentifiers(); - } - - public static IEnumerable ProjectReferencesFromProjectFile() - { - var msbuildProj = MsbuildProject.FromFileOrDirectory( - new ProjectCollection(), - Directory.GetCurrentDirectory()); - - return msbuildProj.GetProjectToProjectReferences() - .Select(r => r.Include); + try + { + return MsbuildProject.FromFileOrDirectory( + new ProjectCollection(), + Directory.GetCurrentDirectory()); + } + catch (Exception e) + { + Report(e); + return null; + } } } } \ No newline at end of file diff --git a/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs b/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs index 0f71da16c..71984d279 100644 --- a/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs +++ b/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs @@ -16,17 +16,15 @@ namespace Microsoft.DotNet.Cli.Add.Reference.Tests { private const string HelpText = @".NET Add Project to Project reference Command -Usage: dotnet add reference [options] [args] +Usage: dotnet add reference [options] Arguments: - The project file to operate on. If a file is not specified, the command will search the current directory for one. + The project file to operate on. If a file is not specified, the command will search the current directory for one. + Project to project references to add Options: - -h|--help Show help information - -f|--framework Add reference only when targeting a specific framework - -Additional Arguments: - Project to project references to add + -h, --help Show help information + -f, --framework Add reference only when targeting a specific framework "; const string FrameworkNet451Arg = "-f net451";