Adding dotnet remove project tests

This commit is contained in:
Justin Goshi 2016-12-15 15:48:04 -08:00
parent 07ddddfa88
commit 20f2242947
15 changed files with 643 additions and 178 deletions

View file

@ -4,6 +4,7 @@
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Add;
using Microsoft.DotNet.Tools.Common;
using NuGet.Frameworks;
using System.Collections.Generic;
@ -13,114 +14,86 @@ using System.Text;
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
public class AddProjectToProjectReferenceCommand
public class AddProjectToProjectReference : IAddSubCommand
{
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
private CommandOption _frameworkOption;
private MsbuildProject _msbuildProj;
private ProjectCollection _projects;
internal AddProjectToProjectReference(string fileOrDirectory, CommandOption frameworkOption)
{
CommandLineApplication app = parentApp.Command("p2p", throwOnUnexpectedArg: false);
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
app.HandleRemainingArguments = true;
app.ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText;
app.HelpOption("-h|--help");
CommandOption frameworkOption = app.Option(
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
LocalizableStrings.CmdFrameworkDescription,
CommandOptionType.SingleValue);
app.OnExecute(() =>
{
try
{
if (!parentApp.Arguments.Any())
{
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
}
var projectOrDirectory = parentApp.Arguments.First().Value;
if (string.IsNullOrEmpty(projectOrDirectory))
{
projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
}
var projects = new ProjectCollection();
var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, projectOrDirectory);
if (app.RemainingArguments.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
}
string frameworkString = frameworkOption.Value();
List<string> references = app.RemainingArguments;
PathUtility.EnsureAllPathsExist(references, CommonLocalizableStrings.ReferenceDoesNotExist);
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(projects, r));
if (frameworkString == null)
{
foreach (var tfm in msbuildProj.GetTargetFrameworks())
{
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(tfm))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
@ref,
msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
return 1;
}
}
}
}
else
{
var framework = NuGetFramework.Parse(frameworkString);
if (!msbuildProj.IsTargettingFramework(framework))
{
Reporter.Error.WriteLine(string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, msbuildProj.ProjectRootElement.FullPath, frameworkString));
return 1;
}
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(framework))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
@ref,
new string[] { frameworkString }));
return 1;
}
}
}
var relativePathReferences = references.Select((r) =>
PathUtility.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r))).ToList();
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
frameworkOption.Value(),
relativePathReferences);
if (numberOfAddedReferences != 0)
{
msbuildProj.ProjectRootElement.Save();
}
return 0;
}
catch (GracefulException e)
{
Reporter.Error.WriteLine(e.Message.Red());
app.ShowHelp();
return 1;
}
});
return app;
_projects = new ProjectCollection();
_msbuildProj = MsbuildProject.FromFileOrDirectory(_projects, fileOrDirectory);
_frameworkOption = frameworkOption;
}
private static string GetProjectNotCompatibleWithFrameworksDisplayString(MsbuildProject project, IEnumerable<string> frameworksDisplayStrings)
public int Add(List<string> references)
{
if (references.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
}
string frameworkString = _frameworkOption.Value();
PathUtility.EnsureAllPathsExist(references, CommonLocalizableStrings.ReferenceDoesNotExist);
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(_projects, r));
if (frameworkString == null)
{
foreach (var tfm in _msbuildProj.GetTargetFrameworks())
{
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(tfm))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
@ref,
_msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
return 1;
}
}
}
}
else
{
var framework = NuGetFramework.Parse(frameworkString);
if (!_msbuildProj.IsTargettingFramework(framework))
{
Reporter.Error.WriteLine(string.Format(
CommonLocalizableStrings.ProjectDoesNotTargetFramework,
_msbuildProj.ProjectRootElement.FullPath,
frameworkString));
return 1;
}
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(framework))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
@ref,
new string[] { frameworkString }));
return 1;
}
}
}
var relativePathReferences = references.Select((r) =>
PathUtility.GetRelativePath(_msbuildProj.ProjectDirectory, Path.GetFullPath(r))).ToList();
int numberOfAddedReferences = _msbuildProj.AddProjectToProjectReferences(
_frameworkOption.Value(),
relativePathReferences);
if (numberOfAddedReferences != 0)
{
_msbuildProj.ProjectRootElement.Save();
}
return 0;
}
private string GetProjectNotCompatibleWithFrameworksDisplayString(MsbuildProject project, IEnumerable<string> frameworksDisplayStrings)
{
var sb = new StringBuilder();
sb.AppendLine(string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFrameworks, project.ProjectRootElement.FullPath));
@ -132,4 +105,33 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
return sb.ToString();
}
}
public class AddProjectToProjectReferenceCommand : AddSubCommandBase
{
private CommandOption _frameworkOption;
protected override string CommandName => "p2p";
protected override string LocalizedDisplayName => LocalizableStrings.AppFullName;
protected override string LocalizedDescription => LocalizableStrings.AppDescription;
protected override string LocalizedHelpText => LocalizableStrings.AppHelpText;
internal override void AddCustomOptions(CommandLineApplication app)
{
_frameworkOption = app.Option(
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
LocalizableStrings.CmdFrameworkDescription,
CommandOptionType.SingleValue);
}
protected override IAddSubCommand CreateIAddSubCommand(string fileOrDirectory)
{
return new AddProjectToProjectReference(fileOrDirectory, _frameworkOption);
}
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var addSubCommand = new AddProjectToProjectReferenceCommand();
return addSubCommand.Create(parentApp);
}
}
}