Adding dotnet remove project tests
This commit is contained in:
parent
07ddddfa88
commit
20f2242947
15 changed files with 643 additions and 178 deletions
|
@ -11,84 +11,56 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Tools.Add;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
|
||||
{
|
||||
public class AddProjectToSolutionCommand
|
||||
public class AddProjectToSolution : IAddSubCommand
|
||||
{
|
||||
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
|
||||
private SlnFile _slnFile;
|
||||
|
||||
public AddProjectToSolution(string fileOrDirectory)
|
||||
{
|
||||
CommandLineApplication app = parentApp.Command("project", throwOnUnexpectedArg: false);
|
||||
app.FullName = LocalizableStrings.AppFullName;
|
||||
app.Description = LocalizableStrings.AppDescription;
|
||||
app.HandleRemainingArguments = true;
|
||||
app.ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText;
|
||||
|
||||
app.HelpOption("-h|--help");
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(projectOrDirectory);
|
||||
|
||||
if (app.RemainingArguments.Count == 0)
|
||||
{
|
||||
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
|
||||
}
|
||||
|
||||
List<string> projectPaths = app.RemainingArguments;
|
||||
PathUtility.EnsureAllPathsExist(projectPaths, CommonLocalizableStrings.ProjectDoesNotExist);
|
||||
var relativeProjectPaths = projectPaths.Select((p) =>
|
||||
PathUtility.GetRelativePath(
|
||||
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
|
||||
Path.GetFullPath(p))).ToList();
|
||||
|
||||
int preAddProjectCount = slnFile.Projects.Count;
|
||||
foreach (var project in relativeProjectPaths)
|
||||
{
|
||||
AddProject(slnFile, project);
|
||||
}
|
||||
|
||||
if (slnFile.Projects.Count > preAddProjectCount)
|
||||
{
|
||||
slnFile.Write();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (GracefulException e)
|
||||
{
|
||||
Reporter.Error.WriteLine(e.Message.Red());
|
||||
app.ShowHelp();
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
_slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
|
||||
}
|
||||
|
||||
private static void AddProject(SlnFile slnFile, string projectPath)
|
||||
public int Add(List<string> projectPaths)
|
||||
{
|
||||
if (projectPaths.Count == 0)
|
||||
{
|
||||
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
|
||||
}
|
||||
|
||||
PathUtility.EnsureAllPathsExist(projectPaths, CommonLocalizableStrings.ProjectDoesNotExist);
|
||||
var relativeProjectPaths = projectPaths.Select((p) =>
|
||||
PathUtility.GetRelativePath(
|
||||
PathUtility.EnsureTrailingSlash(_slnFile.BaseDirectory),
|
||||
Path.GetFullPath(p))).ToList();
|
||||
|
||||
int preAddProjectCount = _slnFile.Projects.Count;
|
||||
foreach (var project in relativeProjectPaths)
|
||||
{
|
||||
AddProject(project);
|
||||
}
|
||||
|
||||
if (_slnFile.Projects.Count > preAddProjectCount)
|
||||
{
|
||||
_slnFile.Write();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void AddProject(string projectPath)
|
||||
{
|
||||
var projectPathNormalized = PathUtility.GetPathWithBackSlashes(projectPath);
|
||||
|
||||
if (slnFile.Projects.Any((p) =>
|
||||
if (_slnFile.Projects.Any((p) =>
|
||||
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
Reporter.Output.WriteLine(string.Format(
|
||||
CommonLocalizableStrings.SolutionAlreadyContainsProject,
|
||||
slnFile.FullPath,
|
||||
_slnFile.FullPath,
|
||||
projectPath));
|
||||
}
|
||||
else
|
||||
|
@ -122,10 +94,33 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
|
|||
FilePath = projectPathNormalized
|
||||
};
|
||||
|
||||
slnFile.Projects.Add(slnProject);
|
||||
_slnFile.Projects.Add(slnProject);
|
||||
Reporter.Output.WriteLine(
|
||||
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, projectPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AddProjectToSolutionCommand : AddSubCommandBase
|
||||
{
|
||||
protected override string CommandName => "project";
|
||||
protected override string LocalizedDisplayName => LocalizableStrings.AppFullName;
|
||||
protected override string LocalizedDescription => LocalizableStrings.AppDescription;
|
||||
protected override string LocalizedHelpText => LocalizableStrings.AppHelpText;
|
||||
|
||||
internal override void AddCustomOptions(CommandLineApplication app)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IAddSubCommand CreateIAddSubCommand(string fileOrDirectory)
|
||||
{
|
||||
return new AddProjectToSolution(fileOrDirectory);
|
||||
}
|
||||
|
||||
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
|
||||
{
|
||||
var addSubCommand = new AddProjectToSolutionCommand();
|
||||
return addSubCommand.Create(parentApp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue