Extract error messages to separate class
This commit is contained in:
parent
3fc0517cda
commit
592af9dd61
4 changed files with 46 additions and 16 deletions
|
@ -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)
|
public GracefulException(string message, Exception innerException) : base(message, innerException)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
27
src/dotnet/Strings.cs
Normal file
27
src/dotnet/Strings.cs
Normal file
|
@ -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.";
|
||||||
|
}
|
||||||
|
}
|
|
@ -49,7 +49,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||||
app.OnExecute(() => {
|
app.OnExecute(() => {
|
||||||
if (string.IsNullOrEmpty(projectArgument.Value))
|
if (string.IsNullOrEmpty(projectArgument.Value))
|
||||||
{
|
{
|
||||||
throw new GracefulException("Argument <Project> is required.");
|
throw new GracefulException(Strings.RequiredArgumentNotPassed, "<Project>");
|
||||||
}
|
}
|
||||||
|
|
||||||
ProjectRootElement project;
|
ProjectRootElement project;
|
||||||
|
@ -69,7 +69,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||||
|
|
||||||
if (app.RemainingArguments.Count == 0)
|
if (app.RemainingArguments.Count == 0)
|
||||||
{
|
{
|
||||||
throw new GracefulException("You must specify at least one reference to add.");
|
throw new GracefulException(Strings.SpecifyAtLeastOneReference);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<string> references = app.RemainingArguments;
|
List<string> references = app.RemainingArguments;
|
||||||
|
@ -120,7 +120,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||||
throw new GracefulException(
|
throw new GracefulException(
|
||||||
string.Join(
|
string.Join(
|
||||||
Environment.NewLine,
|
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))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Provided project `{filename}` does not exist.");
|
throw new GracefulException(Strings.ProjectDoesNotExist, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
var project = TryOpenProject(filename);
|
var project = TryOpenProject(filename);
|
||||||
if (project == null)
|
if (project == null)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Invalid project `{filename}`.");
|
throw new GracefulException(Strings.ProjectIsInvalid, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: improve errors
|
|
||||||
internal static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory)
|
internal static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory)
|
||||||
{
|
{
|
||||||
DirectoryInfo dir;
|
DirectoryInfo dir;
|
||||||
|
@ -170,36 +169,36 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||||
}
|
}
|
||||||
catch (ArgumentException)
|
catch (ArgumentException)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Could not find project or directory `{directory}`.");
|
throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dir.Exists)
|
if (!dir.Exists)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Could not find project or directory `{directory}`.");
|
throw new GracefulException(Strings.CouldNotFindProjectOrDirectory, directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo[] files = dir.GetFiles("*proj");
|
FileInfo[] files = dir.GetFiles("*proj");
|
||||||
if (files.Length == 0)
|
if (files.Length == 0)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Could not find any project in `{directory}`.");
|
throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (files.Length > 1)
|
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();
|
FileInfo projectFile = files.First();
|
||||||
|
|
||||||
if (!projectFile.Exists)
|
if (!projectFile.Exists)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Could not find any project in `{directory}`.");
|
throw new GracefulException(Strings.CouldNotFindAnyProjectInDirectory, directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ret = TryOpenProject(projectFile.FullName);
|
var ret = TryOpenProject(projectFile.FullName);
|
||||||
if (ret == null)
|
if (ret == null)
|
||||||
{
|
{
|
||||||
throw new GracefulException($"Found a project `{projectFile.FullName}` but it is invalid.");
|
throw new GracefulException(Strings.FoundInvalidProject, projectFile.FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -220,14 +219,14 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||||
{
|
{
|
||||||
if (root.HasExistingItemWithCondition(framework, @ref))
|
if (root.HasExistingItemWithCondition(framework, @ref))
|
||||||
{
|
{
|
||||||
Reporter.Output.WriteLine($"Project already has a reference to `{@ref}`.");
|
Reporter.Output.WriteLine(string.Format(Strings.ProjectAlreadyHasAreference, @ref));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
numberOfAddedReferences++;
|
numberOfAddedReferences++;
|
||||||
itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));
|
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;
|
return numberOfAddedReferences;
|
||||||
|
|
|
@ -58,7 +58,7 @@ Commands:
|
||||||
}
|
}
|
||||||
else if (args.Length == 1)
|
else if (args.Length == 1)
|
||||||
{
|
{
|
||||||
Reporter.Error.WriteLine("Required argument <command> was not passed.".Red());
|
Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentNotPassed, "<command>").Red());
|
||||||
Reporter.Output.WriteLine(HelpText);
|
Reporter.Output.WriteLine(HelpText);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ Commands:
|
||||||
return builtin(args);
|
return builtin(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
Reporter.Error.WriteLine("Required argument <command> is invalid.".Red());
|
Reporter.Error.WriteLine(string.Format(Strings.RequiredArgumentIsInvalid, "<command>").Red());
|
||||||
Reporter.Output.WriteLine(HelpText);
|
Reporter.Output.WriteLine(HelpText);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue