diff --git a/src/Microsoft.DotNet.Cli.Utils/GracefulException.cs b/src/Microsoft.DotNet.Cli.Utils/GracefulException.cs index 4c061e4b7..817f8d03d 100644 --- a/src/Microsoft.DotNet.Cli.Utils/GracefulException.cs +++ b/src/Microsoft.DotNet.Cli.Utils/GracefulException.cs @@ -12,6 +12,10 @@ namespace Microsoft.DotNet.Cli.Utils { } + public GracefulException(string format, params string[] args) : this(string.Format(format, args)) + { + } + public GracefulException(string message, Exception innerException) : base(message, innerException) { } diff --git a/src/dotnet/Strings.cs b/src/dotnet/Strings.cs new file mode 100644 index 000000000..484da984b --- /dev/null +++ b/src/dotnet/Strings.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.DotNet.Tools +{ + internal static class Strings + { + // Arguments parsing + public const string RequiredArgumentNotPassed = "Required argument {0} was not passed."; + public const string RequiredArgumentIsInvalid = "Required argument {0} is invalid."; + + // Project + public const string CouldNotFindProjectOrDirectory = "Could not find project or directory `{0}`."; + public const string CouldNotFindAnyProjectInDirectory = "Could not find any project in `{0}`."; + public const string MoreThanOneProjectInDirectory = "Found more than one project in `{0}`. Please specify which one to use."; + public const string FoundInvalidProject = "Found a project `{0}` but it is invalid."; + public const string ProjectIsInvalid = "Invalid project `{0}`."; + public const string ProjectDoesNotExist = "Project `{0}` does not exist."; + + // Project Reference + public const string ProjectAlreadyHasAreference = "Project already has a reference to `{0}`."; + public const string ReferenceAddedToTheProject = "Reference `{0}` added to the project."; + public const string ReferenceDoesNotExist = "Reference `{0}` does not exist."; + public const string SpecifyAtLeastOneReference = "You must specify at least one reference to add."; + } +} diff --git a/src/dotnet/commands/dotnet-add-p2p/Program.cs b/src/dotnet/commands/dotnet-add-p2p/Program.cs index fa1fb36f2..cc2e50a6d 100644 --- a/src/dotnet/commands/dotnet-add-p2p/Program.cs +++ b/src/dotnet/commands/dotnet-add-p2p/Program.cs @@ -49,7 +49,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference app.OnExecute(() => { if (string.IsNullOrEmpty(projectArgument.Value)) { - throw new GracefulException("Argument is required."); + throw new GracefulException(Strings.RequiredArgumentNotPassed, ""); } ProjectRootElement project; @@ -69,7 +69,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference if (app.RemainingArguments.Count == 0) { - throw new GracefulException("You must specify at least one reference to add."); + throw new GracefulException(Strings.SpecifyAtLeastOneReference); } List references = app.RemainingArguments; @@ -120,7 +120,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference throw new GracefulException( string.Join( Environment.NewLine, - notExisting.Select((ne) => $"Reference `{ne}` does not exist."))); + notExisting.Select((r) => string.Format(Strings.ReferenceDoesNotExist, r)))); } } @@ -148,19 +148,18 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference { if (!File.Exists(filename)) { - throw new GracefulException($"Provided project `{filename}` does not exist."); + throw new GracefulException(Strings.ProjectDoesNotExist, filename); } var project = TryOpenProject(filename); if (project == null) { - throw new GracefulException($"Invalid project `{filename}`."); + throw new GracefulException(Strings.ProjectIsInvalid, filename); } return project; } - // TODO: improve errors internal static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory) { DirectoryInfo dir; @@ -170,36 +169,36 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference } catch (ArgumentException) { - throw new GracefulException($"Could not find project or directory `{directory}`."); + throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory); } if (!dir.Exists) { - throw new GracefulException($"Could not find project or directory `{directory}`."); + throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory); } FileInfo[] files = dir.GetFiles("*proj"); if (files.Length == 0) { - throw new GracefulException($"Could not find any project in `{directory}`."); + throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory); } if (files.Length > 1) { - throw new GracefulException("Found more than one project in the current directory. Please specify which one to use."); + throw new GracefulException(Strings.MoreThanOneProjectInDirectory, directory); } FileInfo projectFile = files.First(); if (!projectFile.Exists) { - throw new GracefulException($"Could not find any project in `{directory}`."); + throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory); } var ret = TryOpenProject(projectFile.FullName); if (ret == null) { - throw new GracefulException($"Found a project `{projectFile.FullName}` but it is invalid."); + throw new GracefulException(Strings.FoundInvalidProject, projectFile.FullName); } return ret; @@ -220,14 +219,14 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference { if (root.HasExistingItemWithCondition(framework, @ref)) { - Reporter.Output.WriteLine($"Project already has a reference to `{@ref}`."); + Reporter.Output.WriteLine(string.Format(Strings.ProjectAlreadyHasAreference, @ref)); continue; } numberOfAddedReferences++; itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref)); - Reporter.Output.WriteLine($"Reference `{@ref}` added to the project."); + Reporter.Output.WriteLine(string.Format(Strings.ReferenceAddedToTheProject, @ref)); } return numberOfAddedReferences; diff --git a/src/dotnet/commands/dotnet-add/Program.cs b/src/dotnet/commands/dotnet-add/Program.cs index 2418fdb6e..f563c5ee4 100644 --- a/src/dotnet/commands/dotnet-add/Program.cs +++ b/src/dotnet/commands/dotnet-add/Program.cs @@ -58,7 +58,7 @@ Commands: } else if (args.Length == 1) { - Reporter.Error.WriteLine("Required argument was not passed.".Red()); + Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentNotPassed, "").Red()); Reporter.Output.WriteLine(HelpText); return 1; } @@ -76,7 +76,7 @@ Commands: return builtin(args); } - Reporter.Error.WriteLine("Required argument is invalid.".Red()); + Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentIsInvalid, "").Red()); Reporter.Output.WriteLine(HelpText); return 1; }