WIP Refactor and improve tests

This commit is contained in:
Justin Goshi 2016-12-16 01:04:09 -08:00
parent 20f2242947
commit 023a899db0
17 changed files with 425 additions and 470 deletions

View file

@ -1,56 +1,67 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
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.Remove;
namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
{
public class RemoveProjectFromSolution : IRemoveSubCommand
internal class RemoveProjectFromSolutionCommand : DotNetSubCommandBase
{
private SlnFile _slnFile;
public RemoveProjectFromSolution(string fileOrDirectory)
public static DotNetSubCommandBase Create()
{
_slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
var command = new RemoveProjectFromSolutionCommand()
{
Name = "project",
FullName = LocalizableStrings.AppFullName,
Description = LocalizableStrings.AppDescription,
HandleRemainingArguments = true,
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
};
command.HelpOption("-h|--help");
return command;
}
public void Remove(IList<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.SpecifyAtLeastOneProjectToRemove);
}
var relativeProjectPaths = projectPaths.Select((p) =>
var relativeProjectPaths = RemainingArguments.Select((p) =>
PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(_slnFile.BaseDirectory),
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
Path.GetFullPath(p))).ToList();
bool slnChanged = false;
foreach (var path in relativeProjectPaths)
{
slnChanged |= RemoveProject(path);
slnChanged |= RemoveProject(slnFile, path);
}
if (slnChanged)
{
_slnFile.Write();
slnFile.Write();
}
return 0;
}
private bool RemoveProject(string projectPath)
private bool RemoveProject(SlnFile slnFile, string projectPath)
{
var projectPathNormalized = PathUtility.GetPathWithBackSlashes(projectPath);
var projectsToRemove = _slnFile.Projects.Where((p) =>
var projectsToRemove = slnFile.Projects.Where((p) =>
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)).ToList();
bool projectRemoved = false;
@ -64,7 +75,7 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
{
foreach (var slnProject in projectsToRemove)
{
_slnFile.Projects.Remove(slnProject);
slnFile.Projects.Remove(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, slnProject.FilePath));
}
@ -75,27 +86,4 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
return projectRemoved;
}
}
public class RemoveProjectFromSolutionCommand : RemoveSubCommandBase
{
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 IRemoveSubCommand CreateIRemoveSubCommand(string fileOrDirectory)
{
return new RemoveProjectFromSolution(fileOrDirectory);
}
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var removeSubCommand = new RemoveProjectFromSolutionCommand();
return removeSubCommand.Create(parentApp);
}
}
}