Implement dotnet remove proj

This commit is contained in:
Justin Goshi 2016-12-15 13:40:46 -08:00
parent 441277ccfa
commit 07ddddfa88
7 changed files with 240 additions and 56 deletions

View file

@ -0,0 +1,12 @@
// 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 System.Collections.Generic;
namespace Microsoft.DotNet.Tools.Remove
{
public interface IRemoveSubCommand
{
void Remove(IList<string> items);
}
}

View file

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools.Remove.ProjectFromSolution;
using Microsoft.DotNet.Tools.Remove.ProjectToProjectReference;
namespace Microsoft.DotNet.Tools.Remove
@ -16,6 +17,7 @@ namespace Microsoft.DotNet.Tools.Remove
internal override List<Func<CommandLineApplication, CommandLineApplication>> SubCommands =>
new List<Func<CommandLineApplication, CommandLineApplication>>
{
RemoveProjectFromSolutionCommand.CreateApplication,
RemoveProjectToProjectReferenceCommand.CreateApplication,
};

View file

@ -0,0 +1,64 @@
// 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.Utils;
using Microsoft.DotNet.Tools.Common;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.DotNet.Tools.Remove
{
public abstract class RemoveSubCommandBase
{
protected abstract string CommandName { get; }
protected abstract string LocalizedDisplayName { get; }
protected abstract string LocalizedDescription { get; }
protected abstract string LocalizedHelpText { get; }
internal abstract void AddCustomOptions(CommandLineApplication app);
protected abstract IRemoveSubCommand CreateIRemoveSubCommand(string fileOrDirectory);
internal CommandLineApplication Create(CommandLineApplication parentApp)
{
CommandLineApplication app = parentApp.Command(CommandName, throwOnUnexpectedArg: false);
app.FullName = LocalizedDisplayName;
app.Description = LocalizedDescription;
app.HandleRemainingArguments = true;
app.ArgumentSeparatorHelpText = LocalizedHelpText;
app.HelpOption("-h|--help");
AddCustomOptions(app);
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 removeSubCommand = CreateIRemoveSubCommand(projectOrDirectory);
removeSubCommand.Remove(app.RemainingArguments);
return 0;
}
catch (GracefulException e)
{
Reporter.Error.WriteLine(e.Message.Red());
app.ShowHelp();
return 1;
}
});
return app;
}
}
}

View file

@ -9,7 +9,5 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
public const string AppHelpText = "Project to project references to remove";
public const string CmdFrameworkDescription = "Remove reference only when targetting a specific framework";
public const string SpecifyAtLeastOneReferenceToRemove = "You must specify at least one reference to delete. Please run dotnet delete --help for more information.";
}
}

View file

@ -4,73 +4,66 @@
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Tools.Remove;
namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
{
public class RemoveProjectToProjectReferenceCommand
public class RemoveProjectToProjectReference : IRemoveSubCommand
{
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
private CommandOption _frameworkOption;
private MsbuildProject _msbuildProj;
internal RemoveProjectToProjectReference(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;
_msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), fileOrDirectory);
_frameworkOption = frameworkOption;
}
app.HelpOption("-h|--help");
public void Remove(IList<string> references)
{
if (references.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
}
CommandOption frameworkOption = app.Option(
int numberOfRemovedReferences = _msbuildProj.RemoveProjectToProjectReferences(
_frameworkOption.Value(),
references);
if (numberOfRemovedReferences != 0)
{
_msbuildProj.ProjectRootElement.Save();
}
}
}
public class RemoveProjectToProjectReferenceCommand : RemoveSubCommandBase
{
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);
}
app.OnExecute(() => {
try
{
if (!parentApp.Arguments.Any())
{
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
}
protected override IRemoveSubCommand CreateIRemoveSubCommand(string fileOrDirectory)
{
return new RemoveProjectToProjectReference(fileOrDirectory, _frameworkOption);
}
var projectOrDirectory = parentApp.Arguments.First().Value;
if (string.IsNullOrEmpty(projectOrDirectory))
{
projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
}
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), projectOrDirectory);
if (app.RemainingArguments.Count == 0)
{
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
}
List<string> references = app.RemainingArguments;
int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences(
frameworkOption.Value(),
references);
if (numberOfRemovedReferences != 0)
{
msbuildProj.ProjectRootElement.Save();
}
return 0;
}
catch (GracefulException e)
{
Reporter.Error.WriteLine(e.Message.Red());
app.ShowHelp();
return 1;
}
});
return app;
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var removeSubCommand = new RemoveProjectToProjectReferenceCommand();
return removeSubCommand.Create(parentApp);
}
}
}

View file

@ -0,0 +1,14 @@
// 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.
namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
{
internal class LocalizableStrings
{
public const string AppFullName = ".NET Remove Project from Solution Command";
public const string AppDescription = "Command to remove projects from a solution";
public const string AppHelpText = "Projects to remove from a solution";
}
}

View file

@ -0,0 +1,101 @@
// 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.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
{
private SlnFile _slnFile;
public RemoveProjectFromSolution(string fileOrDirectory)
{
_slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
}
public void Remove(IList<string> projectPaths)
{
if (projectPaths.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove);
}
var relativeProjectPaths = projectPaths.Select((p) =>
PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(_slnFile.BaseDirectory),
Path.GetFullPath(p))).ToList();
bool slnChanged = false;
foreach (var path in relativeProjectPaths)
{
slnChanged |= RemoveProject(_slnFile, path);
}
if (slnChanged)
{
_slnFile.Write();
}
}
private static bool RemoveProject(SlnFile slnFile, string projectPath)
{
var projectPathNormalized = PathUtility.GetPathWithBackSlashes(projectPath);
var projectsToRemove = slnFile.Projects.Where((p) =>
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)).ToList();
bool projectRemoved = false;
if (projectsToRemove.Count == 0)
{
Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.ProjectReferenceCouldNotBeFound,
projectPath));
}
else
{
foreach (var slnProject in projectsToRemove)
{
slnFile.Projects.Remove(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, slnProject.FilePath));
}
projectRemoved = true;
}
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);
}
}
}