WIP Refactor and improve tests
This commit is contained in:
parent
20f2242947
commit
023a899db0
17 changed files with 425 additions and 470 deletions
|
@ -3,64 +3,73 @@
|
|||
|
||||
using Microsoft.Build.Construction;
|
||||
using Microsoft.Build.Evaluation;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli;
|
||||
using Microsoft.DotNet.Cli.Sln.Internal;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Tools.Common;
|
||||
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 AddProjectToSolution : IAddSubCommand
|
||||
internal class AddProjectToSolutionCommand : DotNetSubCommandBase
|
||||
{
|
||||
private SlnFile _slnFile;
|
||||
|
||||
public AddProjectToSolution(string fileOrDirectory)
|
||||
public static DotNetSubCommandBase Create()
|
||||
{
|
||||
_slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
|
||||
var command = new AddProjectToSolutionCommand()
|
||||
{
|
||||
Name = "project",
|
||||
FullName = LocalizableStrings.AppFullName,
|
||||
Description = LocalizableStrings.AppDescription,
|
||||
HandleRemainingArguments = true,
|
||||
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
|
||||
};
|
||||
|
||||
command.HelpOption("-h|--help");
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
public int Add(List<string> projectPaths)
|
||||
public override int Run(string fileOrDirectory)
|
||||
{
|
||||
if (projectPaths.Count == 0)
|
||||
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
|
||||
|
||||
if (RemainingArguments.Count == 0)
|
||||
{
|
||||
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
|
||||
}
|
||||
|
||||
PathUtility.EnsureAllPathsExist(projectPaths, CommonLocalizableStrings.ProjectDoesNotExist);
|
||||
var relativeProjectPaths = projectPaths.Select((p) =>
|
||||
PathUtility.EnsureAllPathsExist(RemainingArguments, CommonLocalizableStrings.ProjectDoesNotExist);
|
||||
var relativeProjectPaths = RemainingArguments.Select((p) =>
|
||||
PathUtility.GetRelativePath(
|
||||
PathUtility.EnsureTrailingSlash(_slnFile.BaseDirectory),
|
||||
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
|
||||
Path.GetFullPath(p))).ToList();
|
||||
|
||||
int preAddProjectCount = _slnFile.Projects.Count;
|
||||
int preAddProjectCount = slnFile.Projects.Count;
|
||||
foreach (var project in relativeProjectPaths)
|
||||
{
|
||||
AddProject(project);
|
||||
AddProject(slnFile, project);
|
||||
}
|
||||
|
||||
if (_slnFile.Projects.Count > preAddProjectCount)
|
||||
if (slnFile.Projects.Count > preAddProjectCount)
|
||||
{
|
||||
_slnFile.Write();
|
||||
slnFile.Write();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void AddProject(string projectPath)
|
||||
private void AddProject(SlnFile slnFile, 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
|
||||
|
@ -94,33 +103,10 @@ 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