From 1ddf5c87c7add3e1b9b9dcc81c2dc4aee536ed4d Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Thu, 7 Dec 2017 17:19:51 -0800 Subject: [PATCH] Add support for accepting directories to add and remove reference commands. This commit adds support for specifying directories containing a single project to both the `add reference` and `remove reference` commands. Fixes issue #7343. --- .../DotnetAddP2PProjects/Empty/README | 2 + src/dotnet/CommonLocalizableStrings.resx | 3 -- .../dotnet-add-reference/Program.cs | 11 ++-- .../dotnet-remove-reference/Program.cs | 15 +++++- .../xlf/CommonLocalizableStrings.cs.xlf | 5 -- .../xlf/CommonLocalizableStrings.de.xlf | 5 -- .../xlf/CommonLocalizableStrings.es.xlf | 5 -- .../xlf/CommonLocalizableStrings.fr.xlf | 5 -- .../xlf/CommonLocalizableStrings.it.xlf | 5 -- .../xlf/CommonLocalizableStrings.ja.xlf | 5 -- .../xlf/CommonLocalizableStrings.ko.xlf | 5 -- .../xlf/CommonLocalizableStrings.pl.xlf | 5 -- .../xlf/CommonLocalizableStrings.pt-BR.xlf | 5 -- .../xlf/CommonLocalizableStrings.ru.xlf | 5 -- .../xlf/CommonLocalizableStrings.tr.xlf | 5 -- .../xlf/CommonLocalizableStrings.zh-Hans.xlf | 5 -- .../xlf/CommonLocalizableStrings.zh-Hant.xlf | 5 -- .../GivenDotnetAddReference.cs | 54 ++++++++++++++++++- .../GivenDotnetRemoveP2P.cs | 51 ++++++++++++++++++ 19 files changed, 125 insertions(+), 76 deletions(-) create mode 100644 TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Empty/README diff --git a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Empty/README b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Empty/README new file mode 100644 index 000000000..439cfe9c7 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Empty/README @@ -0,0 +1,2 @@ +This directory is intentionally empty. + diff --git a/src/dotnet/CommonLocalizableStrings.resx b/src/dotnet/CommonLocalizableStrings.resx index 3733b73f5..e51b2a473 100644 --- a/src/dotnet/CommonLocalizableStrings.resx +++ b/src/dotnet/CommonLocalizableStrings.resx @@ -345,9 +345,6 @@ Specified solution file {0} does not exist, or there is no solution file in the directory. - - Reference {0} does not exist. - Reference `{0}` is invalid. diff --git a/src/dotnet/commands/dotnet-add/dotnet-add-reference/Program.cs b/src/dotnet/commands/dotnet-add/dotnet-add-reference/Program.cs index e79ebfc0c..31d1f716f 100644 --- a/src/dotnet/commands/dotnet-add/dotnet-add-reference/Program.cs +++ b/src/dotnet/commands/dotnet-add/dotnet-add-reference/Program.cs @@ -45,9 +45,9 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference var frameworkString = _appliedCommand.ValueOrDefault("framework"); - PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ReferenceDoesNotExist); + PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true); List refs = _appliedCommand.Arguments - .Select((r) => MsbuildProject.FromFile(projects, r)) + .Select((r) => MsbuildProject.FromFileOrDirectory(projects, r)) .ToList(); if (frameworkString == null) @@ -90,9 +90,10 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference } } - var relativePathReferences = _appliedCommand.Arguments.Select((r) => - Path.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r))) - .ToList(); + var relativePathReferences = refs.Select((r) => + Path.GetRelativePath( + msbuildProj.ProjectDirectory, + r.ProjectRootElement.FullPath)).ToList(); int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences( frameworkString, diff --git a/src/dotnet/commands/dotnet-remove/dotnet-remove-reference/Program.cs b/src/dotnet/commands/dotnet-remove/dotnet-remove-reference/Program.cs index e813dda3a..4f8fabb61 100644 --- a/src/dotnet/commands/dotnet-remove/dotnet-remove-reference/Program.cs +++ b/src/dotnet/commands/dotnet-remove/dotnet-remove-reference/Program.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.IO; using System.Linq; using Microsoft.Build.Evaluation; using Microsoft.DotNet.Cli; @@ -42,10 +43,22 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference public override int Execute() { var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), _fileOrDirectory); + var references = _appliedCommand.Arguments.Select(p => { + var fullPath = Path.GetFullPath(p); + if (!Directory.Exists(fullPath)) + { + return p; + } + + return Path.GetRelativePath( + msbuildProj.ProjectRootElement.FullPath, + MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName + ); + }); int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences( _appliedCommand.ValueOrDefault("framework"), - _appliedCommand.Arguments); + references); if (numberOfRemovedReferences != 0) { diff --git a/src/dotnet/xlf/CommonLocalizableStrings.cs.xlf b/src/dotnet/xlf/CommonLocalizableStrings.cs.xlf index 9b36d6610..ccc70ef3d 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.cs.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.cs.xlf @@ -97,11 +97,6 @@ Aplikace - - Reference {0} does not exist. - Odkaz na {0} neexistuje. - - Reference `{0}` added to the project. Odkaz na {0} byl přidán do projektu. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.de.xlf b/src/dotnet/xlf/CommonLocalizableStrings.de.xlf index d530378af..ec92f7055 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.de.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.de.xlf @@ -97,11 +97,6 @@ Anwendung - - Reference {0} does not exist. - Der Verweis "{0}" ist nicht vorhanden. - - Reference `{0}` added to the project. Der Verweis "{0}" wurde dem Projekt hinzugefügt. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.es.xlf b/src/dotnet/xlf/CommonLocalizableStrings.es.xlf index 65dc74bad..e00b791e1 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.es.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.es.xlf @@ -97,11 +97,6 @@ Aplicación - - Reference {0} does not exist. - La referencia {0} no existe. - - Reference `{0}` added to the project. Se ha agregado la referencia "{0}" al proyecto. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.fr.xlf b/src/dotnet/xlf/CommonLocalizableStrings.fr.xlf index 09daf3906..cd9ba1cee 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.fr.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.fr.xlf @@ -97,11 +97,6 @@ Application - - Reference {0} does not exist. - La référence {0} n'existe pas. - - Reference `{0}` added to the project. Référence '{0}' ajoutée au projet. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.it.xlf b/src/dotnet/xlf/CommonLocalizableStrings.it.xlf index d6f24164c..5ab33a95b 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.it.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.it.xlf @@ -97,11 +97,6 @@ Applicazione - - Reference {0} does not exist. - Il riferimento {0} non esiste. - - Reference `{0}` added to the project. Il riferimento `{0}` è stato aggiunto al progetto. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.ja.xlf b/src/dotnet/xlf/CommonLocalizableStrings.ja.xlf index ded00eee1..4d025f02b 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.ja.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.ja.xlf @@ -97,11 +97,6 @@ アプリケーション - - Reference {0} does not exist. - 参照 {0} は存在しません。 - - Reference `{0}` added to the project. 参照 `{0}` がプロジェクトに追加されました。 diff --git a/src/dotnet/xlf/CommonLocalizableStrings.ko.xlf b/src/dotnet/xlf/CommonLocalizableStrings.ko.xlf index 8e3256909..f9f9a96c0 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.ko.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.ko.xlf @@ -97,11 +97,6 @@ 응용 프로그램 - - Reference {0} does not exist. - {0} 참조가 없습니다. - - Reference `{0}` added to the project. 프로젝트에 '{0}' 참조가 추가되었습니다. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.pl.xlf b/src/dotnet/xlf/CommonLocalizableStrings.pl.xlf index 9f6b7e9cf..ca40ba7d5 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.pl.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.pl.xlf @@ -97,11 +97,6 @@ Aplikacja - - Reference {0} does not exist. - Odwołanie {0} nie istnieje. - - Reference `{0}` added to the project. Do projektu zostało dodane odwołanie „{0}”. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.pt-BR.xlf b/src/dotnet/xlf/CommonLocalizableStrings.pt-BR.xlf index 85b8cfa1b..1fe5abd21 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.pt-BR.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.pt-BR.xlf @@ -97,11 +97,6 @@ Aplicativo - - Reference {0} does not exist. - A referência {0} não existe. - - Reference `{0}` added to the project. A referência ‘{0}’ foi adicionada ao projeto. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.ru.xlf b/src/dotnet/xlf/CommonLocalizableStrings.ru.xlf index b89bef3de..5f250b68d 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.ru.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.ru.xlf @@ -97,11 +97,6 @@ Приложение - - Reference {0} does not exist. - Ссылка {0} не существует. - - Reference `{0}` added to the project. Ссылка "{0}" добавлена в проект. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.tr.xlf b/src/dotnet/xlf/CommonLocalizableStrings.tr.xlf index 552aa93f6..7ddbad9b0 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.tr.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.tr.xlf @@ -97,11 +97,6 @@ Uygulama - - Reference {0} does not exist. - {0} başvurusu yok. - - Reference `{0}` added to the project. `{0}` başvurusu projeye eklendi. diff --git a/src/dotnet/xlf/CommonLocalizableStrings.zh-Hans.xlf b/src/dotnet/xlf/CommonLocalizableStrings.zh-Hans.xlf index 9c68c45f7..c3bf2ce59 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.zh-Hans.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.zh-Hans.xlf @@ -97,11 +97,6 @@ 应用程序 - - Reference {0} does not exist. - 引用 {0} 不存在。 - - Reference `{0}` added to the project. 已将引用“{0}”添加到项目。 diff --git a/src/dotnet/xlf/CommonLocalizableStrings.zh-Hant.xlf b/src/dotnet/xlf/CommonLocalizableStrings.zh-Hant.xlf index e22c55fa5..4a6eedb67 100644 --- a/src/dotnet/xlf/CommonLocalizableStrings.zh-Hant.xlf +++ b/src/dotnet/xlf/CommonLocalizableStrings.zh-Hant.xlf @@ -97,11 +97,6 @@ 應用程式 - - Reference {0} does not exist. - 參考 {0} 不存在。 - - Reference `{0}` added to the project. 參考 `{0}` 已新增至專案。 diff --git a/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs b/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs index ba37b1e73..75269124f 100644 --- a/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs +++ b/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs @@ -559,7 +559,7 @@ Commands: .WithProject(lib.CsProjName) .Execute("\"IDoNotExist.csproj\""); cmd.Should().Fail(); - cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj")); + cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj")); lib.CsProjContent().Should().BeEquivalentTo(contentBefore); } @@ -575,7 +575,7 @@ Commands: .WithProject(lib.CsProjPath) .Execute($"\"{setup.ValidRefCsprojPath}\" \"IDoNotExist.csproj\""); cmd.Should().Fail(); - cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj")); + cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj")); lib.CsProjContent().Should().BeEquivalentTo(contentBefore); } @@ -693,5 +693,55 @@ Commands: cmd.StdErr.Should().MatchRegex(" - net45"); net45lib.CsProjContent().Should().BeEquivalentTo(csProjContent); } + + [Fact] + public void WhenDirectoryContainingProjectIsGivenReferenceIsAdded() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + + var result = new AddReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{Path.GetDirectoryName(setup.ValidRefCsprojPath)}\""); + + result.Should().Pass(); + result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj")); + result.StdErr.Should().BeEmpty(); + } + + [Fact] + public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + + var reference = "Empty"; + var result = new AddReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute(reference); + + result.Should().Fail(); + result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText); + result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, reference)); + } + + [Fact] + public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + + var reference = "MoreThanOne"; + var result = new AddReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute(reference); + + result.Should().Fail(); + result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText); + result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, reference)); + } } } diff --git a/test/dotnet-remove-reference.Tests/GivenDotnetRemoveP2P.cs b/test/dotnet-remove-reference.Tests/GivenDotnetRemoveP2P.cs index 65da417b2..b557ff5df 100644 --- a/test/dotnet-remove-reference.Tests/GivenDotnetRemoveP2P.cs +++ b/test/dotnet-remove-reference.Tests/GivenDotnetRemoveP2P.cs @@ -506,5 +506,56 @@ Commands: csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); csproj.NumberOfProjectReferencesWithIncludeContaining(validref.Name).Should().Be(0); } + + [Fact] + public void WhenDirectoryContainingProjectIsGivenReferenceIsRemoved() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + var libref = AddLibRef(setup, lib); + + var result = new RemoveReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\""); + + result.Should().Pass(); + result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, Path.Combine("Lib", setup.LibCsprojName))); + result.StdErr.Should().BeEmpty(); + } + + [Fact] + public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + + var reference = "Empty"; + var result = new RemoveReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute(reference); + + result.Should().Fail(); + result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText); + result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, Path.Combine(setup.TestRoot, reference))); + } + + [Fact] + public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation() + { + var setup = Setup(); + var lib = NewLibWithFrameworks(dir: setup.TestRoot); + + var reference = "MoreThanOne"; + var result = new RemoveReferenceCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute(reference); + + result.Should().Fail(); + result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText); + result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, Path.Combine(setup.TestRoot, reference))); + } } }